vp

git clone git://oldgit.suckless.org/vp/
Log | Files | Refs | README

commit 808dc3969b471e8c734bdc307c9efe6df7e21a6a
parent 81c2542733c28ec72ebe1498c0ac3997dcd537ed
Author: Kris Maglione <kris@suckless.org>
Date:   Sun, 15 Aug 2010 11:00:11 -0400

Ditch the JS dependency.

Diffstat:
.hgignore | 2+-
README | 1-
vp-base | 40++++++++++++++++++++++++++++++++++++++++
vp-youtube | 55+++++++++++++++++++++----------------------------------
4 files changed, 62 insertions(+), 36 deletions(-)

diff --git a/.hgignore b/.hgignore @@ -1,4 +1,4 @@ config.local.mk -test.html +test*.html vp-patterns .*.sw? diff --git a/README b/README @@ -20,7 +20,6 @@ YouTube and enable access to age-restricted videos. Requirements: 9base or plan9port curl (in the default configuration) - spidermonkey or kjs (YouTube only) To install, edit config.mk or config.local.mk and run, diff --git a/vp-base b/vp-base @@ -74,6 +74,46 @@ if(~ $vp_base_loaded '') { vp_result = $* } + vp_urldec = ' + BEGIN { + hextab["0"] = 0; hextab["6"] = 6; hextab["c"] = 12; + hextab["1"] = 1; hextab["7"] = 7; hextab["d"] = 13; + hextab["2"] = 2; hextab["8"] = 8; hextab["e"] = 14; + hextab["3"] = 3; hextab["9"] = 9; hextab["f"] = 15; + hextab["4"] = 4; hextab["a"] = 10; + hextab["5"] = 5; hextab["b"] = 11; + } + function urldec(str, c, i, res) { + i = 1 + res = "" + for (i=1; i <= length(str); i++) { + c = substr(str, i, 1); + if(c == "%") { + c1 = hextab[tolower(substr(str, i+1, 1))] + c2 = hextab[tolower(substr(str, i+2, 1))] + if(c1 != "" && c2 != "") { + code = c1 * 16 + c2 + c = sprintf("%c", code) + i += 2 + } + } else if(c == "+") + c = " " + if(c != "\r") + res = res c + } + return res + } + function urlparse(a, str, sep1, sep2, i, n, items, kv) { + if(sep1 == "") sep1 = "&" + if(sep2 == "") sep2 = "=" + + n = split(str, items, sep1) + for(i=1; i <= n; i++) { + split(items[i], kv, sep2) + a[urldec(kv[1])] = urldec(kv[2]) + } + }' + config=$XDG_CONFIG_HOME ^ /vp.conf test -f $config && . $config } diff --git a/vp-youtube b/vp-youtube @@ -17,43 +17,30 @@ # 43: 480x360 webm (VP8 ) / Vorbis "Medium" # 45: 1280x720 webm (VP8 ) / Vorbis "HD720" -vp_youtube_formats = '22, 45, 18, 35, 34, 5' +vp_youtube_formats = (22 45 18 35 34 5) vp_need_cookies = 1 . vp-base $0 $* -vp_getdata data `{echo $"* | sed 's/#(!|%21)v=/?v=/'} - -swfargs=`{ { echo ""; sed <$data -n 's/.*flashvars=\\"(([^\\]|\\[^"])+).*/"\1"/p' } | tail -1 } -formats=`{ sed <$data -n 's/.*setAvailableFormat\((.*)\);.*/[\1],/p' } +vp_getdata data `{echo $* | sed 's/#(!|%21)v=/?v=/'} + +swfargs=`{ { echo ""; sed <$data -n 's/.*flashvars=\\"(([^\\]|\\[^"])+).*/\1/p' } | tail -1 } + +fmt_array = ($vp_format $vp_youtube_formats) +fmt_array = 'fmt[' ^ `{seq 1 $#fmt_array} ^ '] = ' ^ $fmt_array ^ ';' + +vp_result `{sed <$data -n 's/.*setAvailableFormat\("(.*)"\);.*/\1/p' | awk -F '", "' $vp_urldec ^ ' + { fmt_url_map[$4] = $1 } + END { + urlparse(swfargs, ENVIRON["swfargs"]) + urlparse(fmt_url_map, swfargs["fmt_url_map"], ",", "|") + '$"fmt_array' + for(i=1; i <= length(fmt); i++) + if(fmt[i] in fmt_url_map) { + print fmt_url_map[fmt[i]] + exit 0 + } + }' +} rm $data -vp_result `{ cat <<! | js } - function safe(fn) { - return function () { try { return fn.apply(null, arguments) } catch(e) { return null } } - } - function dict(ary) { - var obj = {}; - ary.forEach(function (item) { obj[item[0]] = item[1]; }); - return obj; - } - function params(s, l1, l2) { - return dict(s.split(l1 || "&").map( - function(s) { return s.split(l2 || "=", 2).map(safe(decodeURIComponent))} )); - } - - var swfargs = params($swfargs); - var fmt_url_map = params(swfargs.fmt_url_map || "", ",", "|"); - [$formats].forEach(function (a) { - fmt_url_map[a[3]] = a[0]; - }); - - var fmts=[$vp_format].concat([$vp_youtube_formats]); - fmts.forEach(function (f) { - if(f in fmt_url_map) { - print(fmt_url_map[f]); - quit(); - } - }); -! -