From 626c5d35d6fa5bee8bf50c49584dfbe4db851868 Mon Sep 17 00:00:00 2001 From: Tom Vincent Date: Fri, 27 Aug 2010 20:29:31 +0800 Subject: [PATCH] General code clean up. See full summary below: * Parse a user config file in \$HOME/.config/ytmp/ytmp.conf (or fallback) * Changed function syntax to "C" style (also other code style changes) * Quoted variables where necessary * Added new variable (DWN) to specify download folder (or fallback to home) * Modularised zenity and kdialog GUI's (note: _kdialog is untested) --- ytmpc | 91 ++++++++++++++++++++++++++++++----------------------------- 1 file changed, 47 insertions(+), 44 deletions(-) mode change 100644 => 100755 ytmpc diff --git a/ytmpc b/ytmpc old mode 100644 new mode 100755 index 712d092..61a4740 --- a/ytmpc +++ b/ytmpc @@ -1,59 +1,62 @@ #!/bin/bash -source /etc/ytmp.conf +CONFIG="${XDG_CONFIG_HOME:-"$HOME/.config"}/ytmp/" +if [[ -f "$CONFIG/ytmp.conf" ]]; then + . "$CONFIG/ytmp.conf" +else + . /etc/ytmp.conf +fi -function ytplay { - mplayer -cache 2048 $VIDURL & +ytplay() { + mplayer -cache 2048 "$vidurl" & } -function ytdownload { - youtube-dl --max-quality=$MAXQUAL -o "$TITLE".%\(ext\)s $URL & +ytdownload() { + youtube-dl --max-quality="$MAXQUAL" -o "${DWN:-$HOME}/$title".%\(ext\)s "$url" & } -function ytdlandplay { -#mplayer might crash if download speed < bitrate +ytdlandplay() { ytdownload - while ! [ -a $TITLE* ] ; do sleep 3; done - mplayer $TITLE* & + while ! [[ -a $title* ]]; do sleep 3; done + mplayer $title* & } -LASTURL="" -while(true) -do - if [ -f /tmp/ytmdurls ] - then - URL=$(tail -n 1 /tmp/ytmdurls) - if [ "$URL" != "$LASTURL" ] - then - TITLE=$(youtube-dl -e $URL | sed -e 's/ /_/g') - VIDURL=$(youtube-dl --max-quality=$MAXQUAL -g $URL) - case $GUI in - "none") - ytplay ;; - "zenity") - case "$(zenity --list --checklist --text=$TITLE --hide-header --column "1" --column "2" TRUE "View in mplayer" FALSE "Download")" in - "View in mplayer") - ytplay ;; - "Download") - ytdownload ;; - "View in mplayer|Download") - ytdlandplay ;; - esac ;; - "kdialog") - INPUT="$(kdialog --checklist $TITLE 1 "View with mplayer (default)" on 2 "Download" off)" - if ! [ -z "${INPUT}" ]; then - if [ $INPUT = "\"1\"" ]; then ytplay; - else - if [ $INPUT = "\"2\"" ]; then ytdownload; - else - ytdlandplay - fi - fi - fi +_zenity() { + choice=$(zenity --list --checklist --text="$title" --hide-header --column "1" \ + --column "2" TRUE "View in mplayer" FALSE "Download") + + case "$choice" in + "View in mplayer") ytplay;; + "Download") ytdownload;; + "View in mplayer|Download") ytdlandplay;; + esac +} + +_kdialog() { + choice="$(kdialog --checklist "$title" 1 "View with mplayer (default)" on \ + 2 "Download" off)" + + case "$choice" in + "1") ytplay;; + "2") ytdownload;; + "1|2") ytdlandplay;; + esac +} + +lasturl="" +while(true); do + if [[ -f /tmp/ytmdurls ]]; then + url="$(tail -n 1 /tmp/ytmdurls)" + if [[ "$url" != "$lasturl" ]]; then + title="$(youtube-dl -e "$url")" + vidurl="$(youtube-dl --max-quality="$MAXQUAL" -g "$url")" + case "$GUI" in + "none") ytplay;; + "zenity") _zenity;; + "kdialog") _kdialog;; esac - #kill `ps aux | grep youtube-dl | grep -v grep | awk '{print $2}'` fi - LASTURL=$URL + lasturl="$url" fi sleep 1 done