-
Notifications
You must be signed in to change notification settings - Fork 5
/
consume.py
executable file
·64 lines (59 loc) · 1.86 KB
/
consume.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
#!/usr/bin/env python
import glob
import os
import shutil
import subprocess
from datetime import datetime
from models import *
from parsexml import parse_posts, parse_comments, parse_usernames
from utils import *
files = get_files()
# TODO: name sites/id after real url
for file in glob.glob("downloads/**/*.7z", recursive=True):
if "meta" in file:
continue
filename = os.path.basename(file)
code = os.path.splitext(filename)[0]
if filename not in files:
print("{file} doesn't exist on archive.org".format(file=file))
continue
meta = files[filename]
sha1 = file_hash(file)
if sha1 != meta["sha1"]:
print("{file}: hashes don't match".format(file=filename))
continue
alias = Alias.select().where(Alias.url == code)
if len(alias) != 0:
site_id = alias[0].site_id
print(site_id)
site = Site.select().where(Site.id == site_id)[0]
else:
db_element = Site().select().where(Site.url == code)
if len(db_element) == 0:
print("{site} not found in database".format(site=code))
continue
site = db_element[0]
mtime = datetime.fromtimestamp(int(meta["mtime"]))
if site.last_download == mtime:
print("{site} is up to date".format(site=filename))
continue
else:
site.last_download = mtime
site.save()
print(code)
currentdir = os.getcwd()
rawdir = "raw/" + code
chainsdir = "chains/" + code
for dir in [rawdir, chainsdir]:
if not os.path.exists(dir):
os.mkdir(dir)
shutil.copy2(file, rawdir)
os.chdir(rawdir)
print("Start extracting")
subprocess.check_output(["7z", "x", "-aoa", code + ".7z"])
os.chdir(currentdir)
print("Start parsing")
parse_posts(rawdir, rawdir)
parse_comments(rawdir, rawdir)
parse_usernames(rawdir, rawdir)
print("DONE")