youtube-tui

Shell scripts to query YouTube from a terminal.
git clone git://git.concealed.world/youtube-tui
Log | Files | Refs | README | LICENSE

ytchannel (2875B)


      1 #!/bin/bash
      2 # A companion piece for ytsearch. Allows you to find the channel of videos, to use in RSS feed.
      3 # DEPENDS: youtube-dl/yt-dlp, xclip, coreutils
      4 #
      5 #    Copyright (C) <2020>  <nixx@firemail.cc>
      6 #
      7 #    This program is free software: you can redistribute it and/or modify
      8 #    it under the terms of the GNU General Public License as published by
      9 #    the Free Software Foundation, either version 3 of the License, or
     10 #    (at your option) any later version.
     11 #
     12 #    This program is distributed in the hope that it will be useful,
     13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 #    GNU General Public License for more details.
     16 #
     17 #    You should have received a copy of the GNU General Public License
     18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
     19 
     20 message () {
     21 cat << EOF
     22 ytchannel
     23 ~~~~~~~~~
     24 
     25 A companion piece for 'ytsearch'.
     26 Allows for "subscriptions" via RSS feed reader of choice.
     27 
     28 This allows you to find the channel of a video, 
     29 by downloaded file - assumes the default %(title)s-%(id)s.%(ext)s 
     30 format - or by a link. The channel is copied to the clipboard 
     31 (if running Xorg) and printed to stdout, both in format for an RSS feed.
     32 
     33 Usage:
     34 ytchannel --file file.exts
     35 ytchannel --link https://www.youtube.com/watch?v=link
     36 EOF
     37 }
     38 
     39 file () {
     40 find "${1}" > /dev/null 2>&1
     41 [[ $? == 1 ]] && echo File not found, exiting. && exit 1
     42 
     43 tmp=$(mktemp)
     44 
     45 origurl=$(echo "${1}" | rev | cut -d \. -f 2 | cut -c 1-11 | rev)
     46 ${downloader} -f worstaudio "https://www.youtube.com/watch?v=${origurl}" -o "%(title)s-%(channel_id)s.%(ext)s" > $tmp
     47 url=$(cat $tmp | grep "Destination" | head -n 1 | rev | cut -d \. -f 2 | cut -c 1-24 | rev)
     48 fullurl="https://www.youtube.com/feeds/videos.xml?channel_id=${url}"
     49 
     50 echo Channel Link: ${fullurl}
     51 if xset q >/dev/null 2>&1; then
     52 	printf "${fullurl}" | xclip -in -selection clipboard && echo Copied to clipboard.
     53 fi
     54 
     55 [[ -n "${url}" ]] && rm *"${url}"*
     56 rm $tmp
     57 }
     58 
     59 link () {
     60 origurl=$(echo "${1}" | rev | cut -c 1-11 | rev)
     61 
     62 tmp=$(mktemp)
     63 ${downloader} -f worstaudio "https://www.youtube.com/watch?v=${origurl}" -o "%(title)s-%(channel_id)s.%(ext)s" > $tmp
     64 
     65 url=$(cat $tmp | grep "Destination" | head -n 1 | rev | cut -d \. -f 2 | cut -c 1-24 | rev)
     66 fullurl="https://www.youtube.com/feeds/videos.xml?channel_id=${url}"
     67 
     68 echo Channel Link: ${fullurl}
     69 if xset q >/dev/null 2>&1; then
     70 	printf "${fullurl}" | xclip -in -selection clipboard && echo Copied to clipboard.
     71 fi
     72 
     73 [[ -n "${url}" ]] && rm *"${url}"*
     74 rm $tmp
     75 }
     76 
     77 [[ $1 == '-h' || $1 == '--help' ]] && message && exit 0
     78 [[ $# != 2 ]] && message && exit 1
     79 
     80 if [[ $(which yt-dlp > /dev/null 2>&1) ]]; then
     81 	downloader="youtube-dl"
     82 else
     83 	downloader="yt-dlp"
     84 fi
     85 
     86 case "$1" in
     87 	--file) file "${2}" && exit 0 ;;
     88 	--link) link "${2}" && exit 0 ;;
     89 	*) echo Unknown option. && exit 1
     90 esac