/useful/trunk-1

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