tydl - scripts - various script and utils
(HTM) git clone git://z3bra.org/scripts
(DIR) Log
(DIR) Files
(DIR) Refs
---
tydl (1228B)
---
1 #!/bin/sh
2 #
3 # z3bra - (c) wtfpl 2014
4 # download the audio track of the first result of a youtube search
5 # and add it to MPD library (will end up with a .mp3, for tags)
6
7 usage() {
8 echo "`basename $0` [-h] <query>"
9 }
10
11 ys() {
12 num=3
13 regex='^.*<a href="\(/watch[^"]*\)"[^>]*>\([^<]*\)</a>.*$'
14 output='\2 - http://youtube.com\1'
15
16 while getopts "hn:tu" OPT; do
17 case $OPT in
18 n) num=$OPTARG;;
19 t) output='\2';;
20 u) output='http://youtube.com\1';;
21 h) usage long; exit 0;;
22 *) usage; exit 1;;
23 esac
24 done
25
26 shift $((OPTIND - 1))
27
28 query=$(echo $@ | tr ' ' '+')
29 url="https://www.youtube.com/results?search_query=${query}"
30
31 curl -s "$url" | sed -n "s,$regex,$output,p" | sed ${num}q
32 }
33
34 # don't process if no argument given
35 test $# -eq 0 && usage && exit 1
36
37 # you can either pass MULTIPLE search terms or a SINGLE url
38 test $# -gt 1 && uri=$(ys -n1 -u $@) || uri=$1
39
40 # give up if we got no uri
41 if test -z "$uri"; then
42 echo "no result found"
43 exit 1
44 fi
45
46 # download and extract audio track
47 youtube-dl -q -x --audio-format mp3 -o '%(title)s.%(ext)s' "$uri"
48
49 exit 0