-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathurlscrape.mt2.py
157 lines (122 loc) · 3.58 KB
/
urlscrape.mt2.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
#!/usr/bin/env python3
# SPDX-License-Identifier: CC0-1.0
##
# Scrape the actual PDF url from
# https://wikileaks.org/clinton-emails/emailid/{id}
#
# Stored results in clintonurls.json. This is idempotent, just
# restart it if it fails.
#
# At the end, it will spit out an aria2 input file to download
# everything.
##
import os.path
import urllib.parse
import json
import concurrent.futures
import traceback
import threading
import time
import wikileaks
import shlex
import sqlite3
from typing import Optional
db = sqlite3.Connection('wikileaks.db', check_same_thread=False)
done = False
mutex = threading.Lock()
class Shit(object):
def get_info(self, id: int) -> Optional[wikileaks.HeadInfo]:
pass
def insert(self, info: wikileaks.HeadInfo, cur: sqlite3.Cursor):
pass
class DNC(Shit):
def get_info(self, id: int) -> Optional[wikileaks.HeadInfo]:
return wikileaks.head_dnc_eml(id)
def insert(self, info: wikileaks.HeadInfo, cur: sqlite3.Cursor):
cur.execute('INSERT INTO dnc_emails(id, path, url, original_name, size) VALUES (?, ?, ?, ?, ?)', (
info.id,
'dnc-emails/{0:0>5}_{1}'.format(info.id, info.name),
info.url,
info.name,
info.size
))
class Podesta(Shit):
def get_info(self, id: int) -> Optional[wikileaks.HeadInfo]:
return wikileaks.head_podesta_eml(id)
def insert(self, info: wikileaks.HeadInfo, cur: sqlite3.Cursor):
cur.execute('INSERT INTO podesta_emails(id, path, url, original_name, size) VALUES (?, ?, ?, ?, ?)', (
info.id,
'podesta-emails/{0:0>5}_{1}'.format(info.id, info.name),
info.url,
info.name,
info.size
))
class Clinton(Shit):
def get_info(self, id: int) -> Optional[wikileaks.HeadInfo]:
url = wikileaks.get_clinton_pdf_url(id)
return wikileaks.HeadInfo(id, urllib.parse.urlparse(url).path.lstrip('/'), url, size=None)
def insert(self, info: wikileaks.HeadInfo, cur: sqlite3.Cursor):
cur.execute('INSERT INTO clinton_emails(id, path, url, size) VALUES (?, ?, ?, ?)', info)
def get_url_proc(id: int, shit: Shit, pool: concurrent.futures.Executor):
while True:
try:
info = shit.get_info(id)
print(f'{id}: {info}')
break
except urllib.error.HTTPError as e:
print(f'{id}: Caught exception {e}, retrying')
#pool.submit(get_url_proc, id, pool)
#return
except:
traceback.print_exc()
return
mutex.acquire()
try:
cur = db.cursor()
shit.insert(info, cur)
except:
traceback.print_exc()
return
finally:
cur.close()
mutex.release()
needed_podesta = set()
needed_dnc = set()
needed_clinton = set()
cur = db.cursor()
try:
for i in range(1, wikileaks.COUNT_PODESTA + 1):
if not cur.execute('SELECT id FROM podesta_emails WHERE id = ?', (i,)).fetchone():
needed_podesta.add(i)
for i in range(1, wikileaks.COUNT_DNC + 1):
if not cur.execute('SELECT id FROM dnc_emails WHERE id = ?', (i,)).fetchone():
needed_dnc.add(i)
for i in range(1, wikileaks.COUNT_CLINTON + 1):
if not cur.execute('SELECT id FROM clinton_emails WHERE id = ?', (i,)).fetchone():
needed_clinton.add(i)
finally:
cur.close()
def checkpoint():
while not done:
time.sleep(10)
print('~10 seconds passed, checkpointing')
mutex.acquire()
try:
db.commit()
finally:
mutex.release()
cpthread = threading.Thread(target=checkpoint, daemon=False)
cpthread.start()
pshit = Podesta()
dshit = DNC()
cshit = Clinton()
with concurrent.futures.ThreadPoolExecutor(max_workers=50) as pool:
for i in needed_podesta:
pool.submit(get_url_proc, i, pshit, pool)
for i in needed_dnc:
pool.submit(get_url_proc, i, dshit, pool)
for i in needed_clinton:
pool.submit(get_url_proc, i, cshit, pool)
done = True
cpthread.join()
db.close()