-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.py
417 lines (326 loc) · 11 KB
/
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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
from util_funcs import parse_image_post, parse_video_post, parse_gallery_post, upvote_chart, domain_chart, extension_chart
from tqdm import tqdm
from rich.console import Console
from rich import inspect
from pygal.style import NeonStyle, Style
import requests as rq
from datetime import datetime
import json
NeonStyle.transition = '0.3s ease-out'
NeonStyle.colors = (
'#ff5995',
'#b6e354',
'#feed6c',
'#8cedff',
'#9e6ffe',
'#899ca1',
'#f8f8f2',
'#bf4646',
# '#516083',
'#f92672',
'#82b414',
'#fd971f',
'#56c2d6',
'#808384',
'#8c54fe',
# '#465457'
)
NeonStyle.plot_background = NeonStyle.background = 'rgb(10, 10, 10)'
# NeonStyle.value_background = 'rgba(0,255,0,1)'
NeonStyle.foreground = 'rgba(200, 200, 200, 1)'
NeonStyle.foreground_strong = 'rgba(255, 255, 255, 1)'
# NeonStyle.foreground_subtle = 'rgba(0,0,255,1)'
def gen_files_json(ts:datetime):
"""
This function generates a JSON file containing data from various subreddits, including images,
videos, and galleries.
"""
# So that previous posts are preserved
with open('docs/files.json') as f:
raw_data:dict = json.load(f)['data']
pbar = tqdm(leave=True, colour=COLOR, total=len(SUBREDDITS))
for sub in SUBREDDITS:
# printf(f'[b GREEN1][JSON][/] {n+1}/{len(SUBREDDITS)}) {sub}')
pbar.set_description(sub)
url = URL.format(subreddit=sub)
r = rq.get(url, headers=HEADERS)
sub_data = r.json()
posts = sub_data['data']['children'] # This returns a list of posts (dictionaries)
for post in posts:
post_data = post['data']
# Checks if its a crosspost
if post_data.get('crosspost_parent_list'):
continue
# Checks if its a gallery
if post_data.get('is_gallery'):
dics = parse_gallery_post(post_data, IMAGE_URL)
raw_data.update(dics)
continue
# Checks if its a video
if post_data.get('is_video'):
dic = parse_video_post(post_data)
raw_data.update(dic)
continue
# Checks if its an image
if post_data.get('post_hint') == 'image': # Used .get() because images removed by REDDIT dont have a post_hint attribute, like https://www.reddit.com/r/Kitten/comments/tnilow/baby_kitten_summer_adventures/
dic = parse_image_post(post_data)
raw_data.update(dic)
pbar.update()
pbar.close()
to_dump = {
'total_media': len(raw_data),
'last_updated_utc': int(ts.timestamp()),
'last_updated_utc_readable': ts.strftime('%Y-%m-%d %H:%M:%S'),
'data': raw_data,
}
to_dump_min = {}
for fid in to_dump['data']:
to_dump_min[fid] = {}
to_dump_min[fid]['t'] = to_dump['data'][fid]['type'][0]
to_dump_min[fid]['p'] = to_dump['data'][fid]['post_url']
to_dump_min[fid]['m'] = to_dump['data'][fid]['media_url']
with open('docs/files.json', 'w') as f:
json.dump(to_dump, f, indent=4)
with open('docs/files.min.json', 'w') as f:
json.dump(to_dump_min, f)
# with open('testing/test10.json', 'w') as f:
# json.dump(to_write, f, indent=4)
def gen_stats(style:Style):
"""
The function generates charts from raw data and writes the processed data to a JSON file.
"""
# Gets the raw data
with open('docs/files.json') as f:
raw_data = json.load(f)['data'].values()
# Generates the charts
data = {}
data['upvotes'] = upvote_chart(raw_data, style)
data['extensions'] = extension_chart(raw_data, style)
data['domains'] = domain_chart(raw_data, style)
# Writes it to processed_data.json
with open('docs/stats/processed_data.json', 'w') as f:
json.dump(data, f, indent=4)
def gen_subs_md(ts:datetime):
"""
This function generates a markdown table of subreddit data including name, description, and number
of members, and saves it to a file with a timestamp.
"""
# Initialises text
text = '| | Subreddit | Description | Members |\n'
text += '| - | --------- | ----------- | ------- |\n'
# Gets all the data
pbar = tqdm(leave=True, colour=COLOR, total=len(SUBREDDITS))
for n, sub in enumerate(SUBREDDITS):
# printf(f'[b GREEN3][MARKDOWN][/] {n+1}/{len(SUBREDDITS)}) {sub}')
pbar.set_description(sub)
r = rq.get(f'https://www.reddit.com/r/{sub}/about.json', headers=HEADERS)
sub_data = r.json()['data']
name = sub_data['display_name_prefixed']
about = '-' if not sub_data['public_description'] else sub_data['public_description'].splitlines()[0] # Incase the about is empty
members = sub_data['subscribers']
text += f'| {n+1} | [{name}](https://reddit.com/r/{sub}) | {about} | {members:,} |\n'
pbar.update()
pbar.close()
# Adds timestamp
text += f'\n\n<br>\n\nLast Updated (UTC): {ts.strftime("%Y-%m-%d %H:%M:%S")}'
# Writes it
with open('subreddits.md', 'w') as f:
f.write(text)
def gen_files_info_json(ts:datetime):
'''
last_updated_utc,
last_updated_utc_readable,
total_subreddits,
total_files,
total_images,
total_videos,
'''
# Last updated
data = {
'last_updated_utc': int(ts.timestamp()),
'last_updated_utc_readable': ts.strftime('%Y-%m-%d %H:%M:%S'),
}
with open('docs/files.json') as f:
raw_data:dict = json.load(f)['data']
# Number of subreddits
data['total_subreddits'] = len(set([ i['subreddit'] for i in raw_data.values() ]))
# Composition of media
data['total_files'] = len(raw_data)
data['total_images'] = len([ i for i in raw_data if raw_data[i]['type']=='image' ])
data['total_videos'] = len([ i for i in raw_data if raw_data[i]['type']=='video' ])
# Dumping the data
with open('docs/files_info.json' , 'w') as f:
json.dump(data, f, indent=4)
URL = 'https://www.reddit.com/r/{subreddit}/top/.json?limit=100&t=year'
IMAGE_URL = 'https://i.redd.it/{filename}' # i is for images, v is for videos
HEADERS = {'User-Agent': 'https://github.com/msr8/cats'}
COLOR = '#696969'
SUBREDDITS = [
'activationsound',
'airplaneears',
'blurrypicturesofcats',
'catculations',
'catfaceplant',
'cathostage',
'catloaf',
'catpics',
'catpictures',
'cats',
'catsarealiens',
'catsareassholes',
'catsareliquid',
'catsaremuslim',
'catsbeingbanks',
'catsbeingcats',
'catsinsinks',
'catslaps',
'catsmirin',
'catsoncats',
'catsonkeyboards',
'catsridingroombas',
'catsstandingup',
'catsvstechnology',
'catswhochirp',
'catswhosmoke',
'catswhosqueak',
'catswhoyawn',
'catswhoyell',
'catswithjobs',
'catswithsocks',
'curledfeetsies',
'cuteguyswithcats',
'drillcats',
'flamepoints',
'floof',
'gingerkitty',
'greebles',
'holdmycatnip',
'ififitsisits',
'illegallysmolcats',
'kitten',
'legalcatadvice',
'meow_irl',
'meowow',
'miscatculations',
'motorboat',
'murdermittens',
'myhatisacat',
'nebelung',
'nervysquervies',
'noodlebones',
'notmycat',
'oneorangebraincell',
'packadaykitties',
'pallascats',
'petthedamncat',
'petthedamncat',
'pointytailedkittens',
'politecats',
'pottedcats',
'purrkour',
'sadcats',
'shouldercats',
'siamesecats',
'standardissuecat',
'startledcats',
'stealthbombers',
'stuffoncats',
'supermodelcats',
'thecatdimension',
'thecattrapisworking',
'thisismylifemeow',
'torties',
'tuckedinkitties',
'turkishcats',
'whatswrongwithyourcat',
'whiskerfireworks',
]
if __name__ == '__main__':
try:
console = Console()
ts = datetime.utcnow()
# gen_files_json(ts)
# gen_stats(NeonStyle)
# gen_subs_md(ts)
gen_files_info_json(ts)
except Exception as e:
console.print_exception()
# HEADERS = {'User-Agent': 'your_user_agent'}
# url = 'https://www.reddit.com/r/kitten/top/.json?limit=100&t=year'
# r = rq.get(url, headers=HEADERS)
# to_write = r.json()
# with open('testing/test9.json', 'w') as f:
# json.dump(to_write, f, indent=4)
'''
REMEMBER, GALLERY POSTS DONT HAVE "post_hint"
test.json: ALL
test2.json: Having the "is_gallery" attribute
test3.json: Not having the "i.redd.it" domain
test4.json: The post_data of all images and non-galleries
test5.json: The needed data of all images (including galleries) scraped from one sub
test6.json: The needed data of all images and videos scraped from one sub
test7.json: The needed data of all images and videos scraped from different subs
test8.json: ALL the data of r/catswhoyawn, because that subreddit was giving me an error
test9.json: ALL the data of r/kitten, because that subreddit was giving me an error
test10.json: ALL the data of all posts having the "is_gallery" attribute
TO-DO
-> add /library
CustomStyle = Style(
background='black',
ci_colors=(),
colors=('#ff5995', '#b6e354', '#feed6c', '#8cedff', '#9e6ffe', '#899ca1', '#f8f8f2', '#bf4646', '#516083', '#f92672', '#82b414', '#fd971f', '#56c2d6', '#808384', '#8c54fe', '#465457'),
dot_opacity='1',
font_family='Consolas, "Liberation Mono", Menlo, Courier, monospace',
foreground='#999',
foreground_strong='#eee',
foreground_subtle='#555',
guide_stroke_color='black',
guide_stroke_dasharray='4,4',
label_font_family=None,
label_font_size=10,
legend_font_family=None,
legend_font_size=14,
major_guide_stroke_color='black',
major_guide_stroke_dasharray='6,6',
major_label_font_family=None,
major_label_font_size=10,
no_data_font_family=None,
no_data_font_size=64,
opacity='.1',
opacity_hover='.75',
plot_background='#111',
stroke_opacity='.8',
stroke_opacity_hover='.9',
stroke_width='1',
stroke_width_hover='4',
title_font_family=None,
title_font_size=16,
tooltip_font_family=None,
tooltip_font_size=14,
transition='0.5s ease-out',
value_background='rgba(229, 229, 229, 1)',
value_colors=(),
value_font_family=None,
value_font_size=16,
value_label_font_family=None,
value_label_font_size=10
)
.ok {
background-color: #ff5995;
background-color: #b6e354;
background-color: #feed6c;
background-color: #8cedff;
background-color: #9e6ffe;
background-color: #899ca1;
background-color: #f8f8f2;
background-color: #bf4646;
background-color: #516083;
background-color: #f92672;
background-color: #82b414;
background-color: #fd971f;
background-color: #56c2d6;
background-color: #808384;
background-color: #8c54fe;
background-color: #465457;
}
'''