Exemple #1
0
def AnimeList(List: list):
    AnimeLists = AnimeListsStruct.AnimeLists()
    size_list = len(List)
    completedList = 0
    for anime in List:
        node: dict = anime.get("node")
        id = node.get("id")
        print(f"Formatting MAL id: {id}")
        title = node.get("title")
        main_picture = node.get("main_picture")
        medium_picture = main_picture.get("medium")
        large_picture = main_picture.get("large")

        list_status: dict = anime.get("list_status")
        status = list_status.get("status")
        score = list_status.get("score")
        num_episodes_watched = list_status.get("num_episodes_watched")
        is_rewatching = list_status.get("is_rewatching")
        updated_at = list_status.get("updated_at")

        PureAnime = AnimeStruct.Anime()
        MetaID = GenericStruct.MetaID(PluginName="MAL", id=str(id))
        PureAnime.id.list.append(MetaID)

        Name = GenericStruct.Text(Text=title, Localization=DefaultLocalization)
        PureAnime.names.list.append(Name)

        Medium_Image = GenericStruct.Image(url=medium_picture)
        Large_Image = GenericStruct.Image(url=large_picture)
        # Todo: save the images as well, need to develop new cache
        PureAnime.images.list.append(Medium_Image)
        PureAnime.images.list.append(Large_Image)

        State_Mapper = {
            'watching': AnimeStruct.LibraryState.Watching,
            'completed': AnimeStruct.LibraryState.Completed,
            'on_hold': AnimeStruct.LibraryState.Hold,
            'dropped': AnimeStruct.LibraryState.Dropped,
            'plan_to_watch': AnimeStruct.LibraryState.PlanToWatch
        }
        updated_at_datetime = datetime.datetime.fromisoformat(updated_at)
        PureAnime.libraryStatus = AnimeStruct.LibraryStatus(
            state=State_Mapper[status],
            episodesWatched=num_episodes_watched,
            lastUpdated=updated_at_datetime)

        PureAnime.personalRating.rated = score

        AnimeLists.append(anime=PureAnime)
        completedList += 1
        print(f"Formatted {completedList} of {size_list}")

    return AnimeLists
 def namesMetadata(namesList: List[dict]):
     names = AnimeStruct.Names()
     for name in namesList:
         text = name.get('text')
         localization = localizationMetadata(name.get('localization'))
         Name = GenericStruct.Text(text, localization)
         names.list.append(Name)
     return names
