/
ytmpc
executable file
·128 lines (109 loc) · 3.17 KB
/
ytmpc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/bash
. /usr/lib/tlvince/logging.sh
. /usr/lib/tlvince/getConfig.sh
CONFPATH="$(getConfig ytmp)"
. "$CONFPATH"
VERBOSE=true
isSet() {
[[ -z ${!1} ]] && die "$1 is not set in $CONFPATH"
}
isSet "GUI"
isSet "PLAYER"
pidof /usr/bin/ngrep > /dev/null || die "is ytmpd running?"
ytplay() {
info "Now playing: ($host) $title"
$PLAYER "$vidurl" > /dev/null 2>&1
}
ytdownload() {
info "Now downloading: ($host) $title"
case $host in
"youtube") youtube-dl -o "${DWN:-$HOME}/%(stitle)s.%(ext)s" "$id";;
"vimeo") wget -O "${DWN:-$HOME}/$title.flv" "$vidurl";;
esac
}
ytdlandplay() {
case $host in
"youtube") local log="/tmp/youtube-dl.log"
ytdownload | tee "$log" &
vidurl="$(grep -F Destination "$log" | awk '{print $3}')";;
"vimeo") ytdownload &
vidurl="${DWN:-$HOME}/$title.flv"
esac
sleep 3
while ! [ -f "$vidurl" ] ; do sleep 3; done
ytplay &
}
##
# A generic framework for GUI's that provide a choice from multiple selections.
#
# param $1 A choice string.
# param $2 A "play" string. Default: "View in mplayer".
# param $3 A "download" string. Default: "Download".
#
playText="View in mplayer"
dwnText="Download"
parseGUIChoice()
{
[[ $2 ]] || set "$1" "$playText"
[[ $3 ]] || set "$1" "$2" "$dwnText"
case "$1" in
"$2") ytplay &;;
"$3") ytdownload &;;
"$2|$3") ytdlandplay &;;
esac
}
_zenity() {
title="${title//&/&}" # zenity requires ampersand characters to be escaped
choice=$(zenity --list --checklist --text="$title" --hide-header \
--column="1" --column="2" TRUE "$playText" FALSE "$dwnText")
parseGUIChoice "$choice"
}
_kdialog() {
choice=$(kdialog --checklist "$title" 1 "$playText" on 2 "$dwnText" off)
parseGUIChoice "$choice" '"1" ' '"2" '
}
_dmenu() {
choice=$(echo -e "$playText\n$dwnText" | dmenu -i)
parseGUIChoice "$choice"
}
##
# Vimeo support.
#
# Based on video-dl by Vitaly Minko <http://ossguy.com/?p=172>
#
parseVimeo() {
local video_xml signature signature_expires
video_xml=$(wget -q -O - "http://www.vimeo.com/moogaloop/load/clip:$id")
signature=$(echo $video_xml | sed 's/^.*<request_signature>\([^<]*\)<.*$/\1/g')
signature_expires=$(echo $video_xml | \
sed -e 's/^.*<request_signature_expires>\([^<]*\)<.*$/\1/g')
title=$(echo $video_xml | sed 's/^.*locked> <video> <caption>\([^<]*\)<.*$/\1/')
vidurl="http://www.vimeo.com/moogaloop/play/clip:$id/$signature/$signature_expires/?q=sd"
}
lastid=""
$VERBOSE && info "Waiting for download links..."
while(true); do
if [[ -f /tmp/ytmpurls ]]; then
tail -n 2 /tmp/ytmpurls > /tmp/ytmpvars
. /tmp/ytmpvars
if [[ "$id" != "$lastid" ]]; then
case "$host" in
"youtube")
$VERBOSE && info "(youtube) video found. Getting title..."
title="$(youtube-dl -e -- "$id")"
$VERBOSE && info "(youtube) Getting video URL..."
vidurl="$(youtube-dl --max-quality="$YTQUAL" -g -- "$id")"
;;
"vimeo") parseVimeo;;
esac
case "$GUI" in
"none") ytplay &;;
"zenity") _zenity;;
"kdialog") _kdialog;;
"dmenu") _dmenu;;
esac
fi
lastid="$id"
fi
sleep 1
done