#!/bin/bash
#                       /usr/local/bin/qyou
# https://crystalfaeries.net/posix/bin/qyou
# celeste:crystalfaery QYOU 2021-04-19 07:16:01+00:00
# this is the command-line user control and status monitoring program for the daemon youtubedownload
# if invoked with ususal version (-v|--version) or help (-h|--help) arguments, deliver info		and quit
# if invoked with argument -U,		update youtube-dl software (DebIan updates via .deb packages)	and quit
# if invoked with argument -p,		display any running youtube download processes			and quit
# if invoked with argument -l,		display the tail	 	of the youtubedownload log	and quit
# if invoked with argument -l n,	display the tail n lines	of the youtubedownload log	and quit
# if invoked with argument -F, Follow	the tail			of the youtubedownload log
# if invoked with argument -i, interactively receive  URLs to add to	the queue
# if invoked with argument -e, interactively edit the URLs	in	the queue
# if invoked with argument -k, kill	the youtubedownload daemon					and quit
# if invoked with argument -s, start	the youtubedownload daemon					and quit
# if invoked with argument -m		update metadata webpages    from archive and queue		and quit
# if invoked with argument -r		remove URL/youtube_hashcode from our records			and quit
#					(mostly relevant to censored/deleted videos)
# if invoked with argument -w		watch videos listed in  youtubedownload queue without downloading & quit
#			   -w URL [URL] watch/hear URLs listed on command-line after the -w option	and quit
# if invoked sans arguments, display   the       existing	youtubedownload queue			and quit
# if invoked with any other arguments, add those URLs  to  the	youtubedownload queue			and quit
# NOTE: we have configured our rsnapshotlimit daemon,  upon finding free disk space, to call "qyou -s",  which
#    if youtubedownload daemon is not already running, will start it running, thus full auto youtube downloads.
let help=24	# this line number -1
QUE="${HOME}"/downloads/youtube.com/.youtube.txt
			   TMP=/tmp/.youtube.txt
cd -P "${HOME}" # there's no place like Aum

case	$# in
0)	# if invoked without arguments, display the existing youtubedownload queue
#	echo "youtubedownload queue:"				>& 2
	sort -u "${QUE}"
	;;
*)
	# if invoked with ususal version or help arguments, deliver that info	and quit
	case "${1}" in
	-h)		head -n ${help} $0
			exit
			;;
	--help)		head -n ${help} $0
			exit
			;;
	-v)		head -n 4 $0 | tail -n 1
			exit
			;;
	--version)	head -n 4 $0 | tail -n 1
			exit
			;;
	-U | --update)	youtube-dl -U	# disabled in DebIan Linux as handled by apt-get via package.deb
			exit	$?
			;;
	-p)		ps -ef --forest | grep "/usr/local/bin/qyou"			| grep -v grep | grep -v "/bin/bash /usr/local/bin/qyou -p"
			ps -ef --forest | grep "/usr/local/bin/youtubedownload"		| grep -v grep
			ps -ef --forest | grep "python /usr/local/bin/youtube-dl"	| grep -v grep
			;;
	-l)		shift	# consume the -l and check for a line count for tail:
			if [ $# -ne 0 ]
			then
				tail -n ${1}	"${HOME}"/downloads/youtube.com/.youlog.txt
			else
				tail		"${HOME}"/downloads/youtube.com/.youlog.txt
			fi
			;;
	-F)		tail -F	"${HOME}"/downloads/youtube.com/.youlog.txt
			;;
	-w)		# watch videos listed in our QUE, OR, the videos on the command line
			shift	# discard the -w leaving any arguments
		if	[ "${1}" != "" ]
		then
			for URL in "$@"
			  do
				vlc -f "${URL}"
				echo -n "delete ${URL}?: "
				read -p "Delete URL from queue? (abort = quit, y = yes = DELETE, nov = download AUDIOnly, now = DOWNLOAD, else KEEP): "
				case "${REPLY}" in
				abort | quit )	echo "ABORTing watching of command line URLs"
					exit	0
					;;
				yes | y ) qyou -r "${URL}"	# delete from our records
					;;
				nov | audio)	echo "Downloading "${URL}" AUDIO."	
					pushd	${HOME}/downloads/youtube.com/ &&	\
