Пример #1
0
def cal(force_update=False, save=False):
    logger.debug('cal force_update: {} save: {}'.format(force_update, save))
    weekly_list = website.bangumi_calendar(force_update=force_update,
                                           save=save)
    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
    for day, value in weekly_list.items():
        for index, bangumi in enumerate(value):
            bangumi['cover'] = normalize_path(bangumi['cover'])
            if isinstance(bangumi['subtitle_group'], list):
                subtitle_group = list(
                    map(lambda x: {
                        'name': x['name'],
                        'id': x['id']
                    }, Subtitle.get_subtitle_by_id(bangumi['subtitle_group'])))
            else:
                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
Пример #2
0
def cal(force_update=False, save=False):
    logger.debug('cal force_update: {} save: {}'.format(force_update, save))
    weekly_list = website.bangumi_calendar(force_update=force_update, save=save)
    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
    for day, value in weekly_list.items():
        for index, bangumi in enumerate(value):
            bangumi['cover'] = normalize_path(bangumi['cover'])
            if isinstance(bangumi['subtitle_group'], list):
                subtitle_group = list(map(lambda x: {'name': x['name'], 'id': x['id']},
                                          Subtitle.get_subtitle_by_id(
                                              bangumi['subtitle_group'])))
            else:
                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
Пример #3
0
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
Пример #4
0
    def save_bangumi(data: WebsiteBangumi) -> None:
        """save bangumi to database"""
        b, obj_created = Bangumi.get_or_create(keyword=data.keyword,
                                               defaults=data.dict())
        if not obj_created:
            should_save = False
            if data.cover and b.cover != data.cover:
                b.cover = data.cover
                should_save = True

            if data.update_time != "Unknown" and data.update_time != b.update_time:
                b.update_time = data.update_time
                should_save = True

            subtitle_group = Bangumi(
                subtitle_group=data.subtitle_group).subtitle_group

            if b.status != STATUS_UPDATING or b.subtitle_group != subtitle_group:
                b.status = STATUS_UPDATING
                b.subtitle_group = subtitle_group
                should_save = True

            if should_save:
                b.save()

        for subtitle_group in data.subtitle_group:
            (Subtitle.insert({
                Subtitle.id: str(subtitle_group.id),
                Subtitle.name: str(subtitle_group.name),
            }).on_conflict_replace()).execute()
Пример #5
0
Файл: base.py Проект: zhkrb/BGmi
    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
Пример #6
0
    def fetch(self, save=False, group_by_weekday=True):
        bangumi_result, subtitle_group_result = self.fetch_bangumi_calendar_and_subtitle_group(
        )
        if subtitle_group_result:
            for subtitle_group in subtitle_group_result:
                s, if_created = Subtitle.get_or_create(
                    id=_unicode(subtitle_group['id']),
                    name=_unicode(subtitle_group['name']))
                if if_created:
                    s.save()
        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
Пример #7
0
    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
Пример #8
0
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
Пример #9
0
def cal(force_update=False, save=False):
    logger.debug("cal force_update: {} save: {}".format(force_update, save))
    weekly_list = website.bangumi_calendar(force_update=force_update,
                                           save=save)
    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
    for day, value in weekly_list.items():
        for index, bangumi in enumerate(value):
            bangumi["cover"] = normalize_path(bangumi["cover"])
            if isinstance(bangumi["subtitle_group"], list):
                subtitle_group = list(
                    map(
                        lambda x: {
                            "name": x["name"],
                            "id": x["id"]
                        },
                        Subtitle.get_subtitle_by_id(bangumi["subtitle_group"]),
                    ))
            else:
                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
Пример #10
0
    def followed_bangumi():
        """

        :return: list of bangumi followed
        :rtype: list[dict]
        """
        weekly_list_followed = Bangumi.get_updating_bangumi(status=STATUS_FOLLOWED)
        weekly_list_updated = Bangumi.get_updating_bangumi(status=STATUS_UPDATED)
        weekly_list = defaultdict(list)
        for k, v in chain(weekly_list_followed.items(), weekly_list_updated.items()):
            weekly_list[k].extend(v)
        for bangumi_list in weekly_list.values():
            for bangumi in bangumi_list:
                bangumi['subtitle_group'] = [{'name': x['name'],
                                              'id': x['id']} for x in
                                             Subtitle.get_subtitle_by_id(bangumi['subtitle_group'].split(', '))]
        return weekly_list
Пример #11
0
    def followed_bangumi() -> Dict[str, list]:
        """

        :return: list of bangumi followed
        """
        weekly_list_followed = Bangumi.get_updating_bangumi(status=STATUS_FOLLOWED)
        weekly_list_updated = Bangumi.get_updating_bangumi(status=STATUS_UPDATED)
        weekly_list = defaultdict(list)
        for k, v in chain(weekly_list_followed.items(), weekly_list_updated.items()):
            weekly_list[k].extend(v)
        for bangumi_list in weekly_list.values():
            for bangumi in bangumi_list:
                bangumi["subtitle_group"] = [
                    {"name": x["name"], "id": x["id"]}
                    for x in Subtitle.get_subtitle_by_id(
                        bangumi["subtitle_group"].split(", ")
                    )
                ]
        return weekly_list
Пример #12
0
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
Пример #13
0
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
Пример #14
0
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
Пример #15
0
def print_filter(followed_filter_obj):
    print_info('Followed subtitle group: {0}'.format(', '.join(map(lambda s: s['name'], Subtitle.get_subtitle_by_id(
        followed_filter_obj.subtitle.split(', ')))) if followed_filter_obj.subtitle else 'None'))
    print_info('Include keywords: {0}'.format(followed_filter_obj.include))
    print_info('Exclude keywords: {0}'.format(followed_filter_obj.exclude))
    print_info('Regular expression: {0}'.format(followed_filter_obj.regex))