-
Notifications
You must be signed in to change notification settings - Fork 3
/
engine.py
64 lines (51 loc) · 1.59 KB
/
engine.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
53
54
55
56
57
58
59
60
61
62
63
64
import requests, json
def search_endpoint_engine(api_key, title, locale):
r = requests.get("http://api.giphy.com/v1/gifs/search?q={0}&api_key={1}&limit=1&lang={2}".format(title, api_key, locale))
if r.status_code != 200:
return str(r.status_code)
else:
data = r.json()
if not data['data']:
print(r.status_code)
return "fail"
else:
for i in data['data']:
link = i['images']['original']['url']
return link
def translate_endpoint_engine(api_key, title):
r = requests.get("http://api.giphy.com/v1/gifs/translate?s={0}&api_key={1}".format(title, api_key))
if r.status_code != 200:
return str(r.status_code)
else:
data = r.json()
if not data['data']:
print(r.status_code)
return "fail"
else:
return data['data']['images']['original']['url']
def trending_endpoint_engine(api_key, limit):
r = requests.get("https://api.giphy.com/v1/gifs/trending?api_key={0}&limit={1}".format(api_key, limit))
if r.status_code != 200:
print(r.status_code)
else:
data = r.json()
elements = []
for i in data['data']:
element = {}
element['title'] = "Trending gif of the day"
element['image_url'] = i['images']['original']['url']
element['buttons'] = [
{
"type": "element_share"
}
]
elements.append(element)
return elements
def random_endpoint_engine(api_key, tag):
r = requests.get("https://api.giphy.com/v1/gifs/random?api_key={0}&tag={1}".format(api_key, tag))
if r.status_code != 200:
print(r.status_code)
pass
else:
data = r.json()
return data['data']['image_url']