-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemotetosticker.py
93 lines (73 loc) · 2.46 KB
/
emotetosticker.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import argparse
import requests
import re
def get_value(text, name):
token_search_str = f"value=\"[a-z0-9-.]+\" name=\"{name}\""
token_match = re.search(token_search_str, text, re.I)
token = token_match.group().split('"')[1]
return token
def get_action(text):
search_str = "action=\".+\" method=\"POST\""
match = re.search(search_str, text, re.I)
action = match.group().split('"')[1]
return action
def get_apng_url(response):
search_str = "<a class=\"save\" href=\"" + \
"https://s[0-9]{1,2}.ezgif[./a-z0-9-]+(?!gif)png\""
match = re.search(search_str, response.text, re.I)
apng = match.group().split('"')[3]
return apng
def convert_to_apng(emote_url):
url_base = "https://ezgif.com/gif-to-apng"
params = {
"url": emote_url,
}
conv_r = requests.get(url_base, params)
file = get_value(conv_r.text, "file")
token = get_value(conv_r.text, "token")
action = get_action(conv_r.text)
data = {
"token": token,
"file": file
}
response = requests.post(url=action, data=data)
return get_apng_url(response)
def resize_apng(emote_url):
url_base = "https://ezgif.com/resize"
params = {
"url": emote_url,
}
resize_r = requests.get(url_base, params)
file = get_value(resize_r.text, "file")
token = get_value(resize_r.text, "token")
old_width = int(get_value(resize_r.text, "old_width"))
old_height = int(get_value(resize_r.text, "old_height"))
height = 320
width = 320
action = get_action(resize_r.text)
percentage = max(height / old_height, width / old_width) * 100
data = {
"token": token,
"file": file,
"old_width": old_width,
"old_height": old_height,
"width": width,
"height": height,
"percentage": percentage
}
response = requests.post(url=action, data=data)
return get_apng_url(response)
def main(emote_url):
apng = convert_to_apng(emote_url)
apng = resize_apng(apng)
return apng
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="Emote To Sticker")
parser.add_argument("emote_url",
help="The URL you get when you right-click "
"an emote in a Discord message and hit the "
"'Copy Link' option",
metavar="Emote URL",
type=str)
args = parser.parse_args()
print(main(args.emote_url))