def fetch(self, save=False, group_by_weekday=True): bangumi_result, subtitle_group_result = self.fetch_bangumi_calendar_and_subtitle_group( ) Bangumi.delete_all() if subtitle_group_result: for subtitle_group in subtitle_group_result: (Subtitle.insert({ Subtitle.id: text_type(subtitle_group['id']), Subtitle.name: text_type(subtitle_group['name']) }).on_conflict_replace()).execute() if not bangumi_result: print('no result return None') return [] for bangumi in bangumi_result: bangumi['cover'] = self.cover_url + bangumi['cover'] if save: for bangumi in bangumi_result: self.save_data(bangumi) if group_by_weekday: result_group_by_weekday = defaultdict(list) for bangumi in bangumi_result: result_group_by_weekday[bangumi['update_time'].lower()].append( bangumi) bangumi_result = result_group_by_weekday return bangumi_result
def fetch(self, save=False, group_by_weekday=True): ( bangumi_result, subtitle_group_result, ) = self.fetch_bangumi_calendar_and_subtitle_group() Bangumi.delete_all() if subtitle_group_result: for subtitle_group in subtitle_group_result: (Subtitle.insert({ Subtitle.id: str(subtitle_group["id"]), Subtitle.name: str(subtitle_group["name"]), }).on_conflict_replace()).execute() if not bangumi_result: print("no result return None") return [] for bangumi in bangumi_result: bangumi["cover"] = self.cover_url + bangumi["cover"] if save: for bangumi in bangumi_result: self.save_data(bangumi) if group_by_weekday: result_group_by_weekday = defaultdict(list) for bangumi in bangumi_result: result_group_by_weekday[bangumi["update_time"].lower()].append( bangumi) bangumi_result = result_group_by_weekday return bangumi_result
def fetch(self, save: bool = False, group_by_weekday: bool = True) -> Any: bangumi_result = self.fetch_bangumi_calendar() if not bangumi_result: print("can't fetch anything from website") return [] Bangumi.delete_all() if save: for bangumi in bangumi_result: self.save_bangumi(bangumi) if group_by_weekday: result_group_by_weekday = defaultdict(list) for bangumi in bangumi_result: result_group_by_weekday[bangumi.update_time.lower()].append(bangumi) return result_group_by_weekday return bangumi_result
def bangumi_calendar(self, force_update=False, save=True, cover=None): """ :param force_update: :type force_update: bool :param save: set true to enable save bangumi data to database :type save: bool :param cover: list of cover url (of scripts) want to download :type cover: list[str] """ if force_update and not test_connection(): force_update = False print_warning('Network is unreachable') if force_update: print_info('Fetching bangumi info ...') Bangumi.delete_all() weekly_list = self.fetch(save=save) else: weekly_list = Bangumi.get_updating_bangumi() if not weekly_list: print_warning('Warning: no bangumi schedule, fetching ...') weekly_list = self.fetch(save=save) if cover is not None: # download cover to local cover_to_be_download = cover for daily_bangumi in weekly_list.values(): for bangumi in daily_bangumi: _, file_path = convert_cover_url_to_path(bangumi['cover']) if not glob.glob(file_path): cover_to_be_download.append(bangumi['cover']) if cover_to_be_download: print_info('Updating cover ...') download_cover(cover_to_be_download) return weekly_list