-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpull.py
executable file
·478 lines (370 loc) · 15.3 KB
/
pull.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/usr/bin/env python3
import praw
import logging
import os
import urllib
import mysecrets
import json
import sys
import pdb
import hashlib
import imagehash
import re
import argparse
import photohash
import subprocess
import redis
import time
from glob import glob
from PIL import Image
from urllib.parse import urlparse
from imgurpython import ImgurClient
from datetime import datetime
import pprint
start = time.time()
last = start
addurl = lambda urllist, what, entry: urllist.add(f'{what} {entry.subreddit}/{entry.id}')
def ts(w):
global start, last
now = time.time()
#print("{:10.4f} {:10.4f} {}".format(now - last, now - start, w))
last = now
r = redis.Redis(host='localhost', port=6379, db=0,charset="utf-8", decode_responses=True)
logging.basicConfig(level=os.getenv('LOGLEVEL') or 'WARNING')
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--force", help="Force", action='store_true')
parser.add_argument("-g", "--gallery", help="Get the galleries again", action='store_true')
parser.add_argument("-v", "--video", help="Get the video again", action='store_true')
parser.add_argument("-r", "--redgif", help="Get just the redgif again", action='store_true')
args, unknown = parser.parse_known_args()
reddit = praw.Reddit(
client_id=mysecrets.reddit['pull']['id'],
client_secret=mysecrets.reddit['pull']['secret'],
password=mysecrets.reddit['pull']['password'], user_agent='test',
username=mysecrets.reddit['pull']['username']
)
ts('con:reddit')
gfy_list = ['gfycat.com', 'i.redgifs.com', 'redgifs.com', 'www.redgifs.com']
try:
imgur = ImgurClient(
mysecrets.imgur['id'],
mysecrets.imgur['secret']
)
except:
imgur = None
logging.warning("IMGUR failed to load")
ts('con:img')
def lf(path, kind = 'set'):
if os.path.exists(path):
with open(path) as fp:
if kind == 'json':
try:
return json.load(fp)
except:
return {}
return set(fp.read().splitlines())
fail = lf('fail.json', 'json') or {}
subblock = lf('subblock.txt') or set()
def get(url):
request = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'})
return urllib.request.urlopen(request)
def cksumcheck(path, doDelete=True, who=None):
filename = os.path.basename(path)
ihash = r.hget('cksum_rev', "{}/{}".format(who,filename))
if ihash:
return ihash
style = 'md5'
ext = os.path.splitext(path)[1]
print(" Summing {}/{}".format(who,path))
if not os.path.exists(path):
return False
if ext in ['.jpg','.png']:
try:
ihash = imagehash.average_hash(Image.open(path))
style = 'ihash'
except:
style = 'md5'
if style == 'md5':
ihash = hashlib.md5(open(path, 'rb').read()).hexdigest()
style='md5'
#print(style,ext,ihash,path)
ihash = str(ihash)
js = None
exists = r.hget('cksum',ihash)
if exists:
js = json.loads(exists)
if js and not ( filename in js[1] or js[1] in filename ):
print(" == {} is {} ".format(filename, exists))
r.hset('ignore', path, ihash)
if doDelete:
os.unlink(path)
return False
else:
r.hset('cksum', ihash, json.dumps([who, filename]))
r.sadd('cksum_seen', filename)
r.hset('cksum_rev', "{}/{}".format(who,filename), ihash)
return ihash
if len(unknown) > 0:
all = list([m.lower() for m in unknown])
else:
with open('userlist.txt') as fp:
all = sorted(list(set([x[1].strip('/\n ') for x in enumerate(fp)])), key=str.casefold)
if not os.path.isdir('data'):
os.mkdir('data')
for who in all:
content = "data/{}".format(who)
# if we've made the user path
if os.path.exists(content):
# this is O(m*n), hate me later.
existing = list(glob("{}/*[jp][np]g".format(content)))
for i in range(0, len(existing)):
ipath = existing[i]
filename = os.path.basename(ipath)
cut_path = "{}/{}".format(who,filename)
is_new = False
if not r.sismember('cksum_seen', filename):
ihash = cksumcheck(ipath, who=who)
is_new = True
else:
ihash = r.hget('cksum_rev', cut_path)
if is_new:
for j in range(i + 1, len(existing)):
jpath = existing[j]
filename = os.path.basename(jpath)
cut_path = "{}/{}".format(who,filename)
jhash = r.hget('cksum_rev',cut_path) or cksumcheck(jpath, who=who)
try:
dist = photohash.hash_distance(ihash, jhash)
if dist < 3:
print("{} {} == {}".format(dist, existing[i], existing[j]))
r.hset('ignore', ipath, ihash)
if os.path.exists(existing[i]):
os.unlink(existing[i])
except:
pass
for path in glob("{}/*.mp4".format(content)):
flatten = re.sub('/', '_', path)
swapped = re.sub('.mp4', '.jpg', flatten)
path_tn = "tn/{}".format(swapped)
if os.path.exists(path_tn) and not cksumcheck(path_tn, doDelete=False, who=who):
os.unlink(path_tn)
os.unlink(path)
if fail.get(who) and fail.get(who) > 3:
print(" -- {}".format(who))
continue
if not os.path.exists(content):
print(" /{} (Making dir)".format(who))
os.mkdir(content)
else:
print(" /{}".format(who))
urllist = set()
if not args.force:
urllist = set([x.split(' ')[0] for x in lf("{}/urllist.txt".format(content)) or set()])
titlelist = lf("{}/titlelist.txt".format(content)) or set()
entrylist = lf("{}/entrylist.txt".format(content)) or set()
subredUser = lf("{}/subredditlist.txt".format(content), 'json') or []
commentMap = lf("{}/commentmap.txt".format(content), 'json') or dict()
ts('pre sub pull')
try:
submissions = reddit.redditor(who).submissions.new()
except:
print("who is {}".format(who))
continue
try:
if who in fail:
del(fail[who])
except:
if not who in fail:
fail[who] = 0
fail[who] += 1
print("Woops, no submissions {} ({})".format(who, fail[who]))
with open('fail.json', 'w') as f:
json.dump(fail, f)
continue
ts('presub')
isNew = False
url_seen = set()
try:
submissions = list(submissions)
except:
continue
for entry in submissions:
try:
filename = os.path.basename(entry.url)
except:
logging.debug("Couldn't get path for {}".format(entry.url))
continue
path = "{}/{}".format(content, filename)
"""
if r.hget('ignore', filename) or entry.id in entrylist:
break
"""
entrylist.add(entry.id)
isNew = True
parts = urlparse(entry.url)
url_to_get = entry.url
titlelist.add(entry.title)
if r.hget('ignore', path):
continue
if parts.netloc in gfy_list:
if '.' not in path:
path += '.mp4'
if not entry.url in urllist or (args.redgif and 'redgif' in entry.url) or (args.gallery and 'gallery' in entry.url) or (args.video and 'v.redd' in entry.url):
continue if entry.url in url_seen else url_seen.add(entry.url)
if len(filename) == 0:
titlelist.add(entry.selftext)
addurl(urllist, entry.url, entry)
linklist = re.findall(r'http[^\s\])]*', entry.selftext)
if len(linklist) > 0:
for imgurl in linklist:
try:
urlparts = urlparse(imgurl)
except:
logging.warning("Failed to parse url: {}".format(imgurl))
continue
path = "{}/{}".format(content, os.path.basename(urlparts.path))
if not os.path.exists(path) and not r.hget('ignore',path):
try:
remote = get(imgurl)
except:
logging.warning("Cannot grab path {} for text {}".format(imgurl, entry.selftext))
continue
addurl(urllist, imgurl, entry)
try:
with open(path, 'bw') as f:
f.write(remote.read())
print(" \_{}".format(path))
except:
logging.warning("Can't open path {} for text {} to save {}".format(path, entry.selftext, imgurl))
logging.debug("Filename doesn't exist for {}".format(entry.id))
continue
subred = entry.subreddit.display_name
if subred in subblock:
logging.debug("Not grabbing because {} is a blocked sub".format(subred))
continue
subredUser.append([datetime.now().strftime("%Y%m%d"), subred])
remote_temp = None
try:
remote_temp = hasattr(entry, 'is_gallery') and entry.is_gallery and entry.gallery_data is not None
except:
logging.warning("Unable to get gallery for user. Might need to wait. Snoozing a bit")
time.sleep(2)
if remote_temp:
logging.debug("is a gallery")
if os.path.exists(path):
print("<< {}".format(path))
os.unlink(path)
try:
items_temp = entry.media_metadata.items()
except Exception as ex:
logging.warning("Unable to get meta-data: {}".format(ex))
items_temp = {}
for k,v in items_temp:
try:
vs = v.get('s') or {}
if 'u' in vs:
imgurl = vs['u']
elif 'gif' in vs:
imgurl = vs['gif']
else:
print(" woops! Can't find an image: {}".format(v['s']))
continue
except:
print(" woops! Can't find an image".format(json.dumps(v)))
continue
urlparts = urlparse(imgurl)
path = "{}/{}".format(content, urlparts.path[1:])
if not os.path.exists(path) and not r.hget('ignore',path):
remote = get(imgurl)
addurl(urllist, imgurl, entry)
with open(path, 'bw') as f:
f.write(remote.read())
print(" \_{}".format(path))
addurl(urllist, entry.url, entry)
continue
print(" \_{}".format(path))
if hasattr(entry, 'is_video') and entry.is_video and entry.secure_media is not None:
url_to_get = entry.secure_media['reddit_video']['fallback_url']
# this is a lie, but eh so what
path += '.mp4'
if os.path.exists(path):
continue
print(" \_{}".format(url_to_get))
elif parts.netloc in ['imgur.com','i.imgur.com']:
noext = os.path.splitext(parts.path)[0]
pieces = noext.strip('/').split('/')
try:
if pieces[0] == 'a':
for x in imgur.get_album_images(pieces[1]):
url_to_get = x.link
else:
obj = imgur.get_image(pieces[0])
url_to_get = obj.link
except:
print(" \_ Unable to get {}".format(entry.url))
r.hset('ignore', path, "na")
r.hset('ignore', filename, "na")
continue
hasext = os.path.splitext(path)
if not hasext[1]:
ext = os.path.splitext(url_to_get)[1]
path += ext
print(" \_{}".format(url_to_get))
elif parts.netloc in gfy_list:
url_path = parts.path.split('/')
obj = None
to_get = url_path[-1]
to_get = re.sub('i.redgifs.com/i/([^\.]*).*',r'www.redgifs.com/watch/\1',to_get)
to_get = re.sub('.jpg','',to_get)
if not os.path.exists(path):
url_to_get = entry.url
print(" \_{}".format(url_to_get))
subprocess.run(['yt-dlp', 'https://redgifs.com/watch/{}'.format(to_get), '-o', path], capture_output=True)
addurl(urllist, entry.url, entry)
continue
try:
request = urllib.request.Request(url_to_get, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'})
remote = urllib.request.urlopen(request)
with open(path, 'bw') as f:
f.write(remote.read())
addurl(urllist, entry.url, entry)
except Exception as ex:
print(" woops, can't get {} ({} -> {}): {}".format(entry.url, url_to_get, path, ex))
r.hset('ignore', path, "na")
r.hset('ignore', filename, "na")
continue
else:
logging.debug("Exists: {}".format(filename))
if not os.path.exists(path):
attempt = glob("{}.*".format(path))
if len(attempt) > 0:
path = attempt[0]
if os.path.isfile(path):
cksumcheck(path, who=who)
if isNew:
ts('pre comment pull')
try:
for entry in reddit.redditor(who).comments.new():
if entry.id in commentMap:
break
commentMap[entry.id] = entry.body
subred = entry.subreddit.display_name
subredUser.append([datetime.now().strftime("%Y%m%d"), subred])
except Exception as ex:
print("comment issues for {} {}".format(who, ex))
continue
ts('prefile')
with open("{}/commentmap.txt".format(content), 'w') as f:
json.dump(commentMap, f)
with open("{}/entrylist.txt".format(content), 'w') as fp:
fp.write('\n'.join(list(entrylist)))
with open("{}/urllist.txt".format(content), 'w') as fp:
fp.write('\n'.join(list(urllist)))
with open("{}/subredditlist.txt".format(content), 'w') as fp:
json.dump(subredUser, fp)
with open("{}/titlelist.txt".format(content), 'w') as fp:
fp.write('\n'.join(list(titlelist)))
for i in ['fail']:
with open(i + '.json', 'w') as f:
json.dump(globals().get(i), f)
ts('done')