-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_get_oldsite.py
290 lines (269 loc) · 9.51 KB
/
main_get_oldsite.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
import json
from logging import log
from os import EX_OSFILE
from time import sleep
from bs4 import BeautifulSoup
import waybackpy
import requests
from urllib.parse import urljoin, urlparse
import markdownify
import shutil
import datetime
import re
import os
reg = r"(https:\/\/web\.archive\.org\/web\/[^/]*\/|\/web\/[^/]*\/)(.*)"
def getURL(url):
sleep(4)
response = requests.get(url, timeout=(3.05, 27))
return response
def getURLSite(url):
user_agent = ""
wayback = waybackpy.Url(url, user_agent)
if wayback.archive_url is None:
return None
print(wayback)
response = getURL(wayback)
return response
urls = []
walk_page = []
def page_walker(url):
pages = {}
try:
response = getURL(url)
except:
return pages
if response.status_code == 404:
m = re.search(reg, url)
response = getURLSite(m.group(2))
if response is None:
return pages
if response.status_code == 404:
return pages
if url in walk_page:
return[]
walk_page.append(url)
soup = BeautifulSoup(response.content, 'html.parser')
for content in soup.select("#content-primary .content"):
post_title = content.select("a")[0]
summery = content.select(".views-field-body")
name = content.select(".views-field-title")
if len(summery) >= 1:
summery = summery[0].prettify()
else:
summery = ""
if len(name) >= 1:
name = name[0].get_text()
else:
name = ""
ur = urljoin(url, post_title['href'])
m = re.search(reg, ur)
ur_test = urlparse(m.group(2)).path
if ur_test in urls:
continue
data, t = passAcartal(ur, summery, name)
if t:
pages[urlparse(m.group(2)).path] = data
else:
if m.group(2) not in pages.keys():
pages[urlparse(m.group(2)).path] = data
for pager in soup.select(".pager a"):
ur = urljoin(url, pager['href'])
if ur in urls:
continue
urls.append(ur)
p = page_walker(ur)
for px in p.keys():
pages[px] = p[px]
return pages
def passAcartal(url, summery, name):
m = re.search(reg, url)
doc = {
"state": "summery_only",
"summery": summery,
"url": m.group(2),
"where": url,
"title": name,
}
m = re.search(reg, url)
ur_test = urlparse(m.group(2)).path
m = re.search(reg, url)
print("found artical at:", url)
response = getURL(url)
if response.status_code == 404:
response = getURLSite(m.group(2))
if response is None:
print("return: ", 0)
return doc, False
if response.status_code == 404:
print("return: ", 1)
print(doc)
return doc, False
else:
urls.append(ur_test)
soup = BeautifulSoup(response.content, 'html.parser')
for content in soup.select("#content-primary"):
f_doc = {
"state": "full",
"title": "",
"body": "",
"summery": markdownify.markdownify(summery, heading_style="ATX"),
"tags": [],
"source": [],
"url": m.group(2),
"where": url
}
links = content.select("a")
for i in links:
try:
ms = re.search(reg, i["href"])
if ".pirateparty.org.uk" in i["href"]:
i["href"] = urlparse(ms.group(2)).path
else:
i["href"] = ms.group(2)
except:
pass
links = content.select("img")
for i in links:
try:
p = urlparse(ms.group(2)).path
filePath = os.path.join("./oldsite",p)
ms = re.search(reg, i["href"])
if ".pirateparty.org.uk" in i["href"]:
response = getURL(url)
if response.status_code == 404:
response = getURLSite(ms.group(2))
if response.status_code == 404 or response is None:
pass
else:
with open(filePath, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
del response
else:
with open(filePath, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
del response
i["href"] = urlparse(ms.group(2)).path
else:
i["href"] = ms.group(2)
except:
pass
title = content.select(".page-title,.page-title,h1")
if len(title) > 0:
f_doc["title"] = title[0].get_text().replace("\n", "")
body = content.select(".field-name-body")
if len(body) > 0:
f_doc["body"] = markdownify.markdownify(
body[0].prettify(), heading_style="ATX")
tags = content.select(".field-name-field-tags > .field-items a")
for tag in tags:
f_doc["tags"].append(tag.get_text())
sources = content.select(".field-name-field-source-link a")
for source in sources:
f_doc["source"].append(source["href"])
date = content.select(
".field-name-field-date-of-report span,.submitted span")
if len(date) > 0:
try:
f_doc["date"] = date[0]["content"]
except:
pass
date = content.select(".submitted span")
if len(date) > 0:
try:
f_doc["date"] = date[0]["content"]
except:
pass
contact_name = content.select(".field-name-field-contact-name")
if len(contact_name) > 0:
f_doc["name"] = contact_name[0].get_text().replace("\n", "")
email = content.select(".field-name-field-contact-email a")
if len(email) > 0:
try:
f_doc["email"] = email[0].get_text()
except:
pass
dateline = content.select(".field-name-field-dateline span")
print(len(dateline))
if len(dateline) > 0:
f_doc["date"] = dateline[0]["content"]
dateline = content.select(".field-name-field-release-date span")
print(len(dateline))
if len(dateline) > 0:
f_doc["date"] = dateline[0]["content"]
print(f_doc)
print("return: ", 2)
return f_doc, True
return doc, True
docs_output = {}
f = [
"https://web.archive.org/web/20190414152001/https://www.pirateparty.org.uk/",
"https://web.archive.org/web/20190414152001/https://www.pirateparty.org.uk/blog",
"https://web.archive.org/web/20190414152001/https://www.pirateparty.org.uk/party/press-hits",
"https://web.archive.org/web/20190414152001/https://www.pirateparty.org.uk/press-release",
"https://web.archive.org/web/20201029132944/https://legacy.pirateparty.org.uk/",
"https://web.archive.org/web/20201029140955/https://legacy.pirateparty.org.uk/party/press-hits",
"https://web.archive.org/web/20201022204013/https://legacy.pirateparty.org.uk/press-release",
"https://web.archive.org/web/20190414153153/https://www.legacy.pirateparty.org.uk/blog",
"https://web.archive.org/web/20170609224310/https://www.pirateparty.org.uk/press-release"
]
for i in f:
p = page_walker(i)
for x in p.keys():
if p[x]["state"] == "full":
docs_output[x] = p[x]
else:
if x not in docs_output.keys():
docs_output[x] = p[x]
jso = json.dumps(docs_output)
f = open("legacy_pirateparty.json", "w")
f.write(jso)
f.close()
def recoder(content, doc={}):
summery = content.select(".views-field-body")
if len(summery) >= 1:
doc["summery"] = summery[0].prettify()
name = content.select(".views-field-title")
if len(name) >= 1:
doc["name"] = name[0].get_text()
title = content.select(".page-title,.page-title,h1")
if len(title) > 0:
doc["title"] = title[0].get_text().replace("\n", "")
body = content.select(".field-name-body")
if len(body) > 0:
doc["body"] = markdownify.markdownify(
body[0].prettify(), heading_style="ATX")
tags = content.select(".field-name-field-tags > .field-items a")
for tag in tags:
doc["tags"].append(tag.get_text())
sources = content.select(".field-name-field-source-link a")
for source in sources:
doc["source"].append(source["href"])
date = content.select(
".field-name-field-date-of-report span,.submitted span")
if len(date) > 0:
try:
doc["date"] = date[0]["content"]
except:
pass
date = content.select(".submitted span")
if len(date) > 0:
try:
doc["date"] = date[0]["content"]
except:
pass
contact_name = content.select(".field-name-field-contact-name")
if len(contact_name) > 0:
doc["name"] = contact_name[0].get_text().replace("\n", "")
email = content.select(".field-name-field-contact-email a")
if len(email) > 0:
try:
doc["email"] = email[0].get_text()
except:
pass
dateline = content.select(".field-name-field-dateline span")
if len(dateline) > 0:
doc["date"] = dateline[0]["content"]
dateline = content.select(".field-name-field-release-date span")
if len(dateline) > 0:
doc["date"] = dateline[0]["content"]
return doc