예제 #1
0
async def manga_upcoming(bot: Amime, callback: CallbackQuery):
    page = int(callback.matches[0]["page"])

    message = callback.message
    lang = callback._lang

    keyboard = []
    async with httpx.AsyncClient(http2=True) as client:
        response = await client.post(
            url="https://graphql.anilist.co",
            json=dict(
                query="""
                query($per_page: Int) {
                    Page(page: 1, perPage: $per_page) {
                        media(type: MANGA, sort: POPULARITY_DESC, status: NOT_YET_RELEASED) {
                            id
                            title {
                                romaji
                                english
                                native
                            }
                            siteUrl
                        }
                    }
                }
                """,
                variables=dict(per_page=50, ),
            ),
            headers={
                "Content-Type": "application/json",
                "Accept": "application/json",
            },
        )
        data = response.json()
        await client.aclose()
        if data["data"]:
            items = data["data"]["Page"]["media"]
            suggestions = [
                Manga(id=item["id"], title=item["title"], url=item["siteUrl"])
                for item in items
            ]

            layout = Pagination(
                suggestions,
                item_data=lambda i, pg: f"manga {i.id}",
                item_title=lambda i, pg: i.title.romaji,
                page_data=lambda pg: f"upcoming manga {pg}",
            )

            lines = layout.create(page, lines=8)

            if len(lines) > 0:
                keyboard += lines

    keyboard.append([(lang.back_button, "manga")])

    await message.edit_text(
        lang.upcoming_text,
        reply_markup=ikb(keyboard),
    )
예제 #2
0
async def manga_categorie(bot: Amime, callback: CallbackQuery):
    categorie = callback.matches[0]["categorie"]
    page = int(callback.matches[0]["page"])

    message = callback.message
    lang = callback._lang

    genre = categorie.replace("_", " ")
    results = await anilist.AsyncClient().search(genre, "manga", 50)

    layout = Pagination(
        results,
        item_data=lambda i, pg: f"manga {i.id}",
        item_title=lambda i, pg: i.title.romaji,
        page_data=lambda pg: f"categorie manga {categorie} {pg}",
    )

    lines = layout.create(page, lines=8)

    keyboard = []
    if len(lines) > 0:
        keyboard += lines

    keyboard.append([(lang.back_button, "categories manga 1")])

    await message.edit_text(
        lang.categorie_text.format(genre=lang.strings[lang.code][categorie]),
        reply_markup=ikb(keyboard),
    )
예제 #3
0
async def manga_favorites(bot: Amime, callback: CallbackQuery):
    page = int(callback.matches[0]["page"])

    message = callback.message
    user = callback.from_user
    lang = callback._lang

    keyboard = []
    async with anilist.AsyncClient() as client:
        favorites = await Favorites.filter(user=user.id, type="manga")

        results = []
        for favorite in favorites:
            manga = await client.get(favorite.item, "manga")
            results.append((favorite, manga))

        layout = Pagination(
            results,
            item_data=lambda i, pg: f"manga {i[0].item}",
            item_title=lambda i, pg: i[1].title.romaji,
            page_data=lambda pg: f"favorites manga {pg}",
        )

        lines = layout.create(page, lines=8)

        if len(lines) > 0:
            keyboard += lines

    keyboard.append([(lang.back_button, "manga")])

    await message.edit_text(
        lang.favorites_text,
        reply_markup=ikb(keyboard),
    )
예제 #4
0
async def anime_categories(bot: Amime, callback: CallbackQuery):
    page = int(callback.matches[0]["page"])

    message = callback.message
    lang = callback._lang

    # fmt: off
    categories = [
        "action",
        "shounen",
        "martial_arts",
        "adventure",
        "comedy",
        "ecchi",
        "devils",
        "drama",
        "fantasy",
        "yuri",
        "yaoi",
        "school",
        "sports",
        "space",
        "hentai",
        "isekai",
        "shoujo",
        "fight",
        "mystery",
        "supernatural",
        "music",
        "slice_of_life",
    ]
    categories.sort()
    # fmt: on

    layout = Pagination(
        categories,
        item_data=lambda i, pg: f"categorie anime {i} 1",
        item_title=lambda i, pg: lang.strings[lang.code][i],
        page_data=lambda pg: f"categories anime {pg}",
    )

    lines = layout.create(page, lines=8, columns=2)

    keyboard = []
    if len(lines) > 0:
        keyboard += lines

    keyboard.append([(lang.back_button, "anime")])

    await message.edit_text(
        lang.categories_text,
        reply_markup=ikb(keyboard),
    )
