Skip to content

Commit

Permalink
修复了因为作品删除而导致的报错。暂时弃用下载队列持久化。大幅度提升下载性能
Browse files Browse the repository at this point in the history
  • Loading branch information
o5-null committed Jun 2, 2023
1 parent ab8f41c commit 59dd56e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
24 changes: 23 additions & 1 deletion core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import time
import json
import pickle#数据持久化

list = []
fin_list = []

brower = requests.Session() #创建浏览器
headers = {
Expand Down Expand Up @@ -72,6 +73,7 @@ def clean_name(strs):
strs = strs.replace('&','')
strs = strs.replace(':',':')
strs = strs.replace('?','?')
strs = strs.replace('|',' ')
# 去除不可见字符
return strs

Expand All @@ -88,7 +90,9 @@ def dic(info):

#获取与修改下载列表
def downlist(set='null'):
global list
if set == 'null':
return list
if os.path.exists('downlist.obj') and os.path.getsize('downlist.obj') > 0:
with open('downlist.obj','rb') as f:
list = pickle.load(f)
Expand All @@ -98,12 +102,15 @@ def downlist(set='null'):
pickle.dump([],f)
return []
else:
list = set
with open('downlist.obj','wb') as f:
pickle.dump(set,f)

#获取与修改完成列表
def finlist(set='null'):
global fin_list
if set == 'null':
return fin_list
if os.path.exists('finlist.obj') and os.path.getsize('finlist.obj') > 0:
with open('finlist.obj','rb') as f:
list = pickle.load(f)
Expand All @@ -113,6 +120,7 @@ def finlist(set='null'):
pickle.dump([],f)
return []
else:
fin_list = set
with open('finlist.obj','wb') as f:
pickle.dump(set,f)

Expand Down Expand Up @@ -153,6 +161,7 @@ def clean_1(data): #获取网站正文文本
#获取txt格式
txt = doc.summary().replace('</p>','\n')#doc.summary()为提取的网页正文但包含html控制符,这一步是替换换行符
txt = re.sub(r'</?\w+[^>]*>', '', txt) #这一步是去掉<****>内的内容
txt = txt.replace('&nbsp;','')
#创个字典存放数据
fine = {}
fine['title'] = title
Expand Down Expand Up @@ -272,6 +281,19 @@ def lofter_api(targetblogid:int,postid:int) -> dict:
info['postid'] = postid
info['status'] = json_answer['meta']['status']#api状态
info['msg'] = json_answer['meta']['msg']#状态信息
#判断作品状态
if str(info['status']) == '4202':#作品被删除
print(info['msg'])
info['status'] = 404
info['title'] = '错误'
info['writer'] = {}
info['writer']['name'] = '错误'#作者名
info['writer']['img'] = ''#作者头图
info['info'] = ''#文章内容
info['type'] = 1#文章类型 1为文档 2为含有图片 3为音乐(?
info['img'] = []
return info

try:#某些特殊玩意根本没有标题参数
info['title'] = json_answer['response']['posts'][0]['post']['title']#文章标题
except:
Expand Down
18 changes: 14 additions & 4 deletions loftersaver_130_GUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@
#开始下载任务
def start_download():
list = core.downlist()#获取下载列表
list = core.dic(list)
fin_list = 0
all_list = len(list)

while len(list) != 0:#当下载任务不为0时
fin_list += 1
lofter_info_down(list[0],set['download'])#下载任务
time.sleep(0.3)
core.del_downlist(list[0])#删除下载完成的任务

#添加到下载完成列表
Expand All @@ -39,7 +43,7 @@ def start_download():
core.finlist(fin)

#刷新下载进度条
value = len(fin) / len(list)
value = fin_list / all_list
out.set_processbar('down_process',value)
list = core.downlist()#刷新下载任务
out.toast('下载任务已清空', position='center', color='success', duration=0)#显示完成通知
Expand Down Expand Up @@ -85,7 +89,7 @@ def show_fin_list():
#加载任务列表
data_list = core.finlist()#获取需要下载的列表
if len(data_list) == 0:#如果列表为空则等待
time.sleep(1)
time.sleep(0.5)
show_fin_list()
show_list = []
#初始化
Expand Down Expand Up @@ -193,6 +197,8 @@ def lofter_post_list(url):
def lofter_print(info):
#清除旧显示内容
out.clear('info')
if str(info['status']) == '404':
return 'out'
with out.use_scope('info'):#进入视频信息域
with out.use_scope('up_info'):#切换到up信息域
face = requests.get(info['writer']['img']).content#缓存图片
Expand Down Expand Up @@ -230,6 +236,10 @@ def lofter_list_print(list:list):
a += 1
print(url)
answer = lofter_down(url)#申请数据
#如果无法获取数据则跳过
if str(answer['status']) == '404':
out.toast('哎呀,有作品无法获取',position='left', color='error', duration=1)#弹出提示
continue
out.set_processbar('get_api',a / len(list))#刷新进度条
info_list.append(answer)
#遍历创建标题列表
Expand Down Expand Up @@ -265,7 +275,7 @@ def lofter_down(url):
if answer['status'] != 200:#如果返回不正常
#弹出错误弹窗
out.toast(answer['msg'],color='error',duration='0')#弹出错误弹窗
return
return answer
return answer


Expand Down

0 comments on commit 59dd56e

Please sign in to comment.