-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGimmeSomeJazzGifs.py
52 lines (45 loc) · 1.41 KB
/
GimmeSomeJazzGifs.py
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
import requests
import json
from random import choice
class GimmeSomeJazzGifs:
def __init__(self, apikey):
self.apikey = apikey
self.search_terms = [
"jazz",
"saxophone",
"trumpet",
"trombone",
"drums",
"singer",
"coltrane",
"music",
"piano",
"blues"
]
self.lmt = 512
self.buffer = []
self.buffer_size = 128
def draw(self):
r = requests.get(
"https://g.tenor.com/v1/search?q=%s&key=%s&limit=%s" % (choice(self.search_terms),
self.apikey,
self.lmt)
)
if r.status_code == 200:
# load the GIFs using the urls for the smaller GIF sizes
top_gifs = json.loads(r.content)
url = [m for m in choice(top_gifs['results'])['media'] if 'mediumgif' in m][0]['mediumgif']['url']
if url not in self.buffer:
self.buffer.append(url)
if len(self.buffer) > self.buffer_size:
self.buffer.pop(0)
return url
else:
return self.draw()
else:
return None
def main():
g = GimmeSomeJazzGifs()
print(g.draw())
if __name__ == '__main__':
main()