summaryrefslogtreecommitdiff
path: root/q-yt
blob: 852b767758da34e72fb1e80ff2e8684986960aa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/zsh
# Search for things in places

key=$(url encode < $XDG_CONFIG_HOME/priv/keys/youtube)
requests=12
api_base=$(uri create                           \
            --scheme=https                      \
            --authority=www.googleapis.com      \
            --path=youtube/v3/search            \
            --query-param-part=snippet          \
            --query-param-maxResults=$requests  \
            --query-param-key=$key)
name=${${0:t}#q-}

function query {
    local q type

    type=video
    url=$(uri change                \
           --query-param-q="$*"     \
           --query-param-type=$type \
           <<< $api_base)

    curl -s $url
}

function parse {
    jshon -CQ -e items              \
        -a                          \
            -e snippet              \
                -e title -up        \
                -e description -upp \
            -e id -e videoId -u |   \
        while {
            read -r title
            read -r desc
            read -r id
        } do
            printf '%s!%s!%s!%s\n' \
                "$name"            \
                "${title//!}"      \
                "${desc//!}"       \
                "ytdl://$id"
        done
}

query $@ | parse