def filter_(ret): bangumi_obj = Bangumi(name=ret.name) bangumi_obj.select_obj() if not bangumi_obj: print_error('Bangumi {0} does not exist.'.format(bangumi_obj.name)) followed_obj = Followed(bangumi_name=bangumi_obj.name) followed_obj.select_obj() if not followed_obj: print_error( 'Bangumi {0} has not subscribed, try \'bgmi add "{1}"\'.'.format( bangumi_obj.name, bangumi_obj.name)) subtitle = ret.subtitle include = ret.include exclude = ret.exclude regex = ret.regex followed_filter_obj = Filter(bangumi_name=ret.name) followed_filter_obj.select_obj() if not followed_filter_obj: followed_filter_obj.save() if subtitle is not None: subtitle = map(lambda s: s.strip(), subtitle.split(',')) subtitle = map(lambda s: s['id'], 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: True if s in subtitle_list else False, 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() print_info('Usable subtitle group: {0}'.format(', '.join( map(lambda s: s['name'], Subtitle.get_subtitle(bangumi_obj.subtitle_group.split(', '))) )) if bangumi_obj.subtitle_group else 'None') print_filter(followed_filter_obj)
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, } return result
def filter_(name, subtitle=None, include=None, exclude=None, regex=None): result = {'status': 'success', 'message': ''} bangumi_obj = Bangumi(name=name) bangumi_obj.select_obj() if not bangumi_obj: result['status'] = 'error' result['message'] = 'Bangumi {0} does not exist.'.format( bangumi_obj.name) return result followed_obj = Followed(bangumi_name=bangumi_obj.name) followed_obj.select_obj() if not followed_obj: result['status'] = 'error' result['message'] = 'Bangumi {name} has not subscribed, try \'bgmi add "{name}"\'.' \ .format(name=bangumi_obj.name) return result followed_filter_obj = Filter(bangumi_name=name) followed_filter_obj.select_obj() if not followed_filter_obj: followed_filter_obj.save() if subtitle is not None: subtitle = map(lambda s: s.strip(), subtitle.split(',')) subtitle = map(lambda s: s['id'], 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(bangumi_obj.subtitle_group.split(', ')))) print_info('Usable subtitle group: {0}'.format( ', '.join(subtitle_list) if subtitle_list else 'None')) print_filter(followed_filter_obj) result['data'] = { 'name': name, 'subtitle_group': list( map(lambda s: s['name'], Subtitle.get_subtitle( bangumi_obj.subtitle_group.split(', ')))), 'followed': list( map( lambda s: s['name'], Subtitle.get_subtitle(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, } return result