defmatch_previous(bgm_previous): """ 通过比较id来查询过往文件中是否已有该番剧,如果已有,则再判断番剧信息是否正确。 """ if bgm_current['id'] == bgm_previous['id']: if bgm_previous['type'] == '番剧': global flag flag = flag + 1 bgm_current['score'] = bgm_previous['score'] bgm_current['des'] = bgm_previous['des'] bgm_current['wish'] = bgm_previous['wish'] bgm_current['doing'] = bgm_previous['doing'] bgm_current['collect'] = bgm_previous['collect'] bgm_current['totalCount'] = bgm_previous['totalCount']
defmatch(): """ 多线程任务,每次对一个番剧从之前的文件中查找信息match_previous(),如果未找到,或信息无效,则通过API获取信息 """ thread_list_match = [] global flag, matched flag = 0 for want_watch_previous in data_previous['wantWatch']: t_match = threading.Thread(target=match_previous(want_watch_previous)) t_match.start() thread_list_match.append(t_match) [t_match.join() for t_match in thread_list_match] for watching_previous in data_previous['watching']: t_match = threading.Thread(target=match_previous(watching_previous)) t_match.start() thread_list_match.append(t_match) [t_match.join() for t_match in thread_list_match] for watched_previous in data_previous['watched']: t_match = threading.Thread(target=match_previous(watched_previous)) t_match.start() thread_list_match.append(t_match) [t_match.join() for t_match in thread_list_match] ifnot flag: print('\r\033[K本地无信息,将通过API获取:{}'.format(bgm_current['title']), end='') url = "https://api.bgm.tv/v0/subjects/" + bgm_current['id'] headers = { 'user-agent': 'Trrrrw/hexo-bilibili-bangumi-addon(https://github.com/Trrrrw/hexo-bilibili-bangumi-addon)', 'accept': 'application / json' } try: r = httpx.get(url=url, headers=headers) except httpx.ConnectTimeout: print('httpx.ConnectTimeout: The handshake operation timed out') time.sleep(1) r = httpx.get(url=url, headers=headers) dirt_data = json.loads(r.text) try: bgm_current['score'] = dirt_data['rating']['score'] except KeyError: bgm_current['score'] = "-" try: bgm_current['des'] = dirt_data['summary'] except KeyError: bgm_current['des'] = "-" try: bgm_current['wish'] = dirt_data['collection']['wish'] except KeyError: bgm_current['wish'] = "-" try: bgm_current['doing'] = dirt_data['collection']['doing'] except KeyError: bgm_current['doing'] = "-" try: bgm_current['collect'] = dirt_data['collection']['collect'] except KeyError: bgm_current['collect'] = "-" for info in dirt_data['infobox']: if info['key'] == '话数': bgm_current['totalCount'] = '全' + info['value'] + '话' break else: bgm_current['totalCount'] = '全*话' bgm_current['type'] = '番剧' matched = matched + 1
if __name__ == "__main__": # 读取bangumis.json为字典data_previous data_previous = file_operate('r') # 获取新的bangumis.json os.system("hexo bangumi -u") # 读取bangumis.json为字典data_current data_current = file_operate('r') flag = 0 matched = 0 # 多线程 thread_list = [] for bgm_current in data_current['wantWatch']: t = threading.Thread(target=match) t.start() thread_list.append(t) [t.join() for t in thread_list] for bgm_current in data_current['watching']: t = threading.Thread(target=match) t.start() thread_list.append(t) [t.join() for t in thread_list] for bgm_current in data_current['watched']: t = threading.Thread(target=match) t.start() thread_list.append(t) [t.join() for t in thread_list] print('\r\033[K成功获取{}个番剧信息'.format(matched)) file_operate('w')