1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] discord.py, Shikimori API, is there any way to get information about the main...

Discussão em 'Python' iniciado por Stack, Outubro 3, 2024 às 13:32.

  1. Stack

    Stack Membro Participativo

    ``BASE_URL = "https://shikimori.one/api"```

    def search_anime_by_title(title):
    url = f"{BASE_URL}/animes"
    params = {"search": title, "limit": 25}
    headers = {"User-Agent": ""}
    response = requests.get(url, headers=headers, params=params)
    if response.status_code == 200:
    return response.json()
    else:
    return None


    def clean_description(description):
    cleaned_description = re.sub(r'\[.*?\]', '', description)
    return cleaned_description


    class AnimeSelect(Select):
    def __init__(self, anime_results):
    options = [
    discord.SelectOption(
    label=f"{anime['russian'][:97]}..." if len(anime['russian']) > 100 else anime['russian'],
    value=str(anime['id'])) for anime in anime_results[:25]]
    super().__init__(placeholder="Choose an anime", options=options, min_values=1, max_values=1)

    async def callback(self, interaction: discord.Interaction):
    anime_id = self.values[0]
    anime_details = self.get_anime_details(anime_id)
    if anime_details and isinstance(anime_details, dict):
    image_url = "https://shikimori.one" + anime_details['image']['original']
    description = anime_details.get('description', 'No description')
    cleaned_description = clean_description(description) if description else 'No description'

    embed = discord.Embed(title=f"{anime_details.get('russian', 'No description')} "
    f"({anime_details.get('name', 'No name')})",
    description=cleaned_description, color=discord.Color.default())
    embed.add_field(name="Year of release", value=anime_details.get('aired_on', 'Неизвестно'))
    embed.add_field(name="Status", value=anime_details.get('status', '-'))
    embed.add_field(name="Number of episodes", value=anime_details.get('episodes', 'Неизвестно'))
    embed.set_thumbnail(url=image_url)
    button = discord.ui.Button(label="The main characters", style=discord.ButtonStyle.primary)
    button.callback = lambda inter: self.show_characters(inter, anime_id)
    view = discord.ui.View()
    view.add_item(button)
    await interaction.response.edit_message(content="Information about the selected anime::", embed=embed, view=view)

    def show_characters(self, interaction, anime_id):
    characters = self.get_animation_characters(anime_id)
    if characters and isinstance(characters, list):
    character_names = [char['name'] for char in characters]
    content = "Главные герои:\n" + "\n".join(character_names)
    view = discord.ui.View()
    for char in characters:
    button = discord.ui.Button(label=char['name'], style=discord.ButtonStyle.secondary)
    button.callback = lambda inter, name=char['name']: self.show_character_info(inter, name)
    view.add_item(button)
    return interaction.response.send_message(content=content, view=view, ephemeral=True)

    async def show_character_info(self, interaction, anime_title):
    anime_details = search_anime_by_title(anime_title)
    if anime_details and isinstance(anime_details, list) and len(anime_details) > 0:
    anime_detail = anime_details[0]
    characters = anime_detail.get('characters', [])
    if characters:
    embed = discord.Embed(
    title=f"Главные герои аниме: {anime_detail['russian']} ({anime_detail['name']})",
    color=discord.Color.default())
    for character in characters:
    character_name = character.get('name', 'No name')
    character_image = character.get('image', {}).get('original', None)
    character_role = character.get('role', 'Неизвестно')
    embed.add_field(name=character_name, value=f"Роль: {character_role}", inline=False)
    if character_image:
    embed.set_thumbnail(url=character_image)

    await interaction.response.send_message(embed=embed, ephemeral=True)
    else:

    def get_animation_characters(self, anime_id):
    url = f"{BASE_URL}/animes/{anime_id}/characters"
    headers = {"User-Agent": ""}
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
    return response.json()
    return None

    def get_anime_details(self, anime_id):
    url = f"{BASE_URL}/animes/{anime_id}"
    headers = {"User-Agent": ""}
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
    return response.json()
    return None


    class AnimeSelectView(View):
    def __init__(self, anime_results):
    super().__init__(timeout=None)
    self.add_item(AnimeSelect(anime_results))


    @bot.slash_command(name="anime", description="Search for information about anime")
    async def anime(ctx, name: discord.Option(str, description="Anime name")):
    await ctx.response.defer(ephemeral=True)
    anime_results = search_anime_by_title(name)
    if anime_results and isinstance(anime_results, list) and len(anime_results) > 0:
    view = AnimeSelectView(anime_results)
    await ctx.followup.send("Select an anime from the list:", view=view, ephemeral=True)
    else:
    await ctx.followup.send("Anime was not found or an error occurred.", ephemeral=True)


    Is there any way through the Shikimori API to get information about the main characters of the anime that the user has chosen? I tried to write a function, but it doesn't work. Help please

    *I'm sorry if something is not clear in words, I use a translator because I am from Russia`

    Continue reading...

Compartilhe esta Página