Exemple #3
0
def AnimeMetadata(metadata, oldAnime: AnimeStruct.Anime) -> AnimeStruct.Anime:
    Anime = AnimeStruct.Anime()
    Anime.hash = oldAnime.hash

    id: str = str(metadata.get("id"))
    title: str = metadata.get("title")
    main_picture: dict = metadata.get("main_picture")
    medium_picture: str = main_picture.get("medium")
    large_picture: str = main_picture.get("large")
    alternative_titles: dict = metadata.get("alternative_titles")
    synonyms: List[str] = alternative_titles.pop("synonyms")
    start_date: str = metadata.get("start_date")
    end_date: str = metadata.get("end_date")
    synopsis: str = metadata.get("synopsis")
    mean: float = float(metadata.get("mean", 0))
    rank: int = int(metadata.get("rank", 0))
    popularity: int = int(metadata.get("popularity"))
    num_list_users: str = metadata.get("num_list_users")
    num_scoring_users: str = metadata.get("num_scoring_users")
    nsfw: str = metadata.get("nsfw")
    created_at: str = metadata.get("created_at")
    updated_at: str = metadata.get("updated_at")
    media_type: str = metadata.get("media_type")
    status: str = metadata.get("status")
    genres: List[dict] = metadata.get("genres")
    my_list_status: dict = metadata.get("my_list_status")
    list_status: str = my_list_status.get("status")
    score: float = float(my_list_status.get("score"))
    num_episodes_watched: int = int(my_list_status.get("num_episodes_watched"))
    is_rewatching: bool = bool(my_list_status.get("is_rewatching"))
    updated_at: str = my_list_status.get("updated_at")
    num_episodes: int = int(metadata.get("num_episodes"))
    start_season: dict = metadata.get("start_season")
    if start_season:
        year: int = int(start_season.get("year"))
        season: str = start_season.get("season")
    broadcast: dict = metadata.get("broadcast")
    if broadcast:
        day_of_the_week: str = broadcast.get("day_of_the_week")
        start_time: str = broadcast.get("start_time")
    source: str = metadata.get("source")
    average_episode_duration: str = str(
        metadata.get("average_episode_duration"))
    rating: str = str(metadata.get("rating"))
    pictures: List[dict] = metadata.get("pictures")
    background: str = metadata.get("background")
    related_anime: List = metadata.get(
        "related_anime")  # Todo: get more info on this
    related_manga: List = metadata.get("related_manga")
    recommendations: List[dict] = metadata.get(
        "recommendations")  # Note To Self: I'm ignoring this for now
    studios: List[dict] = metadata.get("studios")
    statistics: dict = metadata.get("statistics")
    statistics_status: dict = statistics.get("status")
    watching: str = statistics_status.get("watching")
    completed: str = statistics_status.get("completed")
    on_hold: str = statistics_status.get("on_hold")
    dropped: str = statistics_status.get("dropped")
    plan_to_watch: str = statistics_status.get("plan_to_watch")
    num_list_users: str = str(statistics_status.get("num_list_users"))

    if nsfw != "white" and nsfw != "gray":
        raise Exception("First time seeing Non White, pls help")
    ageRatingDict = {"pg_13": 13}
    Anime.ageRating = AnimeStruct.AgeRating(Age=ageRatingDict["pg_13"],
                                            Tag=rating)

    status_dict: AnimeStruct.ReleaseStatus = {
        "currently_airing": AnimeStruct.ReleaseStatus.ONGOING,
        "not_yet_aired": AnimeStruct.ReleaseStatus.SCHEDULED,
        "finished_airing": AnimeStruct.ReleaseStatus.COMPLETED
    }
    Anime.status = status_dict[status]

    Anime.publicRating = AnimeStruct.Rating(rated=mean,
                                            rank=rank,
                                            popularity=popularity)
    Anime.publicRating.hash = oldAnime.publicRating.hash

    libraryState_dict: AnimeStruct.LibraryState = {
        "watching": AnimeStruct.LibraryState.Watching,
        "on_hold": AnimeStruct.LibraryState.Hold,
        "dropped": AnimeStruct.LibraryState.Dropped,
        "plan_to_watch": AnimeStruct.LibraryState.PlanToWatch,
        "completed": AnimeStruct.LibraryState.Completed
    }
    Anime.libraryStatus = AnimeStruct.LibraryStatus(
        state=libraryState_dict[list_status],
        episodesWatched=num_episodes_watched,
        lastUpdated=datetime.datetime.fromisoformat(updated_at))
    Anime.libraryStatus.hash = oldAnime.libraryStatus.hash

    Anime.personalRating = AnimeStruct.Rating(rated=score)
    Anime.personalRating.hash = oldAnime.personalRating.hash

    # Todo: add new id if the metadata provider differs from the initial id provider
    Anime.id = oldAnime.id

    # Todo: maybe add a script_Dic to map languages to script (Latin, japanese only)
    names = AnimeStruct.Names()
    Name = GenericStruct.Text(Text=title, Localization=DefaultLocalization)
    names.list.append(Name)
    for language in alternative_titles:
        title = alternative_titles[language]
        Country = "Unknown"
        Language = pycountry.languages.get(alpha_2=language).name
        Script = "Unknown"
        Localization = GenericStruct.Localization(Language=Language,
                                                  Script=Script)
        Name = GenericStruct.Text(Text=title, Localization=DefaultLocalization)
        names.list.append(Name)
    for title in synonyms:
        Language = "Unknown"
        Script = "Unknown"
        Localization = GenericStruct.Localization(Language=Language,
                                                  Script=Script)
        Name = GenericStruct.Text(Text=title, Localization=DefaultLocalization)
        names.list.append(Name)
    Anime.names = names
    Anime.names.hash = oldAnime.names.hash

    images = AnimeStruct.Images()
    if medium_picture:
        Medium_Image = GenericStruct.Image(url=medium_picture)
        images.list.append(Medium_Image)
    if large_picture:
        Large_Image = GenericStruct.Image(url=large_picture)
        images.list.append(Large_Image)
    for image_info in pictures:
        Medium_Image = GenericStruct.Image(url=image_info.get("medium"))
        if Medium_Image:
            images.list.append(Medium_Image)
        Large_Image = GenericStruct.Image(url=image_info.get("large"))
        if Large_Image:
            images.list.append(Large_Image)
    Anime.images = images
    Anime.images.hash = oldAnime.images.hash

    tags = AnimeStruct.Tags()
    # Todo: See if exists in database a tag with same name but different aliases
    tags.list = [AnimeStruct.Tag(name=genre.get("name")) for genre in genres]
    Anime.tags = tags
    Anime.tags.hash = oldAnime.tags.hash

    Anime.description = AnimeStruct.Description(synopsis)
    Anime.description.hash = oldAnime.description.hash

    typeDict = {
        "tv": AnimeStruct.AnimeType.TV,
        "ona": AnimeStruct.AnimeType.ONA,
        "ova": AnimeStruct.AnimeType.OVA,
        "special": AnimeStruct.AnimeType.SPECIAL,
        "unknown": AnimeStruct.AnimeType.UNKNOWN,
        "movie": AnimeStruct.AnimeType.MOVIE
    }
    Anime.type = typeDict[media_type]

    # episodes = AnimeStruct.Episodes()
    # for episode_number in range(num_episodes):
    #     text = AnimeStruct.Text("Unknown", DefaultLocalization)
    #     names = AnimeStruct.Names()
    #     names.list.append(text)
    #     episode = AnimeStruct.Episode(names=names)
    #     episode.type = AnimeStruct.EpisodeType.UNKNOWN
    #     episodes.list.append(episode)
    # Anime.episodes = episodes
    # Anime.episodes.hash = oldAnime.episodes.hash

    runnings = AnimeStruct.Runnings()
    since = to = None
    strpTypeDict = {1: "%Y", 2: "%Y-%m", 3: "%Y-%m-%d"}
    if start_date:
        strptype = len(start_date.split("-"))
        since = datetime.datetime.strptime(start_date, strpTypeDict[strptype])
    if end_date:
        strptype = len(end_date.split("-"))
        to = datetime.datetime.strptime(end_date, strpTypeDict[strptype])
    runnings.list.append(AnimeStruct.Running(since=since, to=to))
    Anime.runnings = runnings
    Anime.runnings.hash = oldAnime.runnings.hash

    seasonDict = {
        "winter": AnimeStruct.Seasons.Winter,
        "spring": AnimeStruct.Seasons.Spring,
        "summer": AnimeStruct.Seasons.Summer,
        "fall": AnimeStruct.Seasons.Fall
    }

    if start_season:
        Anime.season = AnimeStruct.Season(Year=AnimeStruct.Year(year),
                                          SeasonName=seasonDict[season])

    studioss = AnimeStruct.Studios()
    for studio in studios:
        name = studio.get("name")
        names = AnimeStruct.Names()
        names.list.append(
            AnimeStruct.Text(Text=name, Localization=DefaultLocalization))
        studioss.list.append(AnimeStruct.Studio(names))
    Anime.studios = studioss
    Anime.studios.hash = oldAnime.studios.hash

    return Anime
