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