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 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 test_filter(bangumi_names): name = bangumi_names[0] main(f"add {name} --episode 0".split()) main([ "filter", name, "--subtitle", "", "--exclude", "MKV", "--regex", "720p|720P" ]) f = Filter.get(bangumi_name=name, exclude="MKV", regex="720p|720P") assert not f.include assert not f.subtitle
def filter_(name, subtitle=None, include=None, exclude=None, regex=None): result = {'status': 'success', 'message': ''} try: bangumi_obj = Bangumi.fuzzy_get(name=name) except Bangumi.DoesNotExist: result['status'] = 'error' result['message'] = 'Bangumi {0} does not exist.'.format(name) return result try: Followed.get(bangumi_name=bangumi_obj.name) except Followed.DoesNotExist as exc: result['status'] = 'error' result['message'] = 'Bangumi {name} has not subscribed, try \'bgmi add "{name}"\'.' \ .format(name=bangumi_obj.name) return result followed_filter_obj, is_this_obj_created = Filter.get_or_create(bangumi_name=name) if is_this_obj_created: followed_filter_obj.save() if subtitle is not None: subtitle = [s.strip() for s in subtitle.split(',')] subtitle = [s['id'] for s in Subtitle.get_subtitle_by_name(subtitle)] subtitle_list = [s.split('.')[0] for s in bangumi_obj.subtitle_group.split(', ') if '.' in s] subtitle_list.extend(bangumi_obj.subtitle_group.split(', ')) subtitle = filter(lambda s: s in subtitle_list, subtitle) subtitle = ', '.join(subtitle) followed_filter_obj.subtitle = subtitle if include is not None: followed_filter_obj.include = include if exclude is not None: followed_filter_obj.exclude = exclude if regex is not None: followed_filter_obj.regex = regex followed_filter_obj.save() subtitle_list = list(map(lambda s: s['name'], Subtitle.get_subtitle_by_id(bangumi_obj.subtitle_group.split(', ')))) result['data'] = { 'name': name, 'subtitle_group': subtitle_list, 'followed': list(map(lambda s: s['name'], Subtitle.get_subtitle_by_id(followed_filter_obj.subtitle.split(', '))) if followed_filter_obj.subtitle else []), 'include': followed_filter_obj.include, 'exclude': followed_filter_obj.exclude, 'regex': followed_filter_obj.regex, } logger.debug(result) return result
def test_include(): e = Filter(include="2,3,5").apply_on_episodes([ Episode(name="1", title="1", download="1", episode=1), Episode(name="1", title="1", download="2", episode=1), Episode(name="2", title="2", download="3", episode=2), Episode(name="2", title="2", download="4", episode=2), Episode(name="3", title="3", download="5", episode=3), Episode(name="5", title="5", download="6", episode=5), ]) assert len(e) == 4, e assert {x.download for x in e} == set("3456")
def test_exclude(): e = Filter(exclude="2,3,5").apply_on_episodes([ Episode(title="1", download="1", episode=1), Episode(title="1", download="2", episode=2), Episode(title="2", download="3", episode=1), Episode(title="2", download="4", episode=2), Episode(title="3", download="5", episode=3), Episode(title="5", download="6", episode=5), ]) assert len(e) == 2, e assert {x.download for x in e} == {"1", "2"}
def filter_wrapper(ret): result = filter_(name=ret.name, subtitle=ret.subtitle, include=ret.include, exclude=ret.exclude, regex=ret.regex) if 'data' not in result: globals()["print_{}".format(result['status'])](result['message']) else: print_info('Usable subtitle group: {0}'.format(', '.join(result['data']['subtitle_group']))) followed_filter_obj = Filter.get(bangumi_name=ret.name) print_filter(followed_filter_obj) return result['data']
def filter_wrapper(ret): result = filter_(name=ret.name, subtitle=ret.subtitle, include=ret.include, exclude=ret.exclude, regex=ret.regex) if 'data' not in result: globals()["print_{}".format(result['status'])](result['message']) else: print_info('Usable subtitle group: {0}'.format(', '.join( result['data']['subtitle_group']))) followed_filter_obj = Filter.get(bangumi_name=ret.name) print_filter(followed_filter_obj) return result['data']
def get_maximum_episode(self, bangumi, subtitle=True, ignore_old_row=True, max_page=MAX_PAGE): """ :type max_page: str :param max_page: :type bangumi: object :type ignore_old_row: bool :param ignore_old_row: :type bangumi: Bangumi :param subtitle: :type subtitle: bool """ followed_filter_obj, _ = Filter.get_or_create(bangumi_name=bangumi.name) if followed_filter_obj and subtitle: subtitle_group = followed_filter_obj.subtitle else: subtitle_group = None if followed_filter_obj and subtitle: include = followed_filter_obj.include else: include = None if followed_filter_obj and subtitle: exclude = followed_filter_obj.exclude else: exclude = None if followed_filter_obj and subtitle: regex = followed_filter_obj.regex else: regex = None data = [i for i in self.fetch_episode(_id=bangumi.keyword, name=bangumi.name, subtitle_group=subtitle_group, include=include, exclude=exclude, regex=regex, max_page=int(max_page)) if i['episode'] is not None] if ignore_old_row: data = [row for row in data if row['time'] > int(time.time()) - 3600 * 24 * 30 * 3] # three month if data: bangumi = max(data, key=lambda _i: _i['episode']) return bangumi, data else: return {'episode': 0}, []
def filter_wrapper(ret: Any) -> None: result = filter_( name=ret.name, subtitle=ret.subtitle, include=ret.include, exclude=ret.exclude, regex=ret.regex, ) if "data" not in result: globals()["print_{}".format(result["status"])](result["message"]) else: print_info("Usable subtitle group: {}".format(", ".join( result["data"]["subtitle_group"]))) followed_filter_obj = Filter.get(bangumi_name=result["data"]["name"]) print_filter(followed_filter_obj)
def get_maximum_episode( self, bangumi: Bangumi, ignore_old_row: bool = True, max_page: int = MAX_PAGE, ) -> Tuple[int, List[Episode]]: followed_filter_obj, _ = Filter.get_or_create( bangumi_name=bangumi.name) info = self.fetch_single_bangumi( bangumi.keyword, subtitle_list=followed_filter_obj.subtitle_group_split, max_page=max_page, ) if info is not None: self.save_bangumi(info) data = followed_filter_obj.apply_on_episodes(info.episodes) else: data = self.fetch_episode_of_bangumi( bangumi_id=bangumi.keyword, max_page=max_page, subtitle_list=followed_filter_obj.subtitle_group_split, ) for episode in data: episode.name = bangumi.name if ignore_old_row: data = [ row for row in data if row.time > int(time.time()) - 3600 * 24 * 30 * 3 ] # three month if data: b = max(data, key=lambda _i: _i.episode) return b.episode, data else: return 0, []
def fetch_(ret): try: bangumi_obj = Bangumi.get(name=ret.name) except Bangumi.DoesNotExist: print_error('Bangumi {0} not exist'.format(ret.name)) return try: Followed.get(bangumi_name=bangumi_obj.name) except Followed.DoesNotExist: print_error('Bangumi {0} is not followed'.format(ret.name)) return followed_filter_obj = Filter.get(bangumi_name=ret.name) print_filter(followed_filter_obj) print_info('Fetch bangumi {0} ...'.format(bangumi_obj.name)) _, data = website.get_maximum_episode(bangumi_obj, ignore_old_row=False if ret.not_ignore else True) if not data: print_warning('Nothing.') for i in data: print_success(i['title'])
def fetch_(ret): try: bangumi_obj = Bangumi.get(name=ret.name) except Bangumi.DoesNotExist: print_error('Bangumi {0} not exist'.format(ret.name)) return try: Followed.get(bangumi_name=bangumi_obj.name) except Followed.DoesNotExist: print_error('Bangumi {0} is not followed'.format(ret.name)) return followed_filter_obj = Filter.get(bangumi_name=ret.name) print_filter(followed_filter_obj) print_info('Fetch bangumi {0} ...'.format(bangumi_obj.name)) _, data = website.get_maximum_episode( bangumi_obj, ignore_old_row=False if ret.not_ignore else True) if not data: print_warning('Nothing.') for i in data: print_success(i['title'])
def fetch_(ret: Any) -> None: try: bangumi_obj = Bangumi.get(name=ret.name) except Bangumi.DoesNotExist: print_error(f"Bangumi {ret.name} not exist") return try: Followed.get(bangumi_name=bangumi_obj.name) except Followed.DoesNotExist: print_error(f"Bangumi {ret.name} is not followed") return followed_filter_obj = Filter.get(bangumi_name=ret.name) print_filter(followed_filter_obj) print_info(f"Fetch bangumi {bangumi_obj.name} ...") _, data = website.get_maximum_episode( bangumi_obj, ignore_old_row=not bool(ret.not_ignore)) if not data: print_warning("Nothing.") for i in data: print_success(i.title)
def filter_(name, subtitle=None, include=None, exclude=None, regex=None): result = {"status": "success", "message": ""} try: bangumi_obj = Bangumi.fuzzy_get(name=name) except Bangumi.DoesNotExist: result["status"] = "error" result["message"] = "Bangumi {} does not exist.".format(name) return result try: Followed.get(bangumi_name=bangumi_obj.name) except Followed.DoesNotExist as exc: result["status"] = "error" result[ "message"] = "Bangumi {name} has not subscribed, try 'bgmi add \"{name}\"'.".format( name=bangumi_obj.name) return result followed_filter_obj, is_this_obj_created = Filter.get_or_create( bangumi_name=name) if is_this_obj_created: followed_filter_obj.save() if subtitle is not None: subtitle = [s.strip() for s in subtitle.split(",")] subtitle = [s["id"] for s in Subtitle.get_subtitle_by_name(subtitle)] subtitle_list = [ s.split(".")[0] for s in bangumi_obj.subtitle_group.split(", ") if "." in s ] subtitle_list.extend(bangumi_obj.subtitle_group.split(", ")) subtitle = filter(lambda s: s in subtitle_list, subtitle) subtitle = ", ".join(subtitle) followed_filter_obj.subtitle = subtitle if include is not None: followed_filter_obj.include = include if exclude is not None: followed_filter_obj.exclude = exclude if regex is not None: followed_filter_obj.regex = regex followed_filter_obj.save() subtitle_list = list( map( lambda s: s["name"], Subtitle.get_subtitle_by_id( bangumi_obj.subtitle_group.split(", ")), )) result["data"] = { "name": name, "subtitle_group": subtitle_list, "followed": list( map( lambda s: s["name"], Subtitle. get_subtitle_by_id(followed_filter_obj.subtitle.split(", ")), ) if followed_filter_obj.subtitle else []), "include": followed_filter_obj.include, "exclude": followed_filter_obj.exclude, "regex": followed_filter_obj.regex, } logger.debug(result) return result
def filter_(name, subtitle=None, include=None, exclude=None, regex=None): result = {'status': 'success', 'message': ''} try: bangumi_obj = Bangumi.get(name=name) except Bangumi.DoesNotExist: result['status'] = 'error' result['message'] = 'Bangumi {0} does not exist.'.format(name) return result try: Followed.get(bangumi_name=bangumi_obj.name) except Followed.DoesNotExist as exc: result['status'] = 'error' result['message'] = 'Bangumi {name} has not subscribed, try \'bgmi add "{name}"\'.' \ .format(name=bangumi_obj.name) return result followed_filter_obj, is_this_obj_created = Filter.get_or_create( bangumi_name=name) if is_this_obj_created: followed_filter_obj.save() if subtitle is not None: subtitle = [s.strip() for s in subtitle.split(',')] subtitle = [s['id'] for s in Subtitle.get_subtitle_by_name(subtitle)] subtitle_list = [ s.split('.')[0] for s in bangumi_obj.subtitle_group.split(', ') if '.' in s ] subtitle_list.extend(bangumi_obj.subtitle_group.split(', ')) subtitle = filter(lambda s: s in subtitle_list, subtitle) subtitle = ', '.join(subtitle) followed_filter_obj.subtitle = subtitle if include is not None: followed_filter_obj.include = include if exclude is not None: followed_filter_obj.exclude = exclude if regex is not None: followed_filter_obj.regex = regex followed_filter_obj.save() subtitle_list = list( map( lambda s: s['name'], Subtitle.get_subtitle_by_id( bangumi_obj.subtitle_group.split(', ')))) result['data'] = { 'name': name, 'subtitle_group': subtitle_list, 'followed': list( map( lambda s: s['name'], Subtitle. get_subtitle_by_id(followed_filter_obj.subtitle.split(', ') )) if followed_filter_obj.subtitle else []), 'include': followed_filter_obj.include, 'exclude': followed_filter_obj.exclude, 'regex': followed_filter_obj.regex, } logger.debug(result) return result
def filter_( name: str, subtitle: Optional[str] = None, include: Optional[str] = None, exclude: Optional[str] = None, regex: Optional[str] = None, ) -> ControllerResult: result = {"status": "success", "message": ""} # type: Dict[str, Any] try: bangumi_obj = Bangumi.fuzzy_get(name=name) except Bangumi.DoesNotExist: result["status"] = "error" result["message"] = f"Bangumi {name} does not exist." return result try: Followed.get(bangumi_name=bangumi_obj.name) except Followed.DoesNotExist: result["status"] = "error" result[ "message"] = "Bangumi {name} has not subscribed, try 'bgmi add \"{name}\"'.".format( name=bangumi_obj.name) return result followed_filter_obj, is_this_obj_created = Filter.get_or_create( bangumi_name=bangumi_obj.name) if is_this_obj_created: followed_filter_obj.save() if subtitle is not None: _subtitle = [s.strip() for s in subtitle.split(",")] _subtitle = [s["id"] for s in Subtitle.get_subtitle_by_name(_subtitle)] subtitle_list = [ s.split(".")[0] for s in bangumi_obj.subtitle_group.split(", ") if "." in s ] subtitle_list.extend(bangumi_obj.subtitle_group.split(", ")) followed_filter_obj.subtitle = ", ".join( filter(lambda s: s in subtitle_list, _subtitle)) if include is not None: followed_filter_obj.include = include if exclude is not None: followed_filter_obj.exclude = exclude if regex is not None: followed_filter_obj.regex = regex followed_filter_obj.save() subtitle_list = [ s["name"] for s in Subtitle.get_subtitle_by_id( bangumi_obj.subtitle_group.split(", ")) ] result["data"] = { "name": bangumi_obj.name, "subtitle_group": subtitle_list, "followed": [ s["name"] for s in Subtitle.get_subtitle_by_id( followed_filter_obj.subtitle.split(", ")) ] if followed_filter_obj.subtitle else [], "include": followed_filter_obj.include, "exclude": followed_filter_obj.exclude, "regex": followed_filter_obj.regex, } logger.debug(result) return result