Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Fenicu committed Sep 22, 2021
1 parent 2dae2ab commit e22ed3a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 78 deletions.
41 changes: 21 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,38 @@ import os.path
from aiotracemoeapi import TraceMoe, types

api = TraceMoe()
# or api = TraceMoe(token="ABC")


async def search_anime(path: str, url: bool):
anime = await api.search(path, is_url=url)
if isinstance(anime.best_result.anilist, types.AniList):
print(f"Anime: {anime.best_result.anilist.mal_url}")
parse_text(anime)

if len(anime.best_result.anilist.title) > 0:

def parse_text(anime_response: types.AnimeResponse):
if isinstance(anime_response.best_result.anilist, types.AniList):
if len(anime_response.best_result.anilist.title) > 0:
print("Title:")
for k, v in anime.best_result.anilist.title.items():
for k, v in anime_response.best_result.anilist.title.items():
if v is None:
continue
print(f"{k}: {v}")

if len(anime.best_result.anilist.synonyms) > 0:
print(f"My Anime List: {anime_response.best_result.anilist.mal_url}")
if len(anime_response.best_result.anilist.synonyms) > 0:
print("Synonyms:")
for syn in anime.best_result.anilist.synonyms:
for syn in anime_response.best_result.anilist.synonyms:
print(syn)

if anime.best_result.anilist.is_adult:
print("Hentai 🔞!")

if anime.best_result.episode:
print(f"Episode: {anime.best_result.episode}")

if anime.best_result.anime_from:
print(f"Starting time of the matching scene: {str(dt.timedelta(seconds=int(anime.best_result.anime_from)))}")

print(f"Similarity: {anime.best_result.short_similarity()}")
if anime_response.best_result.anilist.is_adult:
print("Hentai🔞")
if anime_response.best_result.episode:
episode = anime_response.best_result.episode
if isinstance(anime_response.best_result.episode, list):
episode = " | ".join(str(ep) for ep in anime_response.best_result.episode)
print(f"Episode: {episode}")
if anime_response.best_result.anime_from:
print(
f"Starting time of the matching scene: {dt.timedelta(seconds=int(anime_response.best_result.anime_from))}"
)
print(f"Similarity: {anime_response.best_result.short_similarity()}")


if __name__ == "__main__":
Expand All @@ -62,5 +64,4 @@ if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(search_anime(path, url))


