vp

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

vp-base (3745B)


      1 
      2 vp_script=`{basename $1}; shift
      3 
      4 if(~ $vp_base_loaded '') {
      5 	vp_base_loaded=1
      6 
      7 	. 9.rc
      8 	fn vp_default { if (~ $$1 '') $1 = $2 }
      9 
     10 	vp_default XDG_CONFIG_HOME $home ^ /.config
     11 
     12 	args=$*
     13 	vp_cookie_jar=$XDG_CONFIG_HOME ^ /cookies.txt
     14 	vp_temppat=/tmp/vp.html.XXXXXX
     15 	vp_user_agent='Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2a1pre) Gecko/20081203 Firefox/3.2a1pre'
     16 	vp_vidtemp=$home ^ /vp-out.mpg
     17 
     18 	vp_savevideo=()
     19 
     20 	fn vp_checkmedia { /usr/bin/file -i $* | grep -s '(video|audio)/' }
     21 
     22 	vp_hget_autoname=-OJ
     23 	vp_hget = (curl -L -b $vp_cookie_jar -c $vp_cookie_jar -A $vp_user_agent)
     24 	vp_hget_loud=($vp_hget '-#')
     25 	vp_hget_silent=($vp_hget -s)
     26 
     27 	vp_rtmpget = (flvstreamer)
     28 	vp_rtmpget_loud = ($vp_rtmpget)
     29 	vp_rtmpget_silent = ($vp_rtmpget)
     30 
     31 	fn vp_autoname { true }
     32 	fn vp_paste { xsel -o || sselp }
     33 
     34 	vp_newline='
     35 '
     36 
     37 	# IMPORTANT: Unset this if you use a player that does
     38 	# not read its cookies from $vp_cookie_jar
     39 	vp_have_player_cookies = 1
     40 	fn vp_player { mplayer -cache 5120 -cache-min 20 -cookies -cookies-file $vp_cookie_jar $* }
     41 
     42 	vp_have_rtmpplayer_cookies = ()
     43 	fn vp_rtmpplayer { vlc `{vp_rtmpurl $*} }
     44 
     45 	fn vp_rtmpurl {
     46 		getflags 's ,r url,t ,y path' $*
     47 		echo $flagr
     48 	}
     49 
     50 	fn js { builtin js || kjs /dev/stdin }
     51 
     52 	# Plan 9's sed doesn't work with long lines
     53 	# fn sed { /bin/sed -r $* || /usr/bin/sed -E $* }
     54 
     55 	fn vp_data {
     56 		if (~ $1 '') cat
     57 		if not $vp_hget_silent $1
     58 	}
     59 	fn vp_getdata {
     60 		data=`{mktemp $vp_temppat}
     61 		$$1 = $data
     62 		vp_data $2 >$data
     63 	}
     64 
     65 	fn getflags {
     66 		ifs=() flagfmt=$1 *=$* { shift; eval `{builtin getflags $*; echo ';argv=$*'} }
     67 	}
     68 
     69 	fn vp_play {
     70 		hget = ()
     71 		if(test -n $"vp_prebuffer -o -n $"vp_need_cookies -a -z $"vp_have_player_cookies)
     72 			vp_savevideo = $vp_vidtemp
     73 		if(test -n $"vp_savevideo) {
     74 			echo >[1=2] vp: Saving to $vp_savevideo
     75 			$vp_hget_silent $1 >$vp_savevideo &
     76 			hget = $apid
     77 			* = $vp_savevideo
     78 			while(! vp_checkmedia $vp_savevideo)
     79 				sleep 1
     80 		}
     81 		if (~ $vp_rtmpresult '')
     82 			vp_player $*
     83 		if not
     84 			vp_rtmpplayer $vp_rtmpresult
     85 		if(test -n $"hget)
     86 			kill $hget
     87 	}
     88 
     89 	fn vp_result {
     90 		if(~ $vp_subscript '')
     91 			echo $*
     92 		if not
     93 			vp_result = $*
     94 	}
     95 	fn vp_rtmpresult {
     96 		vp_rtmpresult = $*
     97 		if(~ $vp_subscript '')
     98 			vp_rtmpurl $vp_rtmpresult
     99 	}
    100 
    101 	vp_urldec = 'hextab["' ^ (0 1 2 3 4 5 6 7 8 9 a b c d e f) ^ '"] = ' ^ `{seq 0 15} ^ ';'
    102 	vp_urldec = '
    103 		BEGIN { '$"vp_urldec' }
    104 		function urldec(str, token, ary, c1, c2, i, n, res) {
    105 			if(!token)
    106 				token = "%"
    107 			gsub(/\+/, " ", str)
    108 			n = split(str, ary, token)
    109 			res = ary[1]
    110 			for(i=2; i <= n; i++) {
    111 				c1 = hextab[tolower(substr(ary[i], 1, 1))]
    112 				c2 = hextab[tolower(substr(ary[i], 2, 1))]
    113 				if(c1 != "" && c2 != "")
    114 					res = res sprintf("%c", c1 * 16 + c2)
    115 				res = res substr(ary[i], 3)
    116 			}
    117 			return res
    118 		}
    119 		function urlparse(str, ary, sep1, sep2, i, n, items, kv) {
    120 			if(sep1 == "") sep1 = "&"
    121 			if(sep2 == "") sep2 = "="
    122 
    123 			n = split(str, items, sep1)
    124 			for(i=1; i <= n; i++) {
    125 				split(items[i], kv, sep2)
    126 				ary[urldec(kv[1])] = urldec(kv[2])
    127 			}
    128 		}'
    129 
    130 	fn vp_awk {
    131 		ifs=() {
    132 			eval `{
    133 				awk $"vp_urldec ^ '
    134 				BEGIN { print ";" >"/dev/fd/3" }
    135 				function quote(val) {
    136 					gsub(/''/, "''''", val)
    137 					return "''" val "''"
    138 				}
    139 				function set(var, val) {
    140 					print var "=" quote(val) >"/dev/fd/3"
    141 				}
    142 				function setary(var, val, ifs) {
    143 					if (ifs == "")
    144 						ifs = " "
    145 					print "ifs=" quote(ifs) "{" \
    146 						var "= `{echo -n " quote(val) "}" \
    147 					      "}" >"/dev/fd/3"
    148 				}
    149 				' ^ $"*  >[3=1] >[1=4]
    150 			# Best not think too much about these
    151 			# redirections. In short:
    152 			#   1 -> stdout
    153 			#   3 -> eval
    154 			} >[4=1]
    155 		}
    156 	}
    157 
    158 	config=$XDG_CONFIG_HOME ^ /vp.conf
    159 }
    160 
    161 test -f $config && . $config
    162 if(! ~ $vp_script '')
    163 	config- ^ $vp_script >[2]/dev/null
    164