Exemple #4
0
from typing import List
import pycountry
import datetime
import Core.Structures.List as AnimeListsStruct
import Core.Structures.Anime as AnimeStruct
import Core.Structures.Generic as GenericStruct

DefaultCountry = pycountry.countries.get(name='Japan').name
DefaultLanguage = pycountry.languages.get(name='Japanese').name
DefaultScript = pycountry.scripts.get(name='Latin').name
DefaultTag = 'jpn-Latn'
DefaultLocalization = GenericStruct.Localization(Tag=DefaultTag)


def AnimeList(List: list):
    AnimeLists = AnimeListsStruct.AnimeLists()
    size_list = len(List)
    completedList = 0
    for anime in List:
        node: dict = anime.get("node")
        id = node.get("id")
        print(f"Formatting MAL id: {id}")
        title = node.get("title")
        main_picture = node.get("main_picture")
        medium_picture = main_picture.get("medium")
        large_picture = main_picture.get("large")

        list_status: dict = anime.get("list_status")
        status = list_status.get("status")
        score = list_status.get("score")
        num_episodes_watched = list_status.get("num_episodes_watched")
 def localizationMetadata(localization: dict):
     tag = None
     if localization.get('tag'):
         tag = localization.get('tag')
     return GenericStruct.Localization(tag)
