t2f30 ys - scripts - random scripts HTML git clone git://parazyd.org/scripts.git DIR Log DIR Files DIR Refs --- DIR commit 0f137bc0349c5839ddf2e5f3a48416438de7549b DIR parent 03dc04f52bf41dedfdec38532eb7df43873144ff HTML Author: parazyd <parazyd@dyne.org> Date: Wed, 15 Feb 2017 05:35:22 +0100 2f30 ys Diffstat: C ys -> obsolete/ys | 0 M ys | 67 ++++++++++++++++--------------- 2 files changed, 34 insertions(+), 33 deletions(-) --- DIR diff --git a/ys b/obsolete/ys DIR diff --git a/ys b/ys t@@ -1,37 +1,38 @@ #!/bin/sh # -# perform a search on youtube and return the best result (title + link) - -usage() { - echo "`basename $0` [-htu] [-n <num>] <query>" - - test -z "$1" && return - - cat <<EOF - -h : display this help - -t : output titles only (default 'title - uri') - -u : output uris only - -n : print only <num> results (default: 3) -EOF +# 2f30 +# search youtube via api + +KEY="AIzaSyAa5gAarPnuu9zTVjpp6mPyStbY17uuhSE" +NRES=10 + +search() { + sstr="" + if [ ${#} -ne 1 ]; then + for arg in $@; do + sstr=$sstr"$arg|" + done + else + sstr=$1 + fi + + curl -s \ + -G "https://www.googleapis.com/youtube/v3/search" \ + -d part="snippet" \ + -d q=$sstr \ + -d maxResults=$NRES \ + -d key=$KEY | \ + jq '[.items[] | {"title":.snippet.title, + "url": ["https://www.youtube.com/watch?v=" + .id.videoId] }]' - } -num=10 -regex='^.*<a href="\(/watch[^"]*\)"[^>]*>\([^<]*\)</a>.*$' -output='\2 - https://youtube.com\1' - -while getopts "hn:tu" OPT; do - case $OPT in - n) num=$OPTARG;; - t) output='\2';; - u) output='http://youtube.com\1';; - h) usage long; exit 0;; - *) usage; exit 1;; - esac -done - -shift $((OPTIND - 1)) - -query=$(echo $@ | tr ' ' '+') -url="https://www.youtube.com/results?search_query=${query}" - -curl -k -s "$url" | sed -n "s,$regex,$output,p" | sed ${num}q +case $1 in + -n) + NRES=$2 + shift 2 + search $@ + ;; + *) + search $@ + ;; +esac