-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.py
230 lines (179 loc) · 8.47 KB
/
main.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
import os
import re
import sys
import traceback
import urllib.request
from datetime import datetime
import requests
from bs4 import BeautifulSoup
from dateutil import tz
from dotenv import load_dotenv
from loguru import logger
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from common.elasticsearch_utils import document_view, document_add
load_dotenv()
from config.conf import DATA_DIR, INDEX_NAME
DOWNLOAD_PATH = os.path.join(DATA_DIR, "mailing-list/bitcoin-dev")
ORIGINAL_URL = "https://gnusha.org/pi/bitcoindev/"
CUSTOM_URL = "https://mailing-list.bitcoindevs.xyz/bitcoindev/"
month_dict = {
1: "Jan", 2: "Feb", 3: "March", 4: "April", 5: "May", 6: "June",
7: "July", 8: "Aug", 9: "Sept", 10: "Oct", 11: "Nov", 12: "Dec"
}
def save_web_page(link, file_name):
main_url = ORIGINAL_URL + link
html_response = requests.get(f"{ORIGINAL_URL}{link}")
soup = BeautifulSoup(html_response.content, 'html.parser')
main_url_anchor = soup.new_tag("a", href=main_url.replace('#t', ''), id='main_url')
soup.body.append(main_url_anchor)
path = os.path.join(DOWNLOAD_PATH, file_name)
with open(path, 'w', encoding='utf-8') as file:
logger.info(f'Downloading {file_name}')
file.write(str(soup))
def download_dumps(path, page_visited_count, max_page_count=2):
if page_visited_count > max_page_count: return
page_visited_count += 1
logger.info(f"Page {page_visited_count}: {path}")
with urllib.request.urlopen(f"{path}") as f:
soup = BeautifulSoup(f, "html.parser")
pre_tags = soup.find_all('pre')
if len(pre_tags) < 1:
return
next_page_link = f"{ORIGINAL_URL}{soup.find('a', {'rel': 'next'}).get('href')}"
for tag in pre_tags[1].find_all('a'):
try:
date = tag.next_sibling.strip()[:7]
date = date.strip().split('-')
# date = tag.next_sibling.strip()[:8]
if len(date) < 2:
continue
year = int(date[0])
mon = int(date[1])
month = month_dict.get(int(date[1]))
if year < 2024 or (year == 2024 and mon == 1):
return
href = tag.get('href')
file_name = f"{year}-{month}-{href.strip().split('/')[0]}.html"
save_web_page(href, file_name)
except Exception as e:
logger.error(e)
logger.error(tag)
continue
logger.info('----------------------------------------------------------\n')
if next_page_link:
download_dumps(next_page_link, page_visited_count)
def get_thread_urls_with_date(pre_tags):
urls_dates = []
date_time_pattern = r'\b\d{4}-\d{2}-\d{2} {1,2}(?:[01]?\d|2[0-3]):[0-5]\d\b'
for pre_tag in reversed(pre_tags):
if "links below jump to the message on this page" in pre_tag.text:
anchor_tags = pre_tag.find_all('a', href=lambda href: href and '#' in href)
for anchor in anchor_tags:
date_search = re.search(date_time_pattern, anchor.previous_sibling.text)
if date_search:
date = date_search.group()
original_datetime = datetime.strptime(date, '%Y-%m-%d %H:%M')
original_datetime = original_datetime.replace(tzinfo=tz.tzutc())
dt = original_datetime.isoformat(timespec='milliseconds').replace('+00:00', 'Z')
urls_dates.append((anchor, dt))
# sort the urls_dates list by datetime in ascending order (earliest first)
urls_dates.sort(key=lambda x: x[1])
return urls_dates
def get_year_month(date):
date = date.strip().split('-')
year = int(date[0])
month = int(date[1])
return year, month
def get_author(text):
from_user = re.search(r'From:\s*(.+?)\s+\@', text).group()
# to_user = re.search(r'To:\s*(.+)', text).group()
author = from_user.replace("From: ", "").replace("@", "").replace("'", "").replace(
"via Bitcoin Development Mailing List", '').strip()
return author
def href_contains_text(tag, search_text):
return tag.name == 'a' and tag.has_attr('href') and search_text in tag['href']
def preprocess_body_text(text):
text = text.replace("[|]", "").strip()
text = re.sub(r'\[not found\] <[^>]+>', "", text)
text = re.sub(re.compile(
r'You received this message because you are subscribed to the Google Groups .+? group.\s+'
r'To unsubscribe from this group and stop receiving emails from it, send an email to .+?\.\s+'
r'To view this discussion on the web visit .+\.',
re.DOTALL
), '', text)
return text
def parse_dumps():
doc = []
for root, dirs, files in os.walk(DOWNLOAD_PATH):
for file in reversed(files):
logger.info(f'parsing : {file}')
with open(f'{os.path.join(root, file)}', 'r', encoding='utf-8') as f:
u = file[9:].replace(".html", "")
html_content = f.read()
soup = BeautifulSoup(html_content, 'html.parser')
# scrape url
main_url = soup.find('a', id='main_url')
main_url = main_url.get('href')
# Scrape title
title = soup.find_all('b')[1].text
title = title.replace("[Bitcoin-development] ", "").replace("[bitcoin-dev] ", "").replace(
"[bitcoindev] ", "").replace("\t", "").strip()
urls_with_date = get_thread_urls_with_date(soup.find_all('pre'))
for index, (url, date) in enumerate(urls_with_date):
try:
year, month = get_year_month(date)
if year < 2024 or (year == 2024 and month == 1):
continue
href = url.get('href')
tag_id = url.get('id')
content = soup.find(lambda tag: tag.name == "pre" and tag.find('a', href=f"#{tag_id}"))
# Scrape Body
for c in content.find_all('b'):
c.decompose()
for c in content.find_all('u'):
c.decompose()
for c in content.find_all(lambda tag: href_contains_text(tag, href.replace("#", "")[1:])):
c.decompose()
for c in content.find_all(lambda tag: href_contains_text(tag, u)):
c.decompose()
# for c in content.find_all(lambda tag: tag.name in {'b', 'u'} or any(
# href_contains_text(tag, text) for text in [href.replace("#", "")[1:],u])):
# c.decompose()
body_text = preprocess_body_text(content.text)
# Scraping author
author = get_author(body_text)
doc_id = f"mailing-list-{year}-{month:02d}-{href.replace('#', '')}"
document = {
"id": doc_id,
"authors": [author],
"title": title,
"body": body_text,
"body_type": "raw",
"created_at": date,
"domain": CUSTOM_URL,
"thread_url": main_url,
"url": f"{main_url}{href}"
}
if index == 0:
document['type'] = "original_post"
else:
document['type'] = "reply"
doc.append(document)
except Exception as e:
logger.info(f"{e} \nORIGINAL_URL: {main_url}\n{traceback.format_exc()}")
continue
return doc
def index_documents(docs):
for doc in docs:
resp = document_view(index_name=INDEX_NAME, doc_id=doc['id'])
if not resp:
_ = document_add(index_name=INDEX_NAME, doc=doc, doc_id=doc['id'])
logger.success(f'Successfully added! ID: {doc["id"]}')
else:
logger.info(f"Document already exist! ID: {doc['id']}")
if __name__ == "__main__":
if not os.path.exists(DOWNLOAD_PATH):
os.makedirs(DOWNLOAD_PATH)
download_dumps(ORIGINAL_URL, page_visited_count=0)
documents = parse_dumps()
index_documents(documents)