ys (858B)
1 #!/bin/sh 2 # 3 # perform a search on youtube and return the best result (title + link) 4 5 usage() { 6 echo "`basename $0` [-htu] [-n <num>] <query>" 7 8 test -z "$1" && return 9 10 cat <<EOF 11 -h : display this help 12 -t : output titles only (default 'title - uri') 13 -u : output uris only 14 -n : print only <num> results (default: 3) 15 EOF 16 } 17 18 num=10 19 regex='^.*<a href="\(/watch[^"]*\)"[^>]*>\([^<]*\)</a>.*$' 20 output='\2 - https://youtube.com\1' 21 22 while getopts "hn:tu" OPT; do 23 case $OPT in 24 n) num=$OPTARG;; 25 t) output='\2';; 26 u) output='http://youtube.com\1';; 27 h) usage long; exit 0;; 28 *) usage; exit 1;; 29 esac 30 done 31 32 shift $((OPTIND - 1)) 33 34 query=$(echo $@ | tr ' ' '+') 35 url="https://www.youtube.com/results?search_query=${query}" 36 37 curl -k -s "$url" | sed -n "s,$regex,$output,p" | sed ${num}q