Skip to content

Commit

Permalink
Fix profile estimate color, manga text
Browse files Browse the repository at this point in the history
  • Loading branch information
0x16c3 committed Sep 24, 2021
1 parent 9274017 commit a41d93f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
82 changes: 82 additions & 0 deletions cogs/api/recommend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# discord imports
import discord
from discord.ext import commands
import asyncio

# utilities
from ..utils import anilist
from .types import CUser, CAnime
from dataclasses import dataclass
from typing import Dict, List, Tuple


@dataclass(frozen=True)
class Relation:
media_id: int
mean_score: float
tags: List[str]
genres: List[str]
popularity: int
episodes: int

user_score: float


class Recommender:
def __init__(self, user: CUser) -> None:
self.user = user

@staticmethod
async def create(username: str) -> "Recommender":
pages, entries = await anilist.get(username, "anime_list", limit=50)

for page in range(2, pages.last + 1):
print(page)
_, entries_sub = await anilist.get(
username, "anime_list", page=page, limit=50
)
entries.extend(entries_sub)

relations: List[Relation] = []
genre_matrix: Dict[str, int] = {}
tag_matrix: Dict[str, int] = {}

for entry in entries:
if not hasattr(entry, "score"):
continue

media = entry.media
for genre in media.genres:
if genre not in genre_matrix:
genre_matrix[genre] = 1
continue

genre_matrix[genre] += 1
for tag in media.tags:
if tag not in tag_matrix:
tag_matrix[tag] = 1
continue

tag_matrix[tag] += 1

relations.append(
Relation(
media.id,
media.score.mean,
media.tags,
media.genres,
media.popularity,
media.episodes,
entry.score,
)
)

genre_matrix: List[Tuple[str, int]] = sorted(
genre_matrix.items(), key=lambda v: v[1]
)
genre_matrix.reverse()

tag_matrix: List[Tuple[str, int]] = sorted(
tag_matrix.items(), key=lambda v: v[1]
)
tag_matrix.reverse()
5 changes: 4 additions & 1 deletion cogs/api/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ async def send_embed(
loop = asyncio.get_event_loop()

estimate = await loop.run_in_executor(None, self.get_picture_color)
if isinstance(estimate, int):
estimate = [estimate, estimate, estimate]

colors = len(estimate) - 1

if colors < 0:
Expand Down Expand Up @@ -389,7 +392,7 @@ async def send_embed(
str(item.media.chapters)
if hasattr(item.media, "chapters")
else "???",
f"{str(item.media.chapters)} volumes. "
f"{str(item.media.volumes)} volumes. "
if hasattr(item.media, "volumes")
else "",
"\n Score ⭐: {}".format(listitem.score)
Expand Down
4 changes: 3 additions & 1 deletion cogs/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
CFavouritesUnion,
CStatisticsUnion,
)
from cogs.api.recommend import Recommender
from cogs.api.database import Database, database
from cogs.utils import *

Expand Down Expand Up @@ -97,7 +98,8 @@ async def eval(self, ctx, *, cmd):
"json": json,
"Controller": Controller,
"Feed": Feed,
"AnimeFeed": Activity,
"Activity": Activity,
"Recommender": Recommender,
"Database": Database,
"database": database,
"TinyDB": TinyDB,
Expand Down
2 changes: 1 addition & 1 deletion cogs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,4 @@ def get_debug_guild_id() -> Optional[List[int]]:
JIKAN
"""
anilist = anilist.AsyncClient()
""""""
""""""

0 comments on commit a41d93f

Please sign in to comment.