/useful/trunk-1

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/useful/trunk-1
1 by Gustav Hartvigsson
Inital code.
1
#!/usr/bin/env bash
2
####
3
# Launch a web browser with the chat pop-out for a YouTube live stream.
4
####
5
ARGS=()
6
ARGS="${@}"
7
8
9
__DEFAULT_BROWSER=xdg-open
10
__YOUTUBE_URL=""
11
12
function __usage () {
13
  echo ""
14
  echo "launch_youtube_chat.sh <YouTube-URL> [optional options]"
15
  echo ""
16
  echo "    Takes the provided URL for a YouTube Live Stream and starts a"
17
  echo "    web browser pointing to the address of the chat for that stream."
18
  echo ""
19
  echo " -h"
20
  echo " --help                       Display this help message."
21
  echo ""
22
  echo " -b <browser>" 
23
  echo " --browser <browser>          Use <browser> as the web browser instead"
24
  echo "                              of default as provided by xdg-open."
25
  echo ""
26
  exit 0
27
}
28
29
function __parse_youtube_url {
30
  __YOUTUBE_URL=$(echo $1 | sed -r 's/^.*[&?]v=(.{11})([&#].*)?$/\1/')
31
}
32
33
function __parse_args () {
34
  if [ -z "$1" ]
35
  then
36
    echo "Try --help or -h."
37
    exit 1
38
  fi
39
  
40
  
41
  
42
  while [[ $# -gt 0 ]]
43
  do
44
    
45
    case "${1}" in
46
      -b|--browser)
47
      __DEFAULT_BROWSER="$2"
48
      shift
49
      shift
50
      ;;
51
      -h|--help)
52
      __usage
53
      shift
54
      ;;
55
      *)
56
        if [[ "$__YOUTUBE_URL" == "" ]]
57
        then
58
          __parse_youtube_url $1
59
        else
60
          echo "Did you try to set the URL twice?"
61
          echo "Please run with --help for usage."
62
          exit 1
63
        fi
64
      shift
65
      ;;
66
      --)
67
      shift
68
      break
69
      ;;
70
    esac
71
  done
72
  
73
  if [[ "$__YOUTUBE_URL" == "" ]]
74
  then
75
    echo "URL not set"
76
    echo "Please run with --help for usage."
77
    exit 1
78
  fi
79
  
80
}
81
82
function __main () {
83
  #echo "Browser: " $__DEFAULT_BROWSER
84
  #echo "YouTube video-id: " $__YOUTUBE_URL
85
  
86
  local __final_url
87
  __final_url="https://www.youtube.com/live_chat?is_popout=true&v=""${__YOUTUBE_URL}"
88
  
89
  #echo $__final_url
90
  
91
  $__DEFAULT_BROWSER $__final_url
92
}
93
94
__parse_args "${@}"
95
__main