You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.0 KiB

1 year ago
from db import DbAction
from req import ReqAction
from time import sleep
import datetime
1 year ago
import random
11 months ago
from data_dict import exclude_list
1 year ago
# config
# get_end_date = 'today'
11 months ago
get_end_date = '2023-12-01'
get_start_date = 'today'
# get_start_date = '2023-09-30'
1 year ago
# 整理链接格式
def format_link(link, code):
1 year ago
if '?pwd=' in link:
11 months ago
_link = link
link = _link[:-9]
code = _link[-4:]
1 year ago
if 'init?surl=' in link:
link = 'https://pan.baidu.com/s/1' + link[38:60]
return link, code
1 year ago
# 封装通用方法
1 year ago
def req(_data, req_obj, db_obj):
base_temp_path = r'/Temp/'
date_str = datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d')
save_path = [base_temp_path, _data['cate'] + '_' + date_str + r'/', str(_data['id'])]
1 year ago
print('-' * 30 + str(_data['id']) + '-' * 30)
print(save_path)
link, code = format_link(_data['save_link'], _data['code'])
print(_data['save_link'], ' -> ', link, ' ', _data['code'], ' -> ', code)
result, name = req_obj.process(save_path, link, code)
1 year ago
if result:
db_obj.update_file_name(_data['id'], name)
print(f'{_data["id"]}保存成功' + '\n' * 2)
return True
else:
print(f'{_data["id"]}保存失败,请检查!' + '\n' * 2)
return _data
11 months ago
# 过滤数据
def data_filter(db_obj, _data_list):
for index, data in enumerate(_data_list):
for exclude in exclude_list:
if exclude in data['name']:
_data_list.pop(index)
print('跳过数据:', data['id'], data['name'])
db_obj.set_skip(data['id'])
break
return _data_list
1 year ago
def main():
11 months ago
db_obj = DbAction(get_end_date, get_start_date)
1 year ago
req_obj = ReqAction()
try:
# 处理正常任务
1 year ago
data_list = db_obj.get_db_data()
11 months ago
data_list = data_filter(db_obj, data_list)
1 year ago
failed_list = []
req_obj.prepare()
for _data in data_list:
result = req(_data, req_obj, db_obj)
if isinstance(result, dict):
failed_list.append(result)
sleep(random.randint(0, 3) + random.random())
i = 3
# 重试失败任务
1 year ago
while len(failed_list) > 0 and i > 0:
_temp_list = []
for _data in failed_list:
result = req(_data, req_obj, db_obj)
if isinstance(result, dict):
_temp_list.append(result)
sleep(random.randint(0, 3) + random.random())
failed_list = _temp_list
i -= 1
# 标记失败任务
1 year ago
if len(failed_list):
failed_id = []
for _data in failed_list:
failed_id.append(_data['id'])
db_obj.mark_failed(_data['id'])
1 year ago
print('重试后依然失败:')
print(failed_id)
print('请手动重试以下链接:')
print(db_obj.get_retry())
1 year ago
except Exception as e:
print(e)
finally:
db_obj.disconnect_db()
if __name__ == '__main__':
main()