-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_discord_emotes.py
190 lines (133 loc) · 6.99 KB
/
get_discord_emotes.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#|------------------------------------------------|
#| Imports |
#|------------------------------------------------|
#--------------------------------------
# standard imports
#--------------------------------------
import re
import ctypes
import sys
import os
import requests
import time
#--------------------------------------
# project specific imports
#--------------------------------------
import constants as const
#--------------------------------------
# public/3rd party imports
#--------------------------------------
from pathlib import Path
#|------------------------------------------------|
#| fn defs |
#|------------------------------------------------|
# regex match contains index of last matched char; we look at that
def checkExt(strData, index):
start = index - const.OFFSET_FOR_EXT_START
end = index - const.OFFSET_FOR_EXT_END
firstChar = strData[start]
lastChar = strData[end]
if (firstChar == 'p' or firstChar == 'w') and (lastChar == 'b' or lastChar == 'g'):
return const.PNG_LIST
else:
if ((firstChar == 'g') and (lastChar == 'f') ):
return const.GIF_LIST
return const.NO_MATCH
def makeFullURL(file_id_string, src_type):
if src_type == const.SRC_TYPE_ID_STICKER:
starturl = const.DISC_STICKER_START_URL
elif src_type == const.SRC_TYPE_ID_EMOJI:
starturl = const.DISC_EMOTE_START_URL
else:
return
return starturl + file_id_string + const.ORIG_QUALITY
def getSubString(strData, id_pos_start, id_pos_end):
if (id_pos_start < 0) or (id_pos_end > len(strData)):
return
return strData[id_pos_start:id_pos_end]
def placeInDict(strData, id_pos_start, id_pos_end, dType, ourDict, src_type):
emote_ID = getSubString(strData, id_pos_start, id_pos_end)
if emote_ID not in ourDict:
link = makeFullURL(emote_ID, src_type)
ourDict[emote_ID] = (dType, link)
def savePicFiles(fileName, fileData):
with open(fileName, 'wb') as f:
f.write(fileData)
def moveToFolder(folderName):
if not os.path.isdir(folderName):
Path(folderName).mkdir(parents=True, exist_ok=True)
os.chdir(folderName)
def filterDuplicates(ourDict):
for item in os.scandir('.'):
if item.is_file():
fileName = Path(item.name).stem
if fileName in ourDict:
del ourDict[fileName]
def createMessageBox(msg, title_name, flag):
return ctypes.windll.user32.MessageBoxW(None, msg, title_name, flag)
#|------------------------------------------------|
#| main stuff |
#|------------------------------------------------|
def run():
try:
txt_file = sys.argv[1]
if txt_file:
emote_urls = {}
failedCount = 0
with open(txt_file, errors="ignore") as f: # ignore 'unrecognizable' chars
for line in f:
match = re.search(const.RE_PATTERN_ALL, line)
if match:
spanVals = match.span()
extType = checkExt(line, spanVals[1])
if extType != const.NO_MATCH:
src_val = 0
dictIndexStart = spanVals[0]
secondLetter = dictIndexStart + 1
dictIndexStart += const.OFFSET_FOR_START_ID # get to start of emote ID part; ie, need to move 8 chars (/emojis/)
# /stickers/ is 2 chars more than (/emojis/), hence the offset addition coming up
if line[secondLetter] == 'e':
src_val = const.SRC_TYPE_ID_EMOJI
elif line[secondLetter] == 's':
src_val = const.SRC_TYPE_ID_STICKER
dictIndexStart += const.STICKER_INDEX_OFFSET
dictIndexEnd = dictIndexStart + const.OFFSET_FOR_END_ID
placeInDict(line, dictIndexStart, dictIndexEnd , extType, emote_urls, src_val)
orig_len = len(emote_urls)
if orig_len != 0:
result = createMessageBox('parsed emote links for a total of: ' + str(orig_len) + ' links' + '\n' + 'download?', 'Matches found!', const.W32_MB_YESNOCANCEL)
else:
result = createMessageBox('no emote links parsed; check input .txt file data', 'Error: No matches found', const.W32_MB_OK )
if result == const.W32_IDYES:
moveToFolder(const.FOLDER_NAME)
filterDuplicates(emote_urls)
number = 1
filteredListLen = len(emote_urls)
diff = orig_len - filteredListLen
createMessageBox(str(diff) + ' have already been downloaded. \n' + 'downloading remaining ' + str(filteredListLen) + ' links', 'Matches found!', const.W32_MB_OK )
for item in emote_urls:
downloadurl = emote_urls[item][1]
fileType = emote_urls[item][0]
time.sleep(const.REQ_DELAY)
requestedPage = requests.get(downloadurl, headers={'User-Agent': 'Mozilla/5.0'}, timeout= None)
if requestedPage:
if fileType == const.PNG_LIST:
createdFileName = item + ".png"
elif fileType == const.GIF_LIST:
createdFileName = item + ".gif"
if (requestedPage.status_code == const.HTTP_OK or requestedPage.status_code == const.HTTP_NOT_MODIFIED):
savePicFiles(createdFileName, requestedPage.content)
status = "success!"
else:
failedCount += 1
status = "fail w/ HTTP Error: " + str(requestedPage.status_code)
print(str(number) + "/" + str(filteredListLen) + ": " + status)
number += 1
if failedCount:
createMessageBox('Failed to grab ' + str(failedCount) + ' emotes', 'Error: missed some data', const.W32_MB_ICONWARNING )
else:
createMessageBox('all emotes obtained :)', 'thabk u', const.W32_MB_OK )
#
except IndexError:
createMessageBox('Please drag and drop a .txt file! Exiting...', 'Error: No input file', const.W32_MB_ICONWARNING )
run()