def add(name, episode=None): """ ret.name :str """ # action add # add bangumi by a list of bangumi name # result = {} logger.debug('add name: {} episode: {}'.format(name, episode)) if not Bangumi.get_updating_bangumi(): website.fetch(save=True, group_by_weekday=False) try: bangumi_obj = Bangumi.fuzzy_get(name=name) except Bangumi.DoesNotExist: result = {'status': 'error', 'message': '{0} not found, please check the name'.format(name)} return result followed_obj, this_obj_created = Followed.get_or_create(bangumi_name=bangumi_obj.name, defaults={'status': STATUS_FOLLOWED}) if not this_obj_created: if followed_obj.status == STATUS_FOLLOWED: result = {'status': 'warning', 'message': '{0} already followed'.format(bangumi_obj.name)} return result else: followed_obj.status = STATUS_FOLLOWED followed_obj.save() Filter.get_or_create(bangumi_name=name) bangumi_data, _ = website.get_maximum_episode(bangumi_obj, subtitle=False, max_page=MAX_PAGE) followed_obj.episode = bangumi_data['episode'] if episode is None else episode followed_obj.save() result = {'status': 'success', 'message': '{0} has been followed'.format(bangumi_obj.name)} logger.debug(result) return result
def cal(force_update: bool = False, save: bool = False, cover: Optional[List[str]] = None) -> Dict[str, List[Dict[str, Any]]]: logger.debug("cal force_update: %r save: %r", force_update, save) weekly_list = Bangumi.get_updating_bangumi() if not weekly_list: print_warning("Warning: no bangumi schedule, fetching ...") force_update = True if force_update: print_info("Fetching bangumi info ...") website.fetch(save=save) weekly_list = Bangumi.get_updating_bangumi() 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 (os.path.exists(file_path) and bool(imghdr.what(file_path))): cover_to_be_download.append(bangumi["cover"]) if cover_to_be_download: print_info("Updating cover ...") download_cover(cover_to_be_download) runner = ScriptRunner() patch_list = runner.get_models_dict() for i in patch_list: weekly_list[i["update_time"].lower()].append(i) logger.debug(weekly_list) # for web api, return all subtitle group info r = weekly_list # type: Dict[str, List[Dict[str, Any]]] for day, value in weekly_list.items(): for index, bangumi in enumerate(value): bangumi["cover"] = normalize_path(bangumi["cover"]) subtitle_group = list( map( lambda x: { "name": x["name"], "id": x["id"] }, Subtitle.get_subtitle_by_id( bangumi["subtitle_group"].split(", " "")), )) r[day][index]["subtitle_group"] = subtitle_group logger.debug(r) return r
def add(name, episode=None): """ ret.name :str """ # action add # add bangumi by a list of bangumi name # result = {} logger.debug('add name: {} episode: {}'.format(name, episode)) if not Bangumi.get_updating_bangumi(): website.fetch(save=True, group_by_weekday=False) try: bangumi_obj = Bangumi.get(name=name) except Bangumi.DoesNotExist: result = { 'status': 'error', 'message': '{0} not found, please check the name'.format(name) } return result followed_obj, this_obj_created = Followed.get_or_create( bangumi_name=bangumi_obj.name, defaults={'status': STATUS_FOLLOWED}) if not this_obj_created: if followed_obj.status == STATUS_FOLLOWED: result = { 'status': 'warning', 'message': '{0} already followed'.format(bangumi_obj.name) } return result else: followed_obj.status = STATUS_FOLLOWED followed_obj.save() Filter.get_or_create(bangumi_name=name) bangumi_data, _ = website.get_maximum_episode(bangumi_obj, subtitle=False, max_page=MAX_PAGE) followed_obj.episode = bangumi_data[ 'episode'] if episode is None else episode followed_obj.save() result = { 'status': 'success', 'message': '{0} has been followed'.format(bangumi_obj.name) } logger.debug(result) return result
def add(name: str, episode: int = None) -> ControllerResult: """ ret.name :str """ # action add # add bangumi by a list of bangumi name logger.debug("add name: %s episode: %d", name, episode) if not Bangumi.get_updating_bangumi(): website.fetch(save=True, group_by_weekday=False) try: bangumi_obj = Bangumi.fuzzy_get(name=name) except Bangumi.DoesNotExist: result = { "status": "error", "message": f"{name} not found, please check the name", } return result followed_obj, this_obj_created = Followed.get_or_create( bangumi_name=bangumi_obj.name, defaults={"status": STATUS_FOLLOWED}) if not this_obj_created: if followed_obj.status == STATUS_FOLLOWED: result = { "status": "warning", "message": f"{bangumi_obj.name} already followed", } return result else: followed_obj.status = STATUS_FOLLOWED followed_obj.save() Filter.get_or_create(bangumi_name=name) max_episode, _ = website.get_maximum_episode(bangumi_obj, max_page=int(MAX_PAGE)) followed_obj.episode = max_episode if episode is None else episode followed_obj.save() result = { "status": "success", "message": f"{bangumi_obj.name} has been followed", } logger.debug(result) return result
def update(name, download=None, not_ignore=False): logger.debug( 'updating bangumi info with args: download: {}'.format(download)) result = { 'status': 'info', 'message': '', 'data': { 'updated': [], 'downloaded': [] } } ignore = not bool(not_ignore) print_info('marking bangumi status ...') now = int(time.time()) for i in Followed.get_all_followed(): if i['updated_time'] and int(i['updated_time'] + 60 * 60 * 24) < now: followed_obj = Followed.get(bangumi_name=i['bangumi_name']) followed_obj.status = STATUS_FOLLOWED followed_obj.save() for script in ScriptRunner().scripts: obj = script.Model().obj if obj.updated_time and int(obj.updated_time + 60 * 60 * 24) < now: obj.status = STATUS_FOLLOWED obj.save() print_info('updating bangumi data ...') website.fetch(save=True, group_by_weekday=False) print_info('updating subscriptions ...') download_queue = [] if download: if not name: print_warning('No specified bangumi, ignore `--download` option') if len(name) > 1: print_warning( 'Multiple specified bangumi, ignore `--download` option') if not name: updated_bangumi_obj = Followed.get_all_followed() else: updated_bangumi_obj = [] for i in name: try: f = Followed.get(bangumi_name=i) f = model_to_dict(f) updated_bangumi_obj.append(f) except DoesNotExist: pass runner = ScriptRunner() script_download_queue = runner.run() for subscribe in updated_bangumi_obj: print_info('fetching %s ...' % subscribe['bangumi_name']) try: bangumi_obj = Bangumi.get(name=subscribe['bangumi_name']) except Bangumi.DoesNotExist: print_error('Bangumi<{0}> does not exists.'.format( subscribe['bangumi_name']), exit_=False) continue try: followed_obj = Followed.get(bangumi_name=subscribe['bangumi_name']) except Followed.DoesNotExist: print_error('Bangumi<{0}> is not followed.'.format( subscribe['bangumi_name']), exit_=False) continue episode, all_episode_data = website.get_maximum_episode( bangumi=bangumi_obj, ignore_old_row=ignore, max_page=1) if (episode.get('episode') > subscribe['episode']) or (len(name) == 1 and download): if len(name) == 1 and download: episode_range = download else: episode_range = range(subscribe['episode'] + 1, episode.get('episode', 0) + 1) print_success('%s updated, episode: %d' % (subscribe['bangumi_name'], episode['episode'])) followed_obj.episode = episode['episode'] followed_obj.status = STATUS_UPDATED followed_obj.updated_time = int(time.time()) followed_obj.save() result['data']['updated'].append({ 'bangumi': subscribe['bangumi_name'], 'episode': episode['episode'] }) for i in episode_range: for epi in all_episode_data: if epi['episode'] == i: download_queue.append(epi) break if download is not None: result['data']['downloaded'] = download_queue download_prepare(download_queue) download_prepare(script_download_queue) print_info('Re-downloading ...') download_prepare( Download.get_all_downloads(status=STATUS_NOT_DOWNLOAD)) return result