```
38 changes: 19 additions & 19 deletions examples/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,38 @@
from aiotracemoeapi import TraceMoe, types

api = TraceMoe()
# or api = TraceMoe(token="ABC")


async def search_anime(path: str, url: bool):
anime = await api.search(path, is_url=url)
if isinstance(anime.best_result.anilist, types.AniList):
print(f"Anime: {anime.best_result.anilist.mal_url}")
parse_text(anime)

if len(anime.best_result.anilist.title) > 0:

def parse_text(anime_response: types.AnimeResponse):
if isinstance(anime_response.best_result.anilist, types.AniList):
if len(anime_response.best_result.anilist.title) > 0:
print("Title:")
for k, v in anime.best_result.anilist.title.items():
for k, v in anime_response.best_result.anilist.title.items():
if v is None:
continue
print(f"{k}: {v}")

if len(anime.best_result.anilist.synonyms) > 0:
print(f"My Anime List: {anime_response.best_result.anilist.mal_url}")
if len(anime_response.best_result.anilist.synonyms) > 0:
print("Synonyms:")
for syn in anime.best_result.anilist.synonyms:
for syn in anime_response.best_result.anilist.synonyms:
print(syn)

if anime.best_result.anilist.is_adult:
print("Hentai 🔞!")

if anime.best_result.episode:
print(f"Episode: {anime.best_result.episode}")

if anime.best_result.anime_from:
if anime_response.best_result.anilist.is_adult:
print("Hentai🔞")
if anime_response.best_result.episode:
episode = anime_response.best_result.episode
if isinstance(anime_response.best_result.episode, list):
episode = " | ".join(str(ep) for ep in anime_response.best_result.episode)
print(f"Episode: {episode}")
if anime_response.best_result.anime_from:
print(
f"Starting time of the matching scene: {str(dt.timedelta(seconds=int(anime.best_result.anime_from)))}"
f"Starting time of the matching scene: {dt.timedelta(seconds=int(anime_response.best_result.anime_from))}"
)

print(f"Similarity: {anime.best_result.short_similarity()}")
print(f"Similarity: {anime_response.best_result.short_similarity()}")


if __name__ == "__main__":
Expand Down
102 changes: 63 additions & 39 deletions examples/telegrambot.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import datetime as dt
import io
from typing import Tuple

from aiogram import Bot, Dispatcher, executor, md, types
from aiotracemoeapi import TraceMoe
from aiotracemoeapi.types import AniList
from aiotracemoeapi.types import AniList, AnimeResponse

API_TOKEN = "BOT TOKEN HERE"

Expand All @@ -19,50 +20,37 @@ async def send_welcome(message: types.Message):


@dp.message_handler(
chat_type=[types.ChatType.PRIVATE],
content_types=[types.ContentType.PHOTO],
chat_type=types.ChatType.PRIVATE,
content_types=[
types.ContentType.PHOTO,
types.ContentType.ANIMATION,
types.ContentType.VIDEO,
],
run_task=True,
)
async def search_anime_private(message: types.Message):
async def search_anime(message: types.Message):
msg = await message.answer("Search...")
pic = io.BytesIO()

try:
pic = await message.photo[-1].download(pic)
anime = await trace_bot.search(pic)
data = io.BytesIO()
if message.content_type in types.ContentTypes.VIDEO:
data = await message.video.download(destination_file=data)
elif message.content_type in types.ContentTypes.ANIMATION:
data = await message.animation.download(destination_file=data)
elif message.content_type in types.ContentTypes.PHOTO:
data = await message.photo[-1].download(destination_file=data)
else:
await message.answer("This file type is not supported")
return
anime = await trace_bot.search(data)
except Exception:
await msg.edit_text(r"Ooops ¯\_(ツ)_/¯")
return

out = str()

if isinstance(anime.best_result.anilist, AniList):
if len(anime.best_result.anilist.title) > 0:
out += f"{md.hlink('Title:', anime.best_result.anilist.mal_url)}\n"
for k, v in anime.best_result.anilist.title.items():
if v is None:
continue
out += f" {k}: {v}\n"

if len(anime.best_result.anilist.synonyms) > 0:
out += "Synonyms:\n"
for syn in anime.best_result.anilist.synonyms:
out += f" {syn}\n"

if anime.best_result.anilist.is_adult:
out += "Hentai🔞\n"

if anime.best_result.episode:
out += f"Episode: {md.hbold(str(anime.best_result.episode))}\n"

if anime.best_result.anime_from:
out += f"Starting time of the matching scene: {md.hbold(str(dt.timedelta(seconds=int(anime.best_result.anime_from))))}\n"

out += f"Similarity: {md.hbold(anime.best_result.short_similarity())}\n"

await msg.edit_text(out)
await message.chat.do(types.ChatActions.UPLOAD_VIDEO)
await message.reply_video(anime.best_result.video)
out, kb = parse_text(anime)
await msg.edit_text(out, disable_web_page_preview=True, reply_markup=kb)
if not anime.best_result.anilist.is_adult:
await message.chat.do(types.ChatActions.UPLOAD_VIDEO)
await message.reply_video(anime.best_result.video)


@dp.message_handler(
Expand All @@ -72,11 +60,47 @@ async def search_anime_private(message: types.Message):
run_task=True,
)
async def search_anime_in_group(message: types.Message, reply: types.Message):
allow_list = [types.ContentType.PHOTO]
allow_list = [
types.ContentType.PHOTO,
types.ContentType.ANIMATION,
types.ContentType.VIDEO,
]
if reply.content_type not in allow_list:
await message.answer("This is not a screenshot")
return
await search_anime_private(reply)
await search_anime(reply)


def parse_text(anime_response: AnimeResponse) -> Tuple[str, types.InlineKeyboardMarkup]:
out = str()
kb = types.InlineKeyboardMarkup()
if isinstance(anime_response.best_result.anilist, AniList):
if len(anime_response.best_result.anilist.title) > 0:
out += "Title:\n"
kb.add(
types.InlineKeyboardButton(
"My Anime List", url=anime_response.best_result.anilist.mal_url
)
)
for k, v in anime_response.best_result.anilist.title.items():
if v is None:
continue
out += f" {k}: {v}\n"
if len(anime_response.best_result.anilist.synonyms) > 0:
out += "Synonyms:\n"
for syn in anime_response.best_result.anilist.synonyms:
out += f" {syn}\n"
if anime_response.best_result.anilist.is_adult:
out += "Hentai🔞\n"
if anime_response.best_result.episode:
episode = anime_response.best_result.episode
if isinstance(anime_response.best_result.episode, list):
episode = " | ".join(str(ep) for ep in anime_response.best_result.episode)
out += f"Episode: {md.hbold(str(episode))}\n"
if anime_response.best_result.anime_from:
out += f"Starting time of the matching scene: {md.hbold(str(dt.timedelta(seconds=int(anime_response.best_result.anime_from))))}\n"
out += f"Similarity: {md.hbold(anime_response.best_result.short_similarity())}\n"
return out, kb


if __name__ == "__main__":
Expand Down

0 comments on commit e22ed3a

Please sign in to comment.