Advertisement
metalx1000

YouTube Subscription Viewer for your Shell v2

Jan 13th, 2018
1,028
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.92 KB | None | 0 0
  1. #!/bin/bash
  2. #created by Kris Occhipinti https://filmsbykris.com
  3. #Copyright Jan 20th 2018
  4. #License GPLv3 https://www.gnu.org/licenses/gpl-3.0.txt
  5.  
  6. goback="1day"
  7. ytdir="$HOME/.ytsubs"
  8. subs="$ytdir/ytsubs.lst"
  9. current="$ytdir/current.lst"
  10. viewed="$ytdir/viewed.lst"
  11. tmp="/tmp/ytsubs.lst"
  12. tmp2="/tmp/ytsubs.tmp"
  13.  
  14. format="22"
  15. func=$1
  16. length=0
  17. videos=()
  18.  
  19. #text Editor
  20. editor="vim"
  21.  
  22. #max wait time while pulling video list
  23. wait=30
  24.  
  25. function main(){
  26.   checkFiles
  27.   if [ "$func" == "get" ]
  28.   then
  29.     getCurrent
  30.   elif [ "$func" == "all" ]
  31.   then
  32.     all=true
  33.     showList
  34.   elif [ "$func" == "list" ]
  35.   then
  36.     $editor $subs
  37.   elif [ "$func" == "help" ]
  38.   then
  39.     help
  40.   else
  41.     showList
  42.   fi
  43. }
  44.  
  45. function help(){
  46.   echo -e "\e[7m Options: \e[0m"
  47.   echo -e "\e[32m$0 get \e[37m#updates video list"
  48.   echo -e "\e[32m$0 list \e[37m#edit list of subscriptions"
  49.   echo -e ""
  50.   echo -e "\e[7m Usefull: \e[0m"
  51.   echo -e "Add $0 to your conjobs"
  52.  
  53.   echo ""
  54.   controls
  55. }
  56.  
  57. function checkFiles(){
  58.   if [ ! -d $ytdir ]
  59.   then
  60.     mkdir -p $ytdir
  61.   fi
  62.  
  63.   if [ ! -f $subs ]
  64.   then
  65.     echo "metalx1000" > $subs
  66.     echo "BryanLunduke" >> $subs
  67.   fi
  68. }
  69.  
  70. function getCurrent(){
  71.   #remove old list
  72.   rm "$tmp"
  73.   echo "Saving to $current"
  74.   cat $subs|sort -u|while read sub;do
  75.   echo -n "Getting ${sub}'s Video List..."
  76.   timeout $wait youtube-dl -ciwgef $format --dateafter=now-$goback ytuser:$sub 2>/dev/null|\
  77.     while read line
  78.     do
  79.       if [[ $line != *"http"* ]];
  80.       then
  81.         echo "$sub - $line"|sed 's/&/and/g;s/\[//g;s/\]//g;' >> "$tmp"
  82.       else
  83.         echo "$line" >> "$tmp"
  84.       fi
  85.     done
  86.     echo ""
  87.   done
  88.  
  89.   mv "$tmp" "$current"
  90.   echo "$current updated."
  91. }
  92.  
  93. function playVideo(){
  94.   echo "Playing $1..."
  95.   echo "$1" >> $viewed
  96.   url="$(grep "$1" -A 1 $current|tail -n 1)"
  97.   echo "$url"
  98.   mpv "$url"
  99. }
  100.  
  101. function controls(){
  102.   echo -e "\e[1m\e[4mKeys: w=up;s=down;e=select;q=quit\e[0m"
  103. }
  104.  
  105. function removeViewed(){
  106.   cp $current $tmp2
  107.   cat $viewed|sort -u|grep -v '^http'|while read l;
  108. do
  109.   sed -i "/$l/d" $tmp2
  110. done
  111. grep -v '^http' $tmp2|sort -u
  112. }
  113.  
  114.  
  115. function showList(){
  116.   if [ $all ]
  117.   then
  118.     IFS=$'\r\n' GLOBIGNORE='*' command eval  'videos=($(grep -v "^http" $current))'
  119.   else
  120.     removeViewed
  121.     IFS=$'\r\n' GLOBIGNORE='*' command eval  'videos=($(removeViewed))'
  122.   fi
  123.   length="${#videos[@]}"
  124.   let x=0;
  125.   while [ 1 ]
  126.   do
  127.     clear
  128.     controls
  129.     for (( i=0; i<$length;i++ ));
  130.     do
  131.       if [ "$i" = "$x" ]
  132.       then
  133.         echo -e "\e[7m${videos[$i]}\e[0m"
  134.       else
  135.         echo "${videos[$i]}"
  136.       fi
  137.     done
  138.  
  139.     read -rsn1 k
  140.  
  141.     case $k in
  142.       "s") let x++;;
  143.       "w") let x--;;
  144.       "q") echo "Goodbye...";exit 0;;
  145.       "e") playVideo "${videos[$x]}";;
  146.     esac
  147.  
  148.     if [ $x -lt 0 ]
  149.     then
  150.       let x=0;
  151.     elif [ $x -ge $length ]
  152.     then
  153.       let x=$length-1;
  154.     fi
  155.  
  156.   done
  157. }
  158.  
  159. main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement