URI: 
       reddit.sh - randomcrap - random crap programs of varying quality
  HTML git clone git://git.codemadness.org/randomcrap
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       reddit.sh (1619B)
       ---
            1 #!/bin/sh
            2 # extract .mpd playlist urls (video) for mpv from reddit posts.
            3 
            4 # user-agent is important, tested "Firefox" works, "007" doesn't so there are some basic checks.
            5 ua="Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0"
            6 
            7 arg="$1"
            8 
            9 url="$arg"
           10 # v.redd.it/id -> old.reddit.com/video/id
           11 if printf '%s' "$url" | grep -F -q '/v.redd.it/'; then
           12         url=$(printf '%s' "$url" | sed -E 's@/v\.redd\.it/@old.reddit.com/video/@')
           13 fi
           14 
           15 url=$(printf '%s' "$url" | sed -E 's@www.reddit.com@old.reddit.com@')
           16 
           17 # DEBUG
           18 #printf 'arg=%s, new url=%s\n' "$arg" "$url"
           19 
           20 cookiejar="/tmp/reddit_plumb_cookies.txt"
           21 
           22 # "loid" cookie is important, else reddit blocks.
           23 # getcookie(url)
           24 getcookie() {
           25         curl -s -f -H "User-Agent: $ua" -b "$cookiejar" -c "$cookiejar" "$1" >/dev/null
           26 }
           27 
           28 # getjson(url)
           29 getjson() {
           30         curl -L -s -f -H "User-Agent: $ua" -b "$cookiejar" -c "$cookiejar" "$1"
           31 }
           32 
           33 # parse JSON from stdin.
           34 parsejson() {
           35         jaq '
           36 $1 == "[].data.children[].data.url" && $2 == "s" { data_url = $3; }
           37 $1 ~ /\.dash_url$/ && $2 == "s" { dash_url = $3; }
           38 $1 ~ /\.hls_url$/ && $2 == "s" { hls_url = $3; }
           39 END {
           40         # preferred order, filter them below.
           41         print hls_url;
           42         print dash_url;
           43         print data_url;
           44 
           45 }' | \
           46         LC_ALL=C awk 'length($0) && !x[$0]++'
           47 }
           48 
           49 # get cookie (by HTML page) if cookie do not exist.
           50 if ! test -s "$cookiejar"; then
           51         getcookie "$url"
           52 fi
           53 
           54 tmp=$(mktemp)
           55 jsonurl="${url}.json"
           56 if ! getjson "$jsonurl" > "$tmp"; then
           57         # on failure, retry, cookie might be expired.
           58         getcookie "$url"
           59         # try once
           60         if ! getjson "$jsonurl" > "$tmp"; then
           61                 exit 1
           62         fi
           63 fi
           64 
           65 if test -s "$tmp"; then
           66         parsejson < "$tmp"
           67 fi
           68 
           69 rm -f "$tmp"