dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles
Log | Files | Refs | README | LICENSE Back to index

youtube-channel-id (1006B)


      1 #!/usr/bin/env bash
      2 
      3 function help {
      4 	echo "Usage: ${0##*/} [add] <youtube-url>"
      5 	echo "or:    ${0##*/} [add] <youtube-channel-name>"
      6 	echo "if the optional command 'add' is used, the channel is added to the "
      7 	echo "r2e feed list"
      8 }
      9 
     10 [ $# -lt 1 ] && help && exit 1
     11 [ "$1" = "-h" ] && help && exit 0
     12 [ "$1" = "--help" ] && help && exit 0
     13 
     14 [ $# -eq 1 ] && url="$1"
     15 [ $# -eq 2 ] && url="$2"
     16 
     17 # detect if url or just channel name
     18 if ! [[ "$url" =~ ^(http:\/\/|https:\/\/).*$ ]]; then
     19 	url="https://www.youtube.com/user/$url"
     20 fi
     21 
     22 id="$(curl --silent "$url" | \
     23 	grep 'data-channel-external-id' | \
     24 	sed 's/.*data-channel-external-id="//' | \
     25 	sed 's/".*//' | \
     26 	sed '1q')"
     27 name="$(echo "$url" | sed 's/.*\///' )"
     28 
     29 if [ ! -n "$id" ]; then
     30 	echo "channel $name not found"
     31 	exit 1
     32 fi
     33 
     34 if [ "$1" = "add" ]; then
     35 	r2e add "youtube_${name}" \
     36 		"https://www.youtube.com/feeds/videos.xml?channel_id=${id}"
     37 	echo "$name ($id) added"
     38 else
     39 	echo "$id"
     40 	if type xclip >/dev/null 2>&1; then
     41 	    echo "$id" | xclip
     42 	fi
     43 fi