예제 #5
0
async def on_list_commands_u(c, u):
    lang = u._lang
    is_query = hasattr(u, 'data')
    page = int(u.matches[0]['page'])

    item_format = 'info_command {} {}'
    page_format = 'list_commands {}'

    layout = Pagination([*cmds.items()],
                        item_data=lambda i, pg: item_format.format(i[0], pg),
                        item_title=lambda i, pg: i[0],
                        page_data=lambda pg: page_format.format(pg))

    lines = layout.create(page, columns=2, lines=3)
    if is_query:
        lines.append([(lang.back, 'start')])

    keyb = ikb(lines)
    await (u.edit if is_query else u.reply)(lang.commands_text, keyb)
예제 #6
0
async def on_list_plugins_type(c, u):
    # Determining type of update
    is_query = hasattr(u, "data")

    lang = u._lang
    page = int(u.matches[0]["page"] or 0)
    plugin_type = u.matches[0]["type"]

    inactive_plugins = await get_inactive_plugins(plugins)

    def item_title(i, pg):
        name = i[0]
        notation = i[1]["notation"]
        status = "💤" if notation in inactive_plugins else "❇️"
        return f"{status} {name}"

    layout = Pagination(
        [*plugins[plugin_type].items()],
        item_data=lambda i, pg: "info_plugin {} {} {}".format(
            i[0], plugin_type, pg),
        item_title=item_title,
        page_data=lambda pg: "{}_plugins {}".format(plugin_type, pg),
    )

    lines = layout.create(page, lines=4, columns=2)

    # if the message is /plugins (sent to bot) or it's a callback query 'plugins'
    if hasattr(u, "from_bot_handler") or (hasattr(u, "message") and u.message):
        lines.append([(lang.add_plugin, f"add_plugin {page}")])
    else:  # is command to user
        lines.append([(lang.add_plugin,
                       f"t.me/{bot.me.username}?start=plugin_add", "url")])

    if is_query:
        lines.append([(lang.back, "list_plugins")])
    keyb = ikb(lines)
    await (u.edit if is_query else u.reply)(
        lang.plugins_text(type=plugin_type), keyb)
예제 #7
0
async def on_list_plugins_type(c, u):
    # Determining type of update
    is_query = hasattr(u, 'data')

    lang = u._lang
    page = int(u.matches[0]['page'] or 0)
    plugin_type = u.matches[0]['type']

    inactive_plugins = await get_inactive_plugins(plugins)

    def item_title(i, pg):
        name = i[0]
        notation = i[1]['notation']
        status = '💤' if notation in inactive_plugins else '❇️'
        return f'{status} {name}'

    layout = Pagination(
        [*plugins[plugin_type].items()],
        item_data=lambda i, pg: 'info_plugin {} {} {}'.format(
            i[0], plugin_type, pg),
        item_title=item_title,
        page_data=lambda pg: '{}_plugins {}'.format(plugin_type, pg))

    lines = layout.create(page, lines=4, columns=2)

    # if the message is /plugins (sent to bot) or it's a callback query 'plugins'
    if hasattr(u, 'from_bot_handler') or (hasattr(u, 'message') and u.message):
        lines.append([(lang.add_plugin, f'add_plugin {page}')])
    else:  # is command to user
        lines.append([(lang.add_plugin,
                       f"t.me/{bot.me.username}?start=plugin_add", 'url')])

    if is_query:
        lines.append([(lang.back, 'list_plugins')])
    keyb = ikb(lines)
    await (u.edit if is_query else u.reply)(
        lang.plugins_text(type=plugin_type), keyb)
