-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeme_gen.py
35 lines (30 loc) · 1.14 KB
/
meme_gen.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
import requests
# imgflip
IMGFLIP_API_URL = "https://api.imgflip.com"
class MemeGenerator:
def __init__(self, username : str, password : str) -> None:
self.username = username
self.password = password
def list_memes(self, n : int) -> str:
response = requests.get(IMGFLIP_API_URL+"/get_memes")
memes = (response.json()["data"])["memes"]
result = ""
for meme in memes[:(n-1)]:
whitespace = (12-len(meme["id"]))*"."*2+meme["name"]
add = meme["id"]+whitespace+" [here]("+meme["url"]+")\n"
result += add
return result
def make_meme(
self, template_id: int, top_text: str, bottom_text: str
) -> str:
data = {"template_id": template_id,
"username": self.username,
"password": self.password,
"text0": top_text,
"text1": bottom_text
}
response = requests.post(IMGFLIP_API_URL+"/caption_image", data=data)
if response.json()["success"]:
return (response.json()["data"])["url"]
else:
return response.json()["error_message"]