-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgiphy.sh
executable file
·52 lines (48 loc) · 1.39 KB
/
giphy.sh
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
48
49
50
51
52
#!/bin/bash
# Author: Jia (Jason) Teoh (jteoh)
RANDOM_GIF_SEARCH="http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&rating=g&tag="
RANDOM_STICKER_SEARCH="http://api.giphy.com/v1/stickers/random?api_key=dc6zaTOxFJmzC&rating=g&tag="
TRANSLATE_SEARCH="http://api.giphy.com/v1/gifs/translate?api_key=dc6zaTOxFJmzC&rating=g&s="
if [ "$#" -eq 0 ]; then
search_type="gif"
echo "Defaulting to random GIF search"
else
search_type=$1
fi
case $search_type in
gif)
base_url=$RANDOM_GIF_SEARCH
;;
sticker)
base_url=$RANDOM_STICKER_SEARCH
;;
translate)
base_url=$TRANSLATE_SEARCH
;;
*)
echo "Invalid search type: $search_type must be one of 'gif', 'sticker', 'translate'"
exit -1
esac
if [ "$#" -gt 1 ]; then
orig_search=${@:2}
else
orig_search="pugs"
echo "Defaulting to search term 'pugs'"
fi
echo "Finding $search_type for '$orig_search'"
search=$(sed -e 's/ */\-/g' <<< $orig_search)
url="$base_url$search"
echo "Searching $url"
result=$(curl -s $url | python -c 'import sys, json; print json.load(sys.stdin)["data"]["url"]' | tr -d '\n')
if [[ -z "$result" ]]; then
echo "Unable to find url. No results?"
exit -1
fi
open "$result"
copy_string="$result ($orig_search)"
if [[ $OSTYPE == "linux-gnu" ]]; then
xclip -selection c <<< $copy_string
else
pbcopy <<< $copy_string
fi
echo "$copy_string"