youtube.sh - randomcrap - random crap programs of varying quality
 (HTM) git clone git://git.codemadness.org/randomcrap
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       youtube.sh (2089B)
       ---
            1 #!/bin/sh
            2 # Youtube search script.
            3 # Dependencies: awk, hurl (or curl), json2tsv
            4 # hurl:     https://codemadness.org/hurl.html
            5 # json2tsv: https://codemadness.org/json2tsv.html
            6 
            7 if test -z "$1"; then
            8         echo "$0 <search>" >&2
            9         exit 1
           10 fi
           11 
           12 #curl -s -m 10 -H 'User-Agent:' -H 'Accept-Language: en-US,en;q=0.5' "https://www.youtube.com/results?search_query=$(printf '%s' "$1" | sed 's@ @+@g')" | \
           13 hurl -t 10 -m 1000000 -H 'Accept-Language: en-US,en;q=0.5' "https://www.youtube.com/results?search_query=$(printf '%s' "$1" | sed 's@ @+@g')" | \
           14 awk '
           15 /^[         ]*window\["ytInitialData"\] = / {
           16         print substr($0, 31, length($0) - 31);
           17 }
           18 ' | \
           19 json2tsv | \
           20 awk -F '\t' '
           21 function show(o) {
           22         if (!length(o["id"]))
           23                 return;
           24         print o["title"] " [" o["length"] "]";
           25         print "Url:       https://www.youtube.com/embed/" o["id"];
           26 #        print "Thumbnail: https://i.ytimg.com/vi/" o["id"] "/default.jpg";
           27         print "Image:     https://i.ytimg.com/vi/" o["id"] "/hqdefault.jpg";
           28         print "Published: " o["published"];
           29         print "Views:     " o["viewcount"];
           30         print "Channel:   " o["author"] ", https://www.youtube.com/feeds/videos.xml?channel_id=" o["channelid"];
           31         print "";
           32 }
           33 $1 ~ /\.(items|contents)\[\]\.videoRenderer$/ {
           34         show(o);
           35         delete o;
           36 }
           37 $1 ~ /\.(items|contents)\[\]\.videoRenderer\.viewCountText\.simpleText$/ {
           38         o["viewcount"] = $3;
           39 }
           40 $1 ~ /\.(items|contents)\[\]\.videoRenderer\.videoId$/ {
           41         o["id"] = $3;
           42 }
           43 $1 ~ /\.(items|contents)\[\]\.videoRenderer\.title\.runs\[\]\.text$/ {
           44         o["title"] = $3;
           45 }
           46 $1 ~ /\.(items|contents)\[\]\.videoRenderer\.longBylineText\.runs\[\]\.text$/ {
           47         o["author"] = $3;
           48 }
           49 $1 ~ /\.(items|contents)\[\]\.videoRenderer\.longBylineText\.runs\[\]\.navigationEndpoint\.browseEndpoint\.canonicalBaseUrl$/ {
           50         o["authorurl"] = $3;
           51 }
           52 $1 ~ /\.(items|contents)\[\]\.videoRenderer\.longBylineText\.runs\[\]\.navigationEndpoint\.browseEndpoint\.browseId$/ {
           53         o["channelid"] = $3;
           54 }
           55 $1 ~ /\.(items|contents)\[\]\.videoRenderer\.publishedTimeText\.simpleText$/ {
           56         o["published"] = $3;
           57 }
           58 $1 ~ /\.(items|contents)\[\]\.videoRenderer\.lengthText\.simpleText$/ {
           59         o["length"] = $3;
           60 }
           61 END {
           62         show(o);
           63 }'