def AnimeMetadata(metadata, oldAnime: AnimeStruct.Anime) -> AnimeStruct.Anime:
    Anime = AnimeStruct.Anime()
    Anime.hash = oldAnime.hash

    id: str = str(metadata.get("id"))
    # Todo: Add restrictions
    # Todo: Add releases
    ageRatingsList: List[dict] = metadata.get("ageRatings")
    status = metadata.get("status")
    # Todo: Add relations
    # Todo: Add partOfCanonicals
    genresList: List[dict] = metadata.get("genres")
    # Todo: Add soundtracks
    voiceActingsList: List[dict] = metadata.get("voiceActings")
    staffList: List[dict] = metadata.get("staff")
    starringList: List[dict] = metadata.get("starring")
    namesList: List[dict] = metadata.get("names")
    aliasesList: List[dict] = metadata.get("aliases")
    descriptionsList: List[dict] = metadata.get("descriptions")
    imagesList: List[dict] = metadata.get("images")
    crossrefsList: List[dict] = metadata.get("crossrefs")
    websitesList: List[dict] = metadata.get("websites")
    selfLink = metadata.get("selfLink")
    animeType = metadata.get("type")
    videosList: List[dict] = metadata.get("videos")
    episodesList: List[dict] = metadata.get("episodes")
    runningsList: List[dict] = metadata.get("runnings")

    def localizationMetadata(localization: dict):
        tag = None
        if localization.get('tag'):
            tag = localization.get('tag')
        return GenericStruct.Localization(tag)

    def namesMetadata(namesList: List[dict]):
        names = AnimeStruct.Names()
        for name in namesList:
            text = name.get('text')
            localization = localizationMetadata(name.get('localization'))
            Name = GenericStruct.Text(text, localization)
            names.list.append(Name)
        return names

    if ageRatingsList:
        ageRating = ageRatingsList[0]
        Anime.ageRating = AnimeStruct.AgeRating(Age=ageRating.get('age'),
                                                Tag=ageRating.get('tag'))
    if status:
        Anime.status = AnimeStruct.ReleaseStatus[status]
    if genresList:
        tags = AnimeStruct.Tags()
        for genre in genresList:
            # Todo: See if exists in database a tag with same name but different aliases
            tag = AnimeStruct.Tag(name=genre.get("names")[0].get("text"))
            tags.list.append(tag)
        Anime.tags = tags
    if voiceActingsList:
        voiceActings = AnimeStruct.VoiceActings()
        voiced = None
        actor = None
        for voiceActing in voiceActingsList:
            Voiced = voiceActing.get('voiced')
            Actor = voiceActing.get('actor')
            if Voiced:
                birthday = Voiced.get('birthday')
                # Note To Self: want example of how it comes out
                country = Voiced.get('hometown')
                gender = PersonStruct.Gender[Voiced.get('gender')]
                age = Voiced.get('age')
                name = None
                for tryname in Voiced.get('names'):
                    if tryname.get('localization').get(
                            'script') and tryname.get('localization').get(
                                'script').get('code') == 'Latn':
                        name = tryname.get('text')
                if name is None:
                    name = Voiced.get('names')[0].get('text')
                voiced = PersonStruct.Person(name=name,
                                             birthday=birthday,
                                             country=country,
                                             gender=gender,
                                             age=age)
            if Actor:
                birthday = Actor.get('birthday')
                # Note To Self: want example of how it comes out
                country = Actor.get('hometown')
                gender = PersonStruct.Gender[Actor.get('gender')]
                age = Actor.get('age')
                name = None
                for tryname in Actor.get('names'):
                    if tryname.get('localization').get('script'):
                        if tryname.get('localization').get('script').get(
                                'code') == 'Latn':
                            name = tryname.get('text')
                            break
                if name is None:
                    name = Actor.get('names')[0].get('text')
                actor = PersonStruct.Person(name=name,
                                            birthday=birthday,
                                            country=country,
                                            gender=gender,
                                            age=age)
                voiceActing = AnimeStruct.VoiceActing(actor=actor,
                                                      voiced=voiced)
            voiceActings.list.append(voiceActing)
        Anime.voiceActings = voiceActings
    if staffList:
        studios = AnimeStruct.Studios()
        for staff in staffList:
            # Todo: investigate studio tags
            studioNames = AnimeStruct.Names()
            if staff.get('role').get('tag') == 'animation-works':
                Name = namesMetadata(staff.get('collaborator').get('names'))
                studioNames.list.append(Name)
        Anime.status = studios
    if starringList:
        for starring in starringList:
            relation = starring.get('relation')
            Character = starring.get('character')
            birthday = Character.get('birthday')
            # Note To Self: want example of how it comes out
            country = Character.get('hometown')
            gender = PersonStruct.Gender[Character.get('gender')]
            age = Character.get('age')
            name = None
            for tryname in Character.get('names'):
                if tryname.get('localization').get('script'):
                    if tryname.get('localization').get('script').get(
                            'code') == 'Latn':
                        name = tryname.get('text')
                        break
            if name is None:
                name = Character.get('names')[0].get('text')
            character = PersonStruct.Person(name=name,
                                            birthday=birthday,
                                            country=country,
                                            gender=gender,
                                            age=age)

    if namesList:
        Anime.names = namesMetadata(namesList)
    if aliasesList:
        names = namesMetadata(aliasesList)
        Anime.names.list.extend(names.list)
    if descriptionsList:
        if len(descriptionsList) > 1:
            raise Exception('Need to add localizations to descriptions')
        Anime.description = AnimeStruct.Description(
            descriptionsList[0].get('text'))
    if imagesList:
        files_type_priority = {'webp': 0, "png": 1, 'jpg': 2}
        images = GenericStruct.Images()
        for image in imagesList:
            type = image.get('type')
            files: dict = image.get('image').get('files')
            file_list = [file.get('publicUri') for file in files]
            file_list.sort(
                key=lambda i: files_type_priority.get(i.split('.')[-1], 100))
            images.list.append(GenericStruct.Image(file_list[0]))
        Anime.images = images
    # Note To Self: I'm ignoring crossrefs, may be needed in the future tho
    # Note To Self: I'm ignoring websites, may be needed in the future tho
    # Note To Self: I'm ignoring selflink, may be needed in the future tho
    Anime.type = AnimeStruct.AnimeType[animeType]
    if videosList:
        videos = AnimeStruct.Videos()
        for video in videosList:
            url = video.get('video').get('files')[0].get('publicUri')
            Video = AnimeStruct.Video(
                url=url, type=AnimeStruct.VideoType[video.get('type')])
            videos.list.append(Video)
        Anime.videos = videos
    if episodesList:
        episodes = AnimeStruct.Episodes()
        for episode in episodesList:
            type = AnimeStruct.EpisodeType[episode.get('type')]
            index = episode.get('identifier')
            releasedate = None
            if episode.get('releaseDate'):
                # '2020-12-04T00:00:00Z'
                releasedate = datetime.datetime.strptime(
                    episode.get('releaseDate')[:-2], "%Y-%m-%dT%H:%M:%S")
            names = namesMetadata(episode.get('names'))
            # Ignoring AgeRating, websites and videos
            Episode = AnimeStruct.Episode(names=names,
                                          releaseDate=releasedate,
                                          type=type,
                                          index=index)
            episodes.list.append(Episode)
        Anime.episodes = episodes
    if runningsList:
        runnings = AnimeStruct.Runnings()
        for running in runningsList:
            since = datetime.datetime.strptime(
                runningsList[0].get('from')[:-2], "%Y-%m-%dT%H:%M:%S")
            if runningsList[0].get('to'):
                to = datetime.datetime.strptime(runningsList[0].get('to')[:-2],
                                                "%Y-%m-%dT%H:%M:%S")
            else:
                to = datetime.datetime.utcfromtimestamp(0)
            Running = AnimeStruct.Running(since=since, to=to)
            runnings.list.append(Running)
        Anime.runnings = runnings

    Anime.libraryStatus = oldAnime.libraryStatus
    Anime.id = oldAnime.id
    Anime.ageRating.hash = oldAnime.ageRating.hash
    Anime.publicRating.hash = oldAnime.publicRating.hash
    Anime.personalRating.hash = oldAnime.personalRating.hash
    Anime.names.hash = oldAnime.names.hash
    Anime.images.hash = oldAnime.images.hash
    Anime.soundtracks.hash = oldAnime.soundtracks.hash
    Anime.voiceActings.hash = oldAnime.voiceActings.hash
    Anime.crossRefs.hash = oldAnime.crossRefs.hash
    Anime.videos.hash = oldAnime.videos.hash
    Anime.tags.hash = oldAnime.tags.hash
    Anime.description.hash = oldAnime.description.hash
    Anime.episodes.hash = oldAnime.episodes.hash
    Anime.runnings.hash = oldAnime.runnings.hash
    Anime.studios.hash = oldAnime.studios.hash

    return Anime