/
crawler.py
151 lines (120 loc) · 4.44 KB
/
crawler.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
import os
import datetime
import json
import re
import time
from pathlib import Path
from typing import List
import traceback
import schedule
import PyRSS2Gen
import requests
import tqdm
from bs4 import BeautifulSoup
BASE_URL = 'https://n-notes.tkzt.cn'
ARCHIVES = ['/notes', '/boring-plans', '/cheap-talks']
MAX_ABSTRACT = 150
DIST_DIR = 'dist'
RSS_FILENAME = 'feed.xml'
BLOGS_JSON_FILENAME = 'blogs.json'
class proxy:
def __enter__(self):
os.system('/etc/clash/start.sh start')
def __exit__(self, *args):
os.system('/etc/clash/start.sh stop')
def exec_with_retrying(fn, retry_times=3, retry_interval=3.7, message_when_error=''):
retrying = 0
result = None
while retrying<=retry_times:
try:
result = fn()
break
except:
print(message_when_error)
traceback.print_exc()
time.sleep(retry_interval)
retrying += 1
return result
def crawl() -> List[dict]:
articles = []
proxies = {
'http': 'http://127.0.0.1:7890',
'https': 'http://127.0.0.1:7890',
}
for archive in ARCHIVES:
print(f'\nCrawling {archive}..')
response = exec_with_retrying(
lambda :requests.get(BASE_URL + archive, proxies=proxies),
message_when_error=f'Failed to fetch {archive}'
)
if response:
soup = BeautifulSoup(response.text, "html.parser")
for link in tqdm.tqdm(soup.find_all('a')):
href = link['href']
if href.startswith('/') and href.endswith('html'):
full_url = BASE_URL + href
_response = exec_with_retrying(
lambda :requests.get(full_url, proxies=proxies),
message_when_error=f'Failed to fetch {link}'
)
if _response:
_soup = BeautifulSoup(_response.text, 'html.parser')
title = _soup.select_one('.vp-doc > h1')
content = _soup.select_one('main')
date = _soup.select_one('.info .info-text')
if title and content and date:
title_text = re.sub(r'\s#$', '', title.text)
if not any(map(lambda a: a['title']==title_text, articles)):
articles.append({
'title': title_text,
'link': full_url,
'description': content.prettify(),
'pubDate': datetime.datetime(
*map(lambda x: int(x), date.text.split('-'))
) if '-' in date.text else None,
'guid': PyRSS2Gen.Guid(full_url)
})
time.sleep(1.2)
return articles
def gen_rss(articles: List[dict]) -> None:
rss = PyRSS2Gen.RSS2(
title='N Notes',
description='以有涯隨無涯,殆已!已而為知者,殆而已矣!是的,这是一个博客网站。',
link=BASE_URL,
lastBuildDate=datetime.datetime.now(),
items=map(lambda a: PyRSS2Gen.RSSItem(**a), articles),
)
directory = Path(DIST_DIR)
directory.mkdir(exist_ok=True)
with open(directory / RSS_FILENAME, 'w', encoding='utf-8') as f:
rss.write_xml(f, encoding='utf-8')
def gen_json(articles: List[dict]) -> None:
directory = Path(DIST_DIR)
directory.mkdir(exist_ok=True)
with open(directory / BLOGS_JSON_FILENAME, 'w', encoding='utf-8') as f:
f.write(json.dumps(list(map(
lambda a: {
'title': a['title'],
'date': a['pubDate'].strftime('%Y-%m-%d') if a['pubDate'] else '旧时',
'link': a['link']
}, articles)), ensure_ascii=False))
def update_repo():
time.sleep(3.7)
os.system('./update_repo.sh')
def crawling_job():
with proxy():
articles = crawl()
if articles:
gen_rss(articles)
gen_json(articles)
update_repo()
else:
print("Failed to crawl articles.")
def schedule_job():
schedule.every().day.at("12:54").do(crawling_job)
print('Job initialized.')
while True:
schedule.run_pending()
time.sleep(3.7)
if __name__ == '__main__':
schedule_job()