from db import DbAction from req import ReqAction from time import sleep import datetime import random # config # get_end_date = 'today' get_end_date = '2023-08-03' def format_link(link): if '?pwd=' in link: link = link[:-9] if 'init?surl=' in link: link = 'https://pan.baidu.com/s/1' + link[38:60] return link 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'])] print('-' * 30 + str(_data['id']) + '-' * 30) print(save_path) link = format_link(_data['save_link']) print(_data['save_link'], ' -> ', link, ' ', _data['code']) result, name = req_obj.process(save_path, link, _data['code']) 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 def main(): db_obj = DbAction(get_end_date) req_obj = ReqAction() try: data_list = db_obj.get_db_data() 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 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 if len(failed_list): failed_id = [_data['id'] for _data in failed_list] print('重试后依然失败:') print(failed_id) except Exception as e: print(e) finally: db_obj.disconnect_db() if __name__ == '__main__': main()