nohup					youtube-dl -i -k -x --audio-format flac	"${URL}" < /dev/null >> "${HOME}"/crystalfaeries.net/documents/youtube.com/.youlog.txt 2>&1 & disown %1 # in background
					popd 2> /dev/null
					grep -v	       "${URL}"	"${QUE}"	\
					>  /tmp/.$$.youtube.txt
				       	mv /tmp/.$$.youtube.txt	"${QUE}"
					;;
				now )	echo "Downloading	"${URL}" NOW."	
					pushd	${HOME}/downloads/youtube.com/ &&	\
nohup					youtube-dl -i -k	"${URL}" < /dev/null >> "${HOME}"/crystalfaeries.net/documents/youtube.com/.youlog.txt 2>&1 & disown %1 # in background
					popd 2> /dev/null
					grep -v	       "${URL}"	"${QUE}"	\
					>  /tmp/.$$.youtube.txt
				       	mv /tmp/.$$.youtube.txt	"${QUE}"
					;;
				* )	qyou "${URL}"		# add to queue
					;;
				esac
			  done
		else	#No URLs specified on command line, so use our queue
		    for URL in	$( qyou | sort -u )	
		    do
			if [ "${URL}" = "" ]
				then break
			fi
						   HASH="$(echo ${URL} | sed 's/^.*v=//;s/^-//')"
			vid_title="$(grep	"${HASH}"	"${HOME}"/downloads/youtube.com/.youtitles.txt | cut -f2-)"	|| \
			vid_title="$(youtube-dl --get-title	https://www.youtube.com/watch?v=${HASH})"	|| exit $?	&& \
			echo	"${HASH}	${vid_title}"			>  /tmp/.$$.youtube.txt
			cat	"${HOME}"/downloads/youtube.com/.youtitles.txt	>> /tmp/.$$.youtube.txt
			mv							   /tmp/.$$.youtube.txt	\
				"${HOME}"/downloads/youtube.com/.youtitles.txt
			if	[      "${vid_title}" == "" ]
			then	# make the URL visible
				echo	"WATCHING:	https://www.youtube.com/watch?v=${HASH}"
			else	# make the Title visible
				echo	"WATCHING:	${vid_title}"		
#				firefox	"${HOME}"/crystalfaeries.net/downloads/i.ytimg.com/vi/"${HASH}"/*.jpg
			fi
			vlc -f	"${URL}"					   2>	/dev/null
#			echo -n	"WATCHED:	"			
			grep	"${HASH}" "${HOME}"/crystalfaeries.net/downloads/youtube.com/.youtitles.txt	2>/dev/null \
			||	echo	"https://www.youtube.com/watch?v=${HASH}"
			read -p "Delete URL from queue? (abort = quit, y = yes = DELETE, nov = download AUDIOnly, now = DOWNLOAD, else KEEP): "
			case "${REPLY}" in
			abort | quit )	echo "ABORTing watching of queued URLs"
				exit	0
				;;
			nov | audio)	echo "Downloading "${URL}" AUDIO."	
				pushd	${HOME}/downloads/youtube.com/ &&	\
nohup				youtube-dl -i -k -x --audio-format flac	"${URL}" < /dev/null >> "${HOME}"/crystalfaeries.net/documents/youtube.com/.youlog.txt 2>&1 & disown %1 # in background
				popd 2> /dev/null
				grep -v	       "${URL}"	"${QUE}"	\
				>  /tmp/.$$.youtube.txt
			       	mv /tmp/.$$.youtube.txt	"${QUE}"
				;;
			now )	echo "Downloading "${URL}" NOW."	
				pushd	${HOME}/downloads/youtube.com/ &&	\
