GegoXaren

launch_youtube_chat.sh

Jan 13th, 2021 (edited)
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.97 KB | None | 0 0
  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. ARGS=()
  9. ARGS="${@}"
  10.  
  11.  
  12. __DEFAULT_BROWSER=xdg-open
  13. __YOUTUBE_URL=""
  14.  
  15. function __usage () {
  16.   echo ""
  17.   echo "launch_youtube_chat.sh <YouTube-URL> [optional options]"
  18.   echo ""
  19.   echo "    Takes the provided URL for a YouTube Live Stream and starts a"
  20.   echo "    web browser pointing to the address of the chat for that stream."
  21.   echo ""
  22.   echo " -h"
  23.   echo " --help                       Display this help message."
  24.   echo ""
  25.   echo " -b <browser>"
  26.   echo " --browser <browser>          Use <browser> as the web browser instead"
  27.   echo "                              of default as provided by xdg-open."
  28.   echo ""
  29.   exit 0
  30. }
  31.  
  32. function __parse_youtube_url {
  33.   __YOUTUBE_URL=$(echo $1 | sed -r 's/^.*[&?]v=(.{11})([&#].*)?$/\1/')
  34. }
  35.  
  36. function __parse_args () {
  37.   if [[ -z "$1" ]]
  38.   then
  39.     echo "Try --help or -h."
  40.     exit 1
  41.   fi
  42.  
  43.  
  44.  
  45.   while [[ $# -gt 0 ]]
  46.   do
  47.    
  48.     case "${1}" in
  49.       -b|--browser)
  50.       __DEFAULT_BROWSER="$2"
  51.       shift
  52.       shift
  53.       ;;
  54.       -h|--help)
  55.       __usage
  56.       shift
  57.       ;;
  58.       *)
  59.         if [[ "$__YOUTUBE_URL" == "" ]]
  60.         then
  61.           __parse_youtube_url $1
  62.         else
  63.           echo "Did you try to set the URL twice?"
  64.           echo "Please run with --help for usage."
  65.           exit 1
  66.         fi
  67.       shift
  68.       ;;
  69.       --)
  70.       shift
  71.       break
  72.       ;;
  73.     esac
  74.   done
  75.  
  76.   if [[ "$__YOUTUBE_URL" == "" ]]
  77.   then
  78.     echo "URL not set"
  79.     echo "Please run with --help for usage."
  80.     exit 1
  81.   fi
  82.  
  83. }
  84.  
  85. function __main () {
  86.   #echo "Browser: " $__DEFAULT_BROWSER
  87.   #echo "YouTube video-id: " $__YOUTUBE_URL
  88.  
  89.   local __final_url
  90.   __final_url="https://www.youtube.com/live_chat?is_popout=true&v=""${__YOUTUBE_URL}"
  91.  
  92.   #echo $__final_url
  93.  
  94.   $__DEFAULT_BROWSER $__final_url
  95. }
  96.  
  97. __parse_args "${@}"
  98. __main
Add Comment
Please, Sign In to add comment