예제 #8
0
async def anime_manage(bot: Amime, callback: CallbackQuery):
    message = callback.message
    chat = message.chat
    user = callback.from_user
    lang = callback._lang

    anime_id = int(callback.matches[0].group(1))
    season = int(callback.matches[0].group(2))
    subtitled = bool(int(callback.matches[0].group(3)))
    language = callback.matches[0].group(4)
    page = int(callback.matches[0].group(5))

    if str(user.id) in VIDEOS.keys() and str(anime_id) in VIDEOS[str(
            user.id)].keys():
        chat.cancel_listener()
        del VIDEOS[str(user.id)][str(anime_id)]

    buttons = [
        (
            f"{lang.language_button}: {lang.strings[language]['LANGUAGE_NAME']}",
            f"manage anime language {anime_id} {season} {int(subtitled)} {language} {page}",
        ),
        (
            f"{lang.season_button}: {season}",
            f"manage anime season {anime_id} {season} {int(subtitled)} {language} {page}",
        ),
        (
            f"{lang.subtitled_button}: {lang.yes if subtitled else lang.no}",
            f"manage anime {anime_id} {season} {int(not subtitled)} {language} {page}",
        ),
        (
            lang.add_button,
            f"manage episode {anime_id} {season} -1 {int(subtitled)} {language} {page}",
        ),
    ]

    episodes = await Episodes.filter(anime=anime_id,
                                     season=season,
                                     language=language,
                                     subtitled=subtitled)
    episodes = sorted(episodes, key=lambda episode: episode.number)

    if len(episodes) >= 2:
        buttons.append((
            lang.del_season_button,
            f"manage episode delete {anime_id} {season} -1 {int(subtitled)} {language} {page}",
        ))
    else:
        if page > 0:
            page -= 1
            matches = re.search(
                r"(\d+) (\d+) (\d+) (\w+) (\d+)",
                f"{anime_id} {season} {int(subtitled)} {language} {page}",
            )
            callback.matches = [matches]
            await anime_manage(bot, callback)
            return

    buttons.append((
        lang.add_in_batch_button,
        f"manage episode batch {anime_id} {season} {int(subtitled)} {language} {page}",
    ))

    notifications = await Notifications.filter(
        item=anime_id,
        type="anime",
        language=language,
    )
    if len(notifications) > 0:
        buttons.append((
            lang.notify_users_button,
            f"notify episodes {anime_id} {season} {int(subtitled)} {language} {page}",
        ))

    keyboard = array_chunk(buttons, 2)

    layout = Pagination(
        episodes,
        item_data=lambda i, pg:
        f"manage episode {i.anime} {i.season} {i.number} {int(subtitled)} {language} {pg}",
        item_title=lambda i, pg: f"📝 {i.number}",
        page_data=lambda pg:
        f"manage anime {anime_id} {season} {int(subtitled)} {language} {pg}",
    )

    lines = layout.create(page, lines=5, columns=3)

    if len(lines) > 0:
        keyboard += lines

    keyboard.append([(lang.back_button, f"anime {anime_id}")])

    if bool(message.photo):
        await message.edit_text(
            lang.manage_anime_text,
            reply_markup=ikb(keyboard),
        )
    else:
        await callback.edit_message_media(
            InputMediaPhoto(
                f"https://img.anili.st/media/{anime_id}",
                caption=lang.manage_anime_text,
            ),
            reply_markup=ikb(keyboard),
        )
예제 #9
0
async def anime_episodes(bot: Amime, callback: CallbackQuery):
    message = callback.message
    user = callback.from_user
    lang = callback._lang

    anime_id = int(callback.matches[0].group(1))
    season = int(callback.matches[0].group(2))
    page = int(callback.matches[0].group(3))

    user_db = await Users.get(id=user.id)
    language = user_db.language_anime
    subtitled = user_db.subtitled_anime

    buttons = [
        (
            f"{lang.language_button}: {lang.strings[language]['LANGUAGE_NAME']}",
            f"episodes language {anime_id} {season} {language} {page}",
        ),
    ]

    if season > 0:
        buttons.append((
            f"{lang.season_button}: {season}",
            f"episodes season {anime_id} {season} {page}",
        ))

    buttons.append((
        f"{lang.subtitled_button}: {lang.yes if subtitled else lang.no}",
        f"episodes subtitled {anime_id} {season} {page}",
    ))

    keyboard = array_chunk(buttons, 2)

    episodes = await Episodes.filter(anime=anime_id,
                                     season=season,
                                     language=language,
                                     subtitled=subtitled)
    episodes = sorted(episodes, key=lambda episode: episode.number)
    episodes = [*filter(lambda episode: len(episode.file_id) > 0, episodes)]

    for index, episode in enumerate(
            sorted(episodes, key=lambda episode: episode.number,
                   reverse=True)):
        if await Watched.get_or_none(user=user.id,
                                     episode=episode.id) is not None:
            episode = episodes[-index]

            if index == 0:
                break

            if math.ceil(episode.number / (5 * 3)) != page:
                keyboard.append([(
                    f"{lang.next_episode_button}: {lang.episode[0]}{episode.number}",
                    f"episode {anime_id} {episode.season} {episode.number}",
                )])
            break

    episodes_list = []
    for episode in episodes:
        viewed = bool(await Viewed.get_or_none(user=user.id, item=episode.id))
        watched = bool(await Watched.get_or_none(user=user.id,
                                                 episode=episode.id))
        episodes_list.append((episode, viewed, watched))

    layout = Pagination(
        episodes_list,
        item_data=lambda i, pg:
        f"episode {i[0].anime} {i[0].season} {i[0].number}",
        item_title=lambda i, pg: ("✅" if i[2] else "👁️"
                                  if i[1] else "🙈") + f" {i[0].number}" +
        (f"-{i[0].unified_until}" if i[0].unified_until > 0 else ""),
        page_data=lambda pg: f"episodes {anime_id} {season} {pg}",
    )

    lines = layout.create(page, lines=5, columns=3)

    if len(lines) > 0:
        keyboard += lines

    keyboard.append([(lang.back_button, f"anime {anime_id}")])

    await callback.edit_message_media(
        InputMediaPhoto(
            f"https://img.anili.st/media/{anime_id}",
            caption=lang.watch_list_anime_text,
        ),
        reply_markup=ikb(keyboard),
    )