nohup				youtube-dl -i -k	"${URL}" < /dev/null >> "${HOME}"/crystalfaeries.net/documents/youtube.com/.youlog.txt 2>&1 & disown %1 # in background
				popd 2> /dev/null
				grep -v	       "${URL}"	"${QUE}"	\
				>  /tmp/.$$.youtube.txt
			       	mv /tmp/.$$.youtube.txt	"${QUE}"
				;;
			yes | y) echo "Deleting "${URL}" from queue."	
				grep -v		"${URL}" "${QUE}"	\
				>  /tmp/.$$.youtube.txt
			       	mv /tmp/.$$.youtube.txt	"${QUE}"
				;;
			* )	echo "Keeping: "${URL}"  in  queue."	
				exec qyou -w "${@}"	# no return
				alert "$0: return from exec ${@}"
				;;
			esac
		    done
		exit	$?
		fi
		exit	$?
		;;
	-e)	# if invoked with argument -e, interactively edit the URLs	in	the queue
			sort -u		   "${QUE}"	\
			>	/tmp/.$$.youtube.txt
			vi	/tmp/.$$.youtube.txt # daemons may alter original files while we edit a temporary copy then clobber originals
						     # try not to lose any items from the queue file
			sort -u	/tmp/.$$.youtube.txt					\
			>		   "${QUE}"
			exit	$?
		;;
	-i)	# if invoked with argument -i, interactively receive URLs to add to the queue
		shift	# toss the -i and pass any URLs
			yourl ${@}"	>> "${QUE}"	# append new URL(s) from command-line
		while read URL
		do					# append new URL(s) from standard input until ^D (EOF)
			yourl "${URL}"	>> "${QUE}"	# append new URL
		done
		exit	$?
		;;
	-k)	# kill youtubedownload
		# notice the first grep string MUST match the initial invocation in user's crontab and here:
		PID=$(ps -ef --forest | grep "/usr/local/bin/youtubedownload" | grep -v grep | sed "s/^$(whoami) *//;s/ .*$//")
		echo "killing ${PID}"
		for signal in HUP TERM KILL;do
			kill -"${signal}" ${PID}	>> "${HOME}"/downloads/youtube.com/.youlog.txt	2> /dev/null
			sleep 5
		done
		exit	$?
		;;
	-s)	# start the youtubedownload daemon ONLY IF IT IS NOT ALREADY RUNNING
		# notice the first grep string MUST match the initial invocation in user's crontab and here:
		PID=$(ps -ef --forest | grep "/usr/local/bin/youtubedownload" | grep -v grep | sed "s/^$(whoami) *//;s/ .*$//")
		if [ "${PID}" == "" ]
		then
			:	# youtubedownload is already running
		else
			nohup /usr/local/bin/youtubedownload >> "${HOME}"/downloads/youtube.com/.youlog.txt 2>&1 &
		fi
		exit	$?
		;;
	-m)	# update the metadata webpages from archive and queue and quit
		META="${HOME}"/crystalfaeries.net/video/videos.html
		rsync -auvH "${META}" "${META}~"	# backup the metapage
		echo '<html><head><title>Videos</title></head><body><h1 align="center">Videos</h1>'	>	"${META}"
		echo "<ul>"										>>	"${META}"
		grep '^youtube '	"${HOME}"/downloads/youtube.com/.archive.txt | sed 's/^youtube //'	>	/tmp/$$.txt
		grep 'https://www.youtube.com/watch?v='	"${QUE}"	\
		|	sed 's/^https:\/\/www.youtube.com\/watch?v=//'					>>	/tmp/$$.txt
		grep 'http://www.youtube.com/watch?v='	"${QUE}"	\
		|	sed 's/^http:\/\/www.youtube.com\/watch?v=//'					>>	/tmp/$$.txt
		cut -f2- "${HOME}"/crystalfaeries.net/links.txt						\
		|	grep 'https://www.youtube.com/watch?v='						\
		|	sed 's/^https:\/\/www.youtube.com\/watch?v=//'					>>	/tmp/$$.txt
		cut -f2- "${HOME}"/crystalfaeries.net/links.txt						\
		|	grep 'http://www.youtube.com/watch?v='						\
		|	sed 's/^http:\/\/www.youtube.com\/watch?v=//'					>>	/tmp/$$.txt
		for HASH in								$(sort -u		/tmp/$$.txt)
		do # LOOP through hashes collecting matching titles
			vid_title="$(    youtube-dl	--get-title	https://www.youtube.com/watch?v=${HASH})"
		if	[ "${vid_title}" == "" ]
		then
		#	echo -n "<li>UNAVAILABLE: https://www.youtube.com/watch?v=${HASH}</li>" >> "${META}"
			echo -n "<p> UNAVAILABLE: https://www.youtube.com/watch?v=${HASH} </p>" >  "${HOME}/downloads/youtube.com/${vid_title}.html"
			grep -v "${HASH}"	${HOME}/downloads/youtube.com/.archive.txt		\
			>	/tmp/.$$.txt
			mv	/tmp/.$$.txt	${HOME}/downloads/youtube.com/.archive.txt	# delete our referring link
			grep -v "${HASH}"	"${QUE}"	\
			>	/tmp/.$$.txt
			mv	/tmp/.$$.txt	"${QUE}"	# delete our referring link
			grep -v "${HASH}"	/home/celeste/crystalfaeries.net/links.txt		\
			>	/tmp/.$$.txt
			mv	/tmp/.$$.txt	/home/celeste/crystalfaeries.net/links.txt	# delete our referring link
			grep -H "${HASH}"	/home/celeste/crystalfaeries.net/src/*.txt		\
			>>			/home/celeste/crystalfaeries.net/src/.unavailable.txt	# todo: clear from articles
		else	# build list of video hashes+titles
			echo	"${HASH}	${vid_title}" >>	${HOME}/downloads/youtube.com/.youtitles.txt
		fi
		done;	rm	/tmp/$$.txt	# clean-up
#		sort -u -k2 <						${HOME}/downloads/youtube.com/.youtitles.txt	\
#		|	while read HASH vid_title #	list of video hashes+titles
#	    do	# LOOP through alphabetized	list of video hashes+titles
#			    thumbnail_url="$(youtube-dl	--get-thumbnail https://www.youtube.com/watch?v=${HASH})"
#		download "${thumbnail_url}" < /dev/null >& /dev/null & disown %1 >& /dev/null	# fetch the thumbnail IN BACKGROUND
#		echo "qyou vid_title=${vid_title}"	1>&2	# debug
#		echo -n '<li>'							>> "${META}"
#		echo -n '<li>'							>  "${HOME}"/downloads/youtube.com/"${vid_title}".html"
#		echo     "${vid_title}"						>> "${META}"	# video title
#		echo     "${vid_title}"						>> "${HOME}"/downloads/youtube.com/"${vid_title}".html"
#		echo -n '<br><a href="https://www.youtube.com/watch?v='		>> "${META}"
#		echo -n '<br><a href="https://www.youtube.com/watch?v='		>> "${HOME}"/downloads/youtube.com/"${vid_title}".html"
#		echo -n "${HASH}"						>> "${META}"
#		echo -n "${HASH}"						>> "${HOME}"/downloads/youtube.com/"${vid_title}".html"
#		echo -n '"><img width="320" height="240" align="right"	src="'	>> "${META}"
#		echo -n '"><img						src="'	>> "${HOME}"/downloads/youtube.com/"${vid_title}".html"
#		echo -n  "${thumbnail_url}"					>> "${META}"	# reference thumbnail
#		echo -n  "${vid_title}.jpg"					>> "${HOME}"/downloads/youtube.com/"${vid_title}".html"
#		echo -n '"></a><br><pre>'					>> "${META}"
#		echo -n '"></a><br><pre>'					>> "${HOME}"/downloads/youtube.com/"${vid_title}".html"
#		youtube-dl --get-description --get-format --get-duration			\
#		https://www.youtube.com/watch?v="${HASH}" | fold -s | tee -a	   "${META}"	\
#										>> "${HOME}"/downloads/youtube.com/"${vid_title}".html"
#		echo	"</pre><br clear="all"></li>"				>> "${META}"
#		echo	"</pre><br clear="all"></li>"				>> "${HOME}"/downloads/youtube.com/"${vid_title}".html"
#	    done
		echo "</ul>"								>> "${META}"

		# now add links to any Vimeos we've referenced:
		echo '<br clear="all"><a name="vimeos"><hr align="center" width="50%"></a><h1 align="center">Vimeos</h1><ul>' \
											>> "${META}"
		for f in $(grep vimeo crystalfaeries.net/src/*.txt		\
			| grep -v glossary.txt					\
			| sed 's/^.*http:/https:/;s/^.*https:/https:/;s/".*$//'	\
			| sort -u )
		do
			echo -n '<li><a href="'
			echo -n "${f}"
			echo -n '">'
			echo -n "${f}"
			echo    '</a></li>'
			done								>> "${META}"
		# now complete the page with footer:
		cat	"${HOME}"/crystalfaeries.net/README.html				>> "${META}"
		exit	$?	# pau for now
		;;
	-r)	# remove hashcodes from the youtubedownload queue and records
		shift	# consume the -r then remove hashcodes:
		while [ $# -gt 0 ]
		do
		    extract=$(	  echo "${1}"	    | sed 's/\&list=.*$//;s/^.*=//;s/^.*\///')	# just the hashcode
		    if [ 12 -ne $(echo "${extract}" | /usr/local/bin/wc | sed 's/^.* //') ]
		    then	# we have to fix our code here :-(
			echo	"$0 -r trying to remove video:	${1}"		1>&2
			echo	"we extracted AS hashcode:	${extract}"	1>&2
			exit	1	# Yes, we expect to fail gloriously on non-YouTube URLs :-( always room for more code :-)
		    else	# we successfully extracted the youtube hashcode
			shift	# therefore we may consume the argument
			for oldfile in	${HOME}/downloads/youtube.com/.{archive,hash,playou,youlog,youtitles,youtube}.txt	\
					"${HOME}"/crystalfaeries.net/links.txt "${HOME}"/crystalfaeries.net/recently_linked.html
			do
				grep -v $(echo "${extract}" | sed 's/^-//')	\
						"${oldfile}" >	/tmp/$$.txt
				mv				/tmp/$$.txt	\
						"${oldfile}"
			done
			grep -Hi $(echo "${extract}" | sed 's/^-//')	"${HOME}"/crystalfaeries.net/{src/,}*.{txt,html}	\
			>>		"${HOME}"/crystalfaeries.net/src/.qyou-r."${extract}".txt	# todo: remove references
		    fi
		done
		;;
	*)	# if invoked with any other arguments, add those URLs to the youtubedownload queue
		while [ $# -gt 0 ]
		do
		    yourl	"${URL}" >> "${QUE}"	# append new URL
		    shift	# consume the argument
		done
		;;
	esac
	;;
esac
exit	$?	################################################################
#######	use pieces of this code to improve the -w experience, like
-i on playlists, show available formats for manual choice on any operation
#!/usr/bin/env bash
# https://gist.github.com/Strayer/7989654/raw/dd24cf84ede5daa66459dbffe027129994da4ef4/youtube-dl-dash.bash
#                       /usr/local/bin/youtube-dl-dash
#  http://crystalfaeries.net/posix/bin/youtube-dl-dash
# celeste:crystalfaery 2016-02-05 03:16:07+00:00
set -e

YOUTUBE_FORMATS=$(youtube-dl -F "$1")

if [[ "$YOUTUBE_FORMATS" == *"(DASH Video)"* ]]; then
    VIDEO_NAME=$(youtube-dl --get-filename "$1")
    VIDEO_NAME_TMP="$VIDEO_NAME.tmp"

    echo "Filename: $VIDEO_NAME"

    if [ -e "$VIDEO_NAME" ]; then
        echo "File already exists, aborting..."
        exit $?
    fi

    # Strip dashes from the beginning of the ID to avoid command line errors
    VIDEO_ID=`youtube-dl --get-id "$1" | sed "s/^-/_/"`

    VIDEO_FORMAT=`echo "$YOUTUBE_FORMATS" | fgrep "(DASH Video)" | head -n 1`
    VIDEO_FORMAT_ID=`echo "$VIDEO_FORMAT" | cut -f 1`
    VIDEO_FORMAT_NAME=`echo "$VIDEO_FORMAT" | cut -f 4,5`

    AUDIO_FORMAT=`echo "$YOUTUBE_FORMATS" | fgrep "(DASH Audio)" | head -n 1`
    AUDIO_FORMAT_ID=`echo "$AUDIO_FORMAT" | cut -f 1`
    AUDIO_FORMAT_NAME=`echo "$AUDIO_FORMAT" | cut -f 4,5`

    echo "Using formats: $VIDEO_FORMAT_NAME, $AUDIO_FORMAT_NAME"

    youtube-dl -i -f $VIDEO_FORMAT_ID -o "${VIDEO_ID}.vid" "$1"
    youtube-dl -i -f $AUDIO_FORMAT_ID -o "${VIDEO_ID}.aud" "$1"

    echo -n "Combining files..."
    
    ffmpeg -i "$VIDEO_ID.vid" -i "$VIDEO_ID.aud" -c copy -f mp4 -v warning \
        "$VIDEO_NAME_TMP"
#   rm "$VIDEO_ID".*
    mv "$VIDEO_NAME_TMP" "$VIDEO_NAME"
    
    echo " done!"
else
    youtube-dl -i "$1"
fi
exit $?
