forked from TomoeMami/S1PlainTextBackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s1.py
308 lines (287 loc) · 13.5 KB
/
s1.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env/ python3
import requests
from bs4 import BeautifulSoup
import re
import time
import sys
import io
import os,shutil
import json
import math
import asyncio,aiohttp
# sys.excepthook = lambda *args: exit(1) #只有在出现exit(1)的情况时才终止,debug时不要用
def mkdir(path):
# 去除首位空格
path=path.strip()
# 去除尾部 \ 符号
path=path.rstrip("\\")
path=path.encode('utf-8')
# 判断路径是否存在
# 存在 True
# 不存在 False
isExists=os.path.exists(path)
# 判断结果
if not isExists:
os.makedirs(path)
return True
else:
# 如果目录存在则不创建,并提示目录已存在
return False
def parse_html(html):
# soup = BeautifulSoup(html,from_encoding="utf-8",features="lxml")
soup = BeautifulSoup(html, 'html.parser')
# print(soup)
namelist = soup.find_all(name="div", attrs={"class":"pi"})
# replylist = soup.find_all(name="td", attrs={"class":"t_f"})
replylist = soup.find_all(name='div', attrs={"class":"pcb"})
# next_page = soup.find('a', attrs={'class': 'nxt'})
# if next_page:
# return soupname, souptime, next_page['herf']
title = soup.find_all(name='span',attrs={"id":"thread_subject"})
total_page = int((re.findall(r'<span title="共 (\d+) 页">', str(soup)) + [1])[0])
titles = re.sub(r'<.+?>','',str(title))
titles = re.sub(r'[\]\[]','',titles)
titles = re.sub(r'\|','|',titles)
titles = re.sub(r'/','/',titles)
titles = re.sub(r'\\','\',titles)
titles = re.sub(r':',':',titles)
titles = re.sub(r'\*','*',titles)
titles = re.sub(r'\?','?',titles)
titles = re.sub(r'"','"',titles)
titles = re.sub(r'<','<',titles)
titles = re.sub(r'>','>',titles)
titles = re.sub(r'\.\.\.','…',titles)
titles = re.sub(r'\n',' ',titles)
titles = '['+titles+']'
return namelist,replylist,total_page,titles
# \d{4}-\d{1}-\d{1}\s\d{2}\:\d{2}
# def addtimestamp(filedir,lasttimestamp):
# with open(filedir, 'r+',encoding='UTF-8') as f:
# content = f.read()
# f.seek(0, 0)
# f.write('> ## **本文件最后更新于'+lasttimestamp+'** \n\n'+content)
def thread_dict(thdir,thdict):
with open(thdir,'r',encoding='UTF-8-sig') as f:
lines = f.readlines()
a = ''
for line in lines:
a += line
b = a.split("*****")
for i in b[1::]:
if(re.findall(r'#####\s(\d+)#',i)[0]):
thdict[re.findall(r'\n#####\s(\d+)#',i)[0]] = i
def thread_merge(oridir,desdir):
ori = {}
des = {}
thread_dict(oridir,ori)
if os.path.exists(desdir):
thread_dict(desdir,des)
result = ''
for i in sorted(list(set(ori.keys())-set(des.keys()))):
result = result + ("*****") + ori[i]
with open (desdir,'a',encoding='UTF-8-sig')as f:
f.write(result)
os.remove(oridir)
def get_FileSize(filePath):
fsize = os.path.getsize(filePath)
fsize = fsize/float(1024 * 1024)
return round(fsize, 2)
def get_dir_files(dir_path):
file_list = os.listdir(dir_path)
result = []
for i in file_list:
result.append(os.path.join(dir_path, i))
return result
def FormatStr(namelist, replylist,totalreply):
nametime = []
replys = []
times = []
output= ''
replynumber = []
lastreply = totalreply
for i in namelist:
i = re.sub(r'[\r\n]',' ',str(i))
nametime.append(re.sub(r'<.+?>','',i))
names = nametime[::2]
timestamp = nametime[1::2]
for i in timestamp:
i = re.sub(r'[\r\n]',' ',str(i))
i = re.sub(r'电梯直达','1#',i)
j = re.search(r'\d+[\S\s]+发表于\s\d+-\d+-\d+\s\d+:\d+',i)
k = re.search(r'\d+',i)
# if('#444' in i):
# l = re.search(r'来自<span.+</span>',i)
# m = re.sub(r'<span style=color:#444>','',l.group(0))
# m = re.sub(r'</span>','',m)
# #k = re.search(r'\d+', k.group(0))
# #正则搜索返回的是正则match object
# times.append(j.group(0)+m)
# else:
times.append(j.group(0))
replynumber.append(int(k.group(0)))
for i in replylist:
i = re.sub(r'\r','\n',str(i))
# i = re.sub(r'\n\n','\n',i)
i = re.sub(r'<blockquote>','[[[[blockquote]]]]',i)
i = re.sub(r'</blockquote>','[[[[/blockquote]]]]',i)
# i = re.sub(r'</blockquote>','\n',i)
i = re.sub(r'<strong>','[[[[strong]]]]',i)
i = re.sub(r'</strong>','[[[[/strong]]]]',i)
# i = re.sub(r'</strong>','** ',i)
i = re.sub(r'<span class=\"icon_ring vm\">','﹍﹍﹍\n\n',str(i))
i = re.sub(r'<td class="x.1">','|',i)
i = re.sub(r'\n</td>','',i)
i = re.sub(r'</td>\n','',i)
i = re.sub(r'<div class="modact">(.+?)</div>','\n\n *\\1* \n\n',i)
i = re.sub(r'<a href="http(.+?)" target="_blank">(.+?)</a>','[\\2](http\\1)',i)
i = re.sub(r'<img alt=\".*?\" border=\"\d+?\" smilieid=\"\d+?\" src=\"','[[[[img src="',i)
i = re.sub(r'"/>','"/)',i)
i = re.sub(r'<img .*?file="','[[[[img src="',i)
i = re.sub(r'jpg".+\)','jpg" referrerpolicy="no-referrer"]]]]',i)
i = re.sub(r'png".+\)','png" referrerpolicy="no-referrer"]]]]',i)
i = re.sub(r'gif".+\)','gif" referrerpolicy="no-referrer"]]]]',i)
i = re.sub(r'jpeg".+\)','jpeg" referrerpolicy="no-referrer"]]]]',i)
i = re.sub(r'webp".+\)','webp" referrerpolicy="no-referrer"]]]]',i)
i = re.sub(r'tif".+\)','tif" referrerpolicy="no-referrer"]]]]',i)
i = re.sub(r'<.+?>','',i)
i = re.sub(r'\n(.*?)\|(.*?)\|(.*?)\n','\n|\\1|\\2|\\3|\n',i)
i = re.sub(r'收起\n理由','|昵称|战斗力|理由|\n|----|---|---|',i)
i = re.sub(r'\|\n+?\|','|\n|',i)
i = re.sub(r'\[\[\[\[','<',i)
i = re.sub(r'\]\]\]\]','>',i)
i = re.sub(r'\[(.+?发表于.+?\d)\]\((http.+?)\)','<a href="http\\2" target="_blank">\\1</a>',i)
replys.append(i)
for i in range(len(replylist)):
if(lastreply < replynumber[i]):
output = output + '\n*****\n\n' +'#### '+str(names[i]) + '\n##### '+str(times[i]) + '\n'+str(replys[i] ) +'\n'
output = re.sub(r'\r','\n',output)
output = re.sub(r'\n{2,}','\n\n', output)
lastreply = replynumber[-1]
return output,lastreply
with open ('/home/riko/s1cookie-1.txt','r',encoding='utf-8') as f:
cookie_str1 = f.read()
cookie_str = repr(cookie_str1)[1:-1]
# #把cookie字符串处理成字典,以便接下来使用
cookies = {}
for line in cookie_str.split(';'):
key, value = line.split('=', 1)
cookies[key] = value
# 设置请求头
headers = {'User-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'}
rootdir="/home/riko/S1PlainTextBackup/"
with open(rootdir+'RefreshingData.json',"r",encoding='utf-8-sig') as f:
thdata=json.load(f)
async def UpdateThread(threaddict,semaphore):
# async def UpdateThread(threaddict):
try:
if not (threaddict['update']):
if((int(time.time()) - thdata[threaddict['id']]['lastedit']) > 259200):
#3天过期
titles = threaddict['title']
if(thdata[threaddict['id']]['totalreply'] > 37*40):
filedir_src = rootdir+thdata[threaddict['id']]['category']+'/'+str(threaddict['id'])+titles
else:
filedir_src = rootdir+thdata[threaddict['id']]['category']+'/'+str(threaddict['id'])+'-01'+titles+'.md'
filename_des = re.sub(r'S1PlainTextBackup','S1PlainTextArchive2024',filedir_src)
if os.path.exists(filename_des):
if os.path.isdir(filedir_src):
filedir_src_list = get_dir_files(filedir_src)
for i in filedir_src_list:
j = re.sub(r'S1PlainTextBackup','S1PlainTextArchive2024',i)
thread_merge(i,j)
else:
thread_merge(filedir_src,filename_des)
else:
filedir_des = '/home/riko/S1PlainTextArchive2024/' +thdata[threaddict['id']]['category']+'/'
mkdir(filedir_des)
shutil.move(filedir_src,filedir_des)
thdata[threaddict['id']]['active'] = False
else:
thdata[threaddict['id']]['update'] = False
async with semaphore:
lastpage = threaddict['totalreply']//40
async with aiohttp.ClientSession(headers=headers,cookies=cookies) as session:
url = 'https://bbs.saraba1st.com/2b/thread-'+threaddict['id']+'-1-1.html'
async with session.get(url,headers=headers) as response:
result = await response.content.read()
namelist, replylist,totalpage,newtitles= parse_html(result)
titles = threaddict['title']
thdata[threaddict['id']]['newtitle'] = newtitles
# else:
if(totalpage > 37):
filedir = rootdir+thdata[threaddict['id']]['category']+'/'+str(threaddict['id'])+titles+'/'
mkdir(filedir)
else:
filedir = rootdir+thdata[threaddict['id']]['category']+'/'
#为了确保刚好有50页时能及时重新下载而不是直接跳至51页开始
conn =aiohttp.TCPConnector(limit=3)
contentdict = {}
async with semaphore:
async with aiohttp.ClientSession(connector=conn,headers=headers,cookies=cookies) as session:
for thread in range(lastpage+1,totalpage+1):
rurl = 'https://bbs.saraba1st.com/2b/thread-'+threaddict['id']+'-'+str(thread)+'-1.html'
# rresult = rsession.get(rurl, headers=headers, cookies=cookies)
# rdata = rresult.content
async with session.get(rurl) as response:
rdata = await response.content.read()
rnamelist, rreplylist,rtotalpage,rnewtitles= parse_html(rdata)
ThreadContent,lastreply= FormatStr(rnamelist, rreplylist,threaddict['totalreply'])
contentdict[str(lastreply)] = {}
contentdict[str(lastreply)]['content'] = ThreadContent
contentdict[str(lastreply)]['page'] = thread
if (contentdict.keys()):
# print(threaddict['id'])
if(min(list(map(int,contentdict.keys()))) > threaddict['totalreply']):
print(threaddict['id']+'-'+str(contentdict.keys()))
for replynum in sorted(list(map(int,contentdict.keys()))):
#lastsave=time.strftime('%Y-%m-%d %H:%M',time.localtime(time.time()+28800))#把GithubAction服务器用的UTC时间转换为北京时间
#增量更新不再创建时间戳
pages = '%02d' %math.ceil(contentdict[str(replynum)]['page']/37)
filename = str(threaddict['id'])+'-'+str(pages)+titles+'.md'
with open((filedir+filename).encode('utf-8'),'a',encoding='utf-8-sig') as f:
f.write(contentdict[str(replynum)]['content'])
thdata[threaddict['id']]['totalreply'] = max(list(map(int,contentdict.keys())))
thdata[threaddict['id']]['lastedit'] = int(time.time())
thdata[threaddict['id']]['title'] = titles
with open(rootdir+'RefreshingData.json',"w",encoding='utf-8-sig') as f:
f.write(json.dumps(thdata,indent=2,ensure_ascii=False))
except Exception as e:
# with open(rootdir+'ErrorLog.txt','a',encoding='utf-8') as f:
# f.write(str(e)+'\n')
# f.write('!!error:id='+threaddict['id']+'\n')
pass
async def main():
tasks = []
threaddicts = {}
semaphore = asyncio.Semaphore(12)
for tid in thdata.keys():
if(thdata[tid]['active']):
threaddicts[tid] = {}
threaddicts[tid]['id'] = tid
threaddicts[tid]['totalreply'] = int(thdata[tid]['totalreply'])
threaddicts[tid]['title'] = thdata[tid]['title']
threaddicts[tid]['update'] = thdata[tid]['update']
for thread in threaddicts.keys():
tasks.append(UpdateThread(threaddicts[thread],semaphore))
# tasks.append(UpdateThread(threaddicts[thread]))
await asyncio.gather(*tasks)
if __name__ == '__main__':
# with open(rootdir+'ErrorLog.txt','w',encoding='utf-8-sig') as f:
# f.write('\n')
asyncio.run(main())
for dir_prefix in ["外野/","手游专楼/","游戏区/","漫区/","虚拟主播区专楼/"]:
subdir = rootdir + dir_prefix
for item in os.listdir(subdir):
if not str(item).endswith(".md"):
name_list = str(item).split("[")
#找到有未挪入文件夹的01存档文件
old_path = subdir+name_list[0]+"-01["+name_list[1]+".md"
if(os.path.exists(old_path)):
print("正在合并:"+ old_path)
new_path = subdir+item+"/"+name_list[0]+"-01["+name_list[1]+".md"
#如果里面已经有个01了,把里面的新内容移到外面旧文件中
if(os.path.exists(new_path)):
with open(new_path,"r",encoding='utf-8-sig') as n, open(old_path,"a",encoding='utf-8-sig') as o:
o.write(n.read())
#把旧文件放到里面去
os.rename(old_path,new_path)