forked from sararin/Discord-Scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord.py
348 lines (253 loc) · 10.7 KB
/
discord.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#
# Imported module functions
#
# Use our SimpleRequests module for this experimental version.
from SimpleRequests import SimpleRequest
from SimpleRequests.SimpleRequest import error
# Use the datetime module for generating timestamps and snowflakes.
from datetime import datetime, timedelta
# Use the time module for generating timestamps that are backwards compatible with Python 2.
from time import mktime
# Use the os module for creating directories and writing files.
from os import makedirs, getcwd, path
# Use the mimetypes module to determine the mimetype of a file.
from mimetypes import MimeTypes
# Use the sqlite3 module to access SQLite databases.
from sqlite3 import connect
# Use the random module to choose from a list at random.
from random import choice
# Convert JSON to a Python dictionary for ease of traversal.
from json import loads
#
# Lambda functions
#
# Return a random string of a specified length.
random_str = lambda length: ''.join([choice('0123456789ABCDEF') for i in range(length)])
# Get the mimetype string from an input filename.
mimetype = lambda name: MimeTypes().guess_type(name)[0] \
if MimeTypes().guess_type(name)[0] is not None \
else 'application/octet-stream'
# Return a Discord snowflake from a timestamp.
snowflake = lambda timestamp_s: (timestamp_s * 1000 - 1420070400000) << 22
# Return a timestamp from a Discord snowflake.
timestamp = lambda snowflake_t: ((snowflake_t >> 22) + 1420070400000) / 1000.0
#
# Global functions
#
def get_day(day, month, year):
"""Get the timestamps from 00:00 to 23:59 of the given day.
:param day: The target day.
:param month: The target month.
:param year: The target year.
"""
min_time = mktime((year, month, day, 0, 0, 0, -1, -1, -1))
max_time = mktime((year, month, day, 23, 59, 59, -1, -1, -1))
return {
'00:00': snowflake(int(min_time)),
'23:59': snowflake(int(max_time))
}
def safe_name(name):
"""Convert name to a *nix/Windows compliant name.
:param name: The filename to convert.
"""
output = ""
for char in name:
if char not in '\\/<>:"|?*':
output += char
return output
def create_query_body(**kwargs):
"""Generate a search query string for Discord."""
query = ""
for key, value in kwargs.items():
if value is True and key != 'nsfw':
query += '&has=%s' % key[:-1]
if key == 'nsfw':
query += '&include_nsfw=%s' % str(value).lower()
return query
#
# Classes
#
class DiscordConfig(object):
"""Just a class used to store configs as objects."""
class Discord:
"""Experimental Discord scraper class."""
def __init__(self, config='config.json', apiver='v6'):
"""Discord constructor.
:param config: The configuration JSON file.
:param apiver: The current Discord API version.
"""
with open(config, 'r') as configfile:
configdata = loads(configfile.read())
cfg = type('DiscordConfig', (object,), configdata)()
if cfg.token == "" or cfg.token is None:
error('You must have an authorization token set in %s' % config)
exit(-1)
self.api = apiver
self.buffer = cfg.buffer
self.headers = {
'user-agent': cfg.agent,
'authorization': cfg.token
}
self.types = cfg.types
self.query = create_query_body(
images=cfg.query['images'],
files=cfg.query['files'],
embeds=cfg.query['embeds'],
links=cfg.query['links'],
videos=cfg.query['videos'],
nsfw=cfg.query['nsfw']
)
self.directs = cfg.directs if len(cfg.directs) > 0 else {}
self.servers = cfg.servers if len(cfg.servers) > 0 else {}
# Save us the time by exiting out when there's nothing to scrape.
if len(cfg.directs) == 0 and len(cfg.servers) == 0:
error('No servers or DMs were set to be grabbed, exiting.')
exit(0)
def get_server_name(self, serverid, isdm=False):
"""Get the server name by its ID.
:param serverid: The server ID.
:param isdm: A flag to check whether we're in a DM or not.
"""
if isdm:
return serverid
request = SimpleRequest(self.headers).request
server = request.grab_page('https://discordapp.com/api/%s/guilds/%s' % (self.api, serverid))
if server is not None and len(server) > 0:
return '%s_%s' % (serverid, safe_name(server['name']))
else:
error('Unable to fetch server name from id, generating one instead.')
return '%s_%s' % (serverid, random_str(12))
def get_channel_name(self, channelid, isdm=False):
"""Get the channel name by its ID.
:param channelid: The channel ID.
:param isdm: A flag to check whether we're in a DM or not.
"""
if isdm:
return channelid
request = SimpleRequest(self.headers).request
channel = request.grab_page('https://discordapp.com/api/%s/channels/%s' % (self.api, channelid))
if channel is not None and len(channel) > 0:
return '%s_%s' % (channelid, safe_name(channel['name']))
else:
error('Unable to fetch channel name from id, generating one instead.')
return '%s_%s' % (channelid, random_str(12))
@staticmethod
def create_folders(server, channel):
"""Create the folder structure.
:param server: The server name.
:param channel: The channel name.
"""
folder = path.join(getcwd(), 'Discord Scrapes', server, channel)
if not path.exists(folder):
makedirs(folder)
return folder
def download(self, url, folder):
"""Download the contents of a URL.
:param url: The target URL.
:param folder: The target folder.
"""
request = SimpleRequest(self.headers).request
request.set_header('user-agent', 'Mozilla/5.0 (X11; Linux x86_64) Chrome/78.0.3904.87 Safari/537.36')
filename = safe_name('%s_%s' % (url.split('/')[-2], url.split('/')[-1]))
if not path.exists(filename):
request.stream_file(url, folder, filename, self.buffer)
def check_config_mimetypes(self, source, folder):
"""Check the config settings against the source mimetype.
:param source: Response from Discord search.
:param folder: Folder where the data will be stored.
"""
for attachment in source['attachments']:
if self.types['images'] is True:
if mimetype(attachment['proxy_url']).split('/')[0] == 'image':
self.download(attachment['proxy_url'], folder)
if self.types['videos'] is True:
if mimetype(attachment['proxy_url']).split('/')[0] == 'video':
self.download(attachment['proxy_url'], folder)
if self.types['files'] is True:
if mimetype(attachment['proxy_url']).split('/')[0] not in ['image', 'video']:
self.download(attachment['proxy_url'], folder)
@staticmethod
def insert_text(server, channel, message):
"""Insert the text data into our SQLite database file.
:param server: The server name.
:param channel: The channel name.
:param message: Our message object.
"""
dbdir = path.join(getcwd(), 'Discord Scrapes')
if not path.exists(dbdir):
makedirs(dbdir)
dbfile = path.join(dbdir, 'text.db')
db = connect(dbfile)
c = db.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS text_%s_%s (
id TEXT,
name TEXT,
content TEXT,
timestamp TEXT
)''' % (server, channel))
c.execute('INSERT INTO text_%s_%s VALUES (?,?,?,?)' % (server, channel), (
message['author']['id'],
'%s#%s' % (message['author']['username'], message['author']['discriminator']),
message['content'],
message['timestamp']
))
db.commit()
db.close()
def grab_data(self, folder, server, channel, isdm=False):
"""Scan and grab the attachments.
:param folder: The folder name.
:param server: The server name.
:param channel: The channel name.
:param isdm: A flag to check whether we're in a DM or not.
"""
date = datetime.today()
while date.year >= 2015:
request = SimpleRequest(self.headers).request
today = get_day(date.day, date.month, date.year)
if not isdm:
request.set_header('referer', 'https://discordapp.com/channels/%s/%s' % (server, channel))
content = request.grab_page(
'https://discordapp.com/api/%s/guilds/%s/messages/search?channel_id=%s&min_id=%s&max_id=%s&%s' %
(self.api, server, channel, today['00:00'], today['23:59'], self.query)
)
else:
request.set_header('referer', 'https://discordapp.com/channels/@me/%s' % channel)
content = request.grab_page(
'https://discordapp.com/api/%s/channels/%s/messages/search?min_id=%s&max_id=%s&%s' %
(self.api, channel, today['00:00'], today['23:59'], self.query)
)
try:
if content['messages'] is not None:
for messages in content['messages']:
for message in messages:
self.check_config_mimetypes(message, folder)
if self.types['text'] is True:
if len(message['content']) > 0:
self.insert_text(server, channel, message)
except TypeError:
continue
date += timedelta(days=-1)
def grab_server_data(self):
"""Scan and grab the attachments within a server."""
for server, channels in self.servers.items():
for channel in channels:
folder = self.create_folders(
self.get_server_name(server),
self.get_channel_name(channel)
)
self.grab_data(folder, server, channel)
def grab_dm_data(self):
"""Scan and grab the attachments within a direct message."""
for alias, channel in self.directs.items():
folder = self.create_folders(
path.join('Direct Messages', alias),
channel
)
self.grab_data(folder, alias, channel, True)
#
# Initializer
#
if __name__ == '__main__':
ds = Discord()
ds.grab_server_data()
ds.grab_dm_data()