-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathschlabber.py
executable file
·428 lines (407 loc) · 19.3 KB
/
schlabber.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
#!/usr/bin/env python3
import os
import stat
import argparse
import requests
import json
import pprint
import hashlib
from bs4 import BeautifulSoup
class Soup:
def assertdir(self,dirname):
if not os.path.isdir(dirname):
os.makedirs(dirname)
def has_valid_timestamp(self, meta):
return meta and 'time' in meta and meta['time']
def __init__(self, soup, bup_dir):
self.rooturl = "http://"+soup+".soup.io"
self.bup_dir = os.path.abspath(bup_dir)
self.assertdir(self.bup_dir)
self.dlnextfound = False
self.sep = os.path.sep
self.session = requests.session()
print("Backup: " + self.rooturl)
print("into: " + self.bup_dir)
def find_next_page(self, cur_page, cur_url):
for script in cur_page.find_all('script'):
if script.string and "SOUP.Endless.next_url" in script.string:
self.dlnextfound = True
url = script.string.split('\'')[-2].strip()
print("\t...found script: " + url)
return url
for more in cur_page.find_all('a', class_='more'):
if more['href'] and "since" in more['href']:
self.dlnextfound=True
url = more['href'].strip()
print("\t...found pagination: " + url)
return url
self.dlnextfound = False
return cur_url
def get_asset_name(self, name):
return name.split('/')[-1].split('.')[0]
def get_timstemp(self, post):
for time_meta in post.find_all("abbr"):
return time_meta.get('title').strip().split(" ")
return None
def process_image(self, post, dlurl):
print("\t\tImage:")
meta = {'soup_page' : dlurl}
meta['time'] = self.get_timstemp(post)
for desc in post.find_all("div", {'class': 'description'}):
meta['text'] = desc.get_text()
for link in post.find_all('div', {"class":"imagecontainer"}):
lightbox = link.find("a", {"class": "lightbox"})
if lightbox:
meta['soup_url'] = lightbox.get('href')
else:
meta['soup_url'] = link.find("img").get('src')
for caption in link.find_all("div", {'class': 'caption'}):
meta['source'] = caption.find('a').get("href")
if 'soup_url' in meta:
basepath = self.bup_dir + self.sep
if self.has_valid_timestamp(meta):
basepath = basepath + meta['time'][2] + self.sep + meta['time'][0] + self.sep
filename = self.get_asset_name(meta['soup_url'])
path = basepath + filename + "." + meta['soup_url'].split(".")[-1]
if os.path.isfile(path) == True:
print("\t\t\tSkip " + meta['soup_url'] + ": File exists")
else:
print("\t\t\tsoup_url: " + meta['soup_url'] + " -> " + path)
self.assertdir(basepath)
r = self.session.get(meta['soup_url'], allow_redirects=True, cookies=self.session.cookies)
with open(path, "wb") as tf:
tf.write(r.content)
self.assertdir(basepath + "meta" + self.sep )
with open(basepath + "meta" + self.sep + filename + ".json", 'w') as outfile:
json.dump(meta, outfile)
def process_quote(self, post):
print("\t\tQuote:")
meta = {}
meta['time'] = self.get_timstemp(post)
body = post.find("span", {"class", 'body'}).get_text()
author = post.find("cite").get_text()
quote = '"' + body + '"' + "\n\t" + author + "\n"
qhash = hashlib.sha256(quote.encode())
hashsum = str(qhash.hexdigest().upper())
filename = "quote_" + hashsum + ".txt"
basepath = self.bup_dir + self.sep
if self.has_valid_timestamp(meta):
basepath = basepath + meta['time'][2] + self.sep + meta['time'][0] + self.sep
path = basepath + filename
if os.path.isfile(path) == True:
print("\t\t\tSkip: " + filename + ": File exists")
else:
self.assertdir(basepath)
print("\t\t\t-> " + path)
with open(path, "w") as qf:
qf.write(quote)
def process_link(self, post, dlurl):
print("\t\tLink:")
meta = {'soup_page' : dlurl}
meta['time'] = self.get_timstemp(post)
linkelem = post.find("h3")
meta["link_title"] = linkelem.get_text().strip()
meta["url"] = linkelem.find('a').get('href')
meta["text"] = post.find('span', {'class','body'}).get_text().strip()
qhash = hashlib.sha256(meta["url"].encode())
hashsum = str(qhash.hexdigest().upper())
filename = "dl-link_" + hashsum + ".sh"
basepath = self.bup_dir + self.sep
if self.has_valid_timestamp(meta):
basepath = basepath + meta['time'][2] + self.sep + meta['time'][0] + self.sep
path = basepath + filename
if os.path.isfile(path) == True:
print("\t\t\tSkip: " + filename + ": File exists")
else:
self.assertdir(basepath)
print("\t\t\t-> " + path)
filecontent="#! /bin/bash\nwget -c " + meta['url'] + "\n"
with open(path, "w") as df:
df.write(filecontent)
st = os.stat(path)
os.chmod(path, st.st_mode | stat.S_IEXEC)
self.assertdir(basepath + "meta" + self.sep )
with open(basepath + "meta" + self.sep + "dl-link_" + hashsum + ".meta", "w") as jf:
json.dump(meta, jf)
def process_video(self, post, dlurl):
print("\t\tVideo:")
meta = {'soup_page' : dlurl}
meta['time'] = self.get_timstemp(post)
meta['embeded'] = post.find("div", {'class':'embed'}).prettify()
video = post.find("div", {'class':'embed'}).find('video')
if video:
meta['soup_url'] = video.get("src")
if meta['soup_url'] is None and video.find('source'):
for source in video.find_all('source'):
src = source.get("src")
# prefer mp4 (as for my example webm returned 404)
if meta['soup_url'] is None or (src is not None and src.endswith(".mp4")):
meta['soup_url'] = src
bodyelem = post.find("div", {'class':'body'})
if bodyelem:
meta['body'] = bodyelem.get_text().strip()
else:
meta['body'] = "";
if 'soup_url' in meta and meta['soup_url'] is not None:
if meta['soup_url'].startswith("//"):
meta['soup_url'] = "http:" + meta['soup_url']
basepath = self.bup_dir + self.sep
if self.has_valid_timestamp(meta):
basepath = basepath + meta['time'][2] + self.sep + meta['time'][0] + self.sep
filename = self.get_asset_name(meta['soup_url'])
path = basepath + filename + "." + meta['soup_url'].split(".")[-1]
if os.path.isfile(path) == True:
print("\t\t\tSkip " + meta['soup_url'] + ": File exists")
else:
print("\t\t\tsoup_url: " + meta['soup_url'] + " -> " + path)
self.assertdir(basepath)
r = self.session.get(meta['soup_url'], allow_redirects=True, cookies=self.session.cookies)
with open(path, "wb") as tf:
tf.write(r.content)
self.assertdir(basepath + "meta" + self.sep )
with open(basepath + "meta" + self.sep + filename + ".json", 'w') as outfile:
json.dump(meta, outfile)
else:
data = meta['embeded'] + meta['body']
qhash = hashlib.sha256(data.encode())
hashsum = str(qhash.hexdigest().upper())
filename = "video_" + hashsum + ".json"
basepath = self.bup_dir + self.sep
if self.has_valid_timestamp(meta):
basepath = basepath + meta['time'][2] + self.sep + meta['time'][0] + self.sep
path = basepath + filename
if os.path.isfile(path) == True:
print("\t\t\tSkip: " + filename + ": File exists")
else:
self.assertdir(basepath)
print("\t\t\t-> " + path)
with open(path, "w") as vf:
json.dump(meta, vf)
def process_file(self, post, dlurl):
print("\t\tFile:")
meta = {'soup_page' : dlurl}
meta['time'] = self.get_timstemp(post)
linkelem = post.find("h3")
if linkelem:
meta["link_title"] = linkelem.get_text().strip()
meta["soup_url"] = linkelem.find('a').get('href')
meta["text"] = post.find('div', {'class','body'}).get_text().strip()
if 'soup_url' in meta:
filename = meta["soup_url"].split("/")[-1]
else:
filename = "file_unkown"
basepath = self.bup_dir + self.sep
if self.has_valid_timestamp(meta):
basepath = basepath + meta['time'][2] + self.sep + meta['time'][0] + self.sep
path = basepath + filename
if os.path.isfile(path) == True:
print("\t\t\tSkip: " + filename + ": File exists")
else:
if 'soup_url' in meta and meta['soup_url'].startswith('http'):
print("\t\t\tsoup_url: " + meta['soup_url'] + " -> " + path)
self.assertdir(basepath)
r = self.session.get(meta['soup_url'], allow_redirects=True, cookies=self.session.cookies)
with open(path, "wb") as df:
df.write(r.content)
self.assertdir(basepath + "meta" + self.sep )
jsonname = filename.split(".")[0]
with open(basepath + "meta" + self.sep + jsonname + ".json", 'w') as outfile:
json.dump(meta, outfile)
def process_review(self, post, dlurl):
print("\t\tReview:")
meta = {'soup_page' : dlurl}
meta['time'] = self.get_timstemp(post)
for link in post.find_all('div', {"class":"imagecontainer"}):
lightbox = link.find("a", {"class": "lightbox"})
if lightbox:
meta['soup_url'] = lightbox.get('href')
else:
meta['soup_url'] = link.find("img").get('src')
descelem = post.find("div", {'class','description'})
if descelem:
meta['description'] = descelem.get_text().strip()
meta['rating'] = post.find("abbr", {"class", "rating"}).get("title")
h3elem = post.find("a", {"class":"url"})
meta['url'] = h3elem.get("href")
meta['title'] = h3elem.get_text()
if 'soup_url' in meta:
basepath = self.bup_dir + self.sep
if self.has_valid_timestamp(meta):
basepath = basepath + meta['time'][2] + self.sep + meta['time'][0] + self.sep
filename = "review_" + self.get_asset_name(meta['soup_url'])
path = basepath + filename + "." + meta['soup_url'].split(".")[-1]
if os.path.isfile(path) == True:
print("\t\t\tSkip " + meta['soup_url'] + ": File exists")
else:
print("\t\t\tsoup_ulr: " + meta['soup_url'] + " -> " + path)
self.assertdir(basepath)
r = self.session.get(meta['soup_url'], allow_redirects=True, cookies=self.session.cookies)
with open(path, "wb") as tf:
tf.write(r.content)
self.assertdir(basepath + "meta" + self.sep )
with open(basepath + "review_" + filename + ".json", 'w') as outfile:
json.dump(meta, outfile)
def process_event(self, post, dlurl):
print("\t\tEvent:")
meta = {'soup_page' : dlurl}
meta['time'] = self.get_timstemp(post)
for link in post.find_all('div', {"class":"imagecontainer"}):
lightbox = link.find("a", {"class": "lightbox"})
if lightbox:
meta['soup_url'] = lightbox.get('href')
else:
meta['soup_url'] = link.find("img").get('src')
h3elem = post.find("a", {"class":"url"})
meta['url'] = h3elem.get("href")
meta['title'] = h3elem.get_text()
meta['dtstart'] = post.find("abbr", {'class':'dtstart'}).get("title")
dtelem = post.find("abbr", {'class':'dtend'})
if dtelem:
meta['dtend'] = dtelem.get("title")
meta['location'] = post.find("span", {'class':'location'}).get_text().strip()
meta['ical_url'] = post.find("div", {'class': 'info'}).find('a').get('href')
descelem = post.find("div", {'class','description'})
if descelem:
meta['description'] = descelem.get_text().strip()
if 'soup_url' in meta:
basepath = self.bup_dir + self.sep
if self.has_valid_timestamp(meta):
basepath = basepath + meta['time'][2] + self.sep + meta['time'][0] + self.sep
filename = "event_" + self.get_asset_name(meta['soup_url'])
path = basepath + filename + "." + meta['soup_url'].split(".")[-1]
if os.path.isfile(path) == True:
print("\t\t\tSkip " + meta['soup_url'] + ": File exists")
else:
print("\t\t\tsoup_ulr: " + meta['soup_url'] + " -> " + path)
self.assertdir(basepath)
r = self.session.get(meta['soup_url'], allow_redirects=True, cookies=self.session.cookies)
with open(path, "wb") as tf:
tf.write(r.content)
i = self.session.get(meta['ical_url'], allow_redirects=True, cookies=self.session.cookies)
with open(basepath + filename + ".ical", "wb") as icf:
icf.write(i.content)
self.assertdir(basepath + "meta" + self.sep )
with open(basepath + "meta" + self.sep + filename + ".json", 'w') as outfile:
json.dump(meta, outfile)
def process_regular(self, post):
print("\t\tRegular:")
meta = {}
meta['time'] = self.get_timstemp(post)
h3elem = post.find("h3")
title = ""
if h3elem:
title = h3elem.get_text().strip()
body = post.find("div", {'class':'body'}).get_text().strip()
content = title + "\n" + body + "\n"
qhash = hashlib.sha256(content.encode())
hashsum = str(qhash.hexdigest().upper())
filename = "regular_" + hashsum + ".txt"
basepath = self.bup_dir + self.sep
if self.has_valid_timestamp(meta):
basepath = basepath + meta['time'][2] + self.sep + meta['time'][0] + self.sep
path = basepath + filename
if os.path.isfile(path) == True:
print("\t\t\tSkip: " + filename + ": File exists")
else:
self.assertdir(basepath)
print("\t\t\t-> " + path)
with open(path, "w") as rf:
rf.write(content)
def process_unkown(self, post, post_type, dlurl):
print("\t\tUnsuported tpye:")
print("\t\t\tType: " + post_type)
meta = {'soup_page' : dlurl}
meta['type'] = post_type
meta['time'] = self.get_timstemp(post)
content = post.prettify()
qhash = hashlib.sha256(content.encode())
hashsum = str(qhash.hexdigest().upper())
meta['content'] = content
filename = "unknown_" + hashsum + ".txt"
basepath = self.bup_dir + self.sep
if self.has_valid_timestamp(meta):
basepath = basepath + meta['time'][2] + self.sep + meta['time'][0] + self.sep
path = basepath + filename
if os.path.isfile(path) == True:
print("\t\t\tSkip: " + filename + ": File exists")
else:
print("\t\t\t-> " + path)
self.assertdir(basepath)
with open(path, "w") as uf:
json.dump(meta, uf)
def process_posts(self, cur_page, dlurl):
posts = cur_page.find_all('div', {"class": "post"})
for post in posts:
post_type = post.get('class')[1]
if post_type == "post_image":
self.process_image(post, dlurl)
elif post_type == "post_quote":
self.process_quote(post)
elif post_type == "post_video":
self.process_video(post, dlurl)
elif post_type == "post_link":
self.process_link(post, dlurl)
elif post_type == "post_file":
self.process_file(post, dlurl)
elif post_type == "post_review":
self.process_review(post, dlurl)
elif post_type == "post_event":
self.process_event(post, dlurl)
elif post_type == "post_regular":
self.process_regular(post)
else:
self.process_unkown(post, post_type, dlurl)
def ack_if_nsfw(self, nsfw_page, dlurl):
form = nsfw_page.find("form")
cw_site = '/override_content_warning'
if form:
if form.get("action") == cw_site:
print("Soup is NSFW")
payload = {}
inputs = form.find_all("input")
for inputval in inputs:
payload[inputval.get("name").strip()] = inputval.get("value").strip()
print("Send Payload:\n" + str(payload))
dl = self.session.post(self.rooturl + cw_site, data=payload)
return nsfw_page
def backup(self, cont_url = "", max_retries = "10"):
maxretrycount = max_retries
retrycount = 0
dlurl = self.rooturl + cont_url
old_url = ""
while retrycount < maxretrycount:
print("Get: " + dlurl)
dl = self.session.get(dlurl, allow_redirects=True, cookies=self.session.cookies)
pprint.pprint(self.session.cookies.get_dict())
if dl.status_code == 200:
page = self.ack_if_nsfw(BeautifulSoup(dl.content, 'html.parser'), dlurl)
if dlurl != old_url:
print("Process Posts")
self.process_posts(page, dlurl)
print("Looking for next Page")
old_url = dlurl
extension = self.find_next_page(page, dlurl)
if extension.startswith("http"):
dlurl = extension
else:
dlurl = self.rooturl + extension
else:
self.dlnextfound = False
print("Failed with Status Code: " + str(dl.status_code))
if self.dlnextfound == False:
retrycount=retrycount+1
print("no next found. retry {} of {}".format(retrycount, maxretrycount))
else:
retrycount = 0
def main(soups, bup_dir, cont_from, max_retries):
for site in soups:
soup = Soup(site, bup_dir)
soup.backup(cont_from, max_retries)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Soup.io backup')
parser.add_argument('soups', nargs=1, type=str, default=None, help="Name your soup")
parser.add_argument('-d','--dir', default=os.getcwd(), help="Directory for Backup (default: Working dir)")
parser.add_argument('-c', '--continue_from', default="", help='Continue from given suburl (Example: /since/696270106?mode=own)')
parser.add_argument('-r', '--retries', type=int, default=10, help="Set the maximum number of retries (default: 10)")
args = parser.parse_args()
main(args.soups, args.dir, args.continue_from, args.retries)