def test_deserialization(self): """ Tests deserializing an ID object :return: None """ source = Id({IdType.MYANIMELIST: 1}) dest = Id({IdType.MYANIMELIST: 2}) data = { "source": source.serialize(), "source_type": "ANIME", "dest": dest.serialize(), "dest_type": "MANGA", "type": "SEQUEL" } self.assertEqual( Relation.deserialize(data), Relation(source, MediaType.ANIME, dest, MediaType.MANGA, RelationType.SEQUEL))
def test_serialization(self): """ Tests serializing an ID object :return: None """ ob = Id({IdType.MYANIMELIST: 1, IdType.ANILIST: 2}) data = ob.serialize() self.assertEqual(data, {"MYANIMELIST": 1, "ANILIST": 2, "KITSU": None})
def test_serialization(self): """ Tests serializing a Relation object :return: None """ source = Id({IdType.MYANIMELIST: 1}) dest = Id({IdType.MYANIMELIST: 2}) ob = Relation(source, MediaType.ANIME, dest, MediaType.MANGA, RelationType.SEQUEL) data = ob.serialize() self.assertEqual( data, { "source": source.serialize(), "source_type": "ANIME", "dest": dest.serialize(), "dest_type": "MANGA", "type": "SEQUEL" })
def __generate_media_user_data(media_type: MediaType, data: Dict[str, Any]) -> MediaUserData: """ Generates an Media User Data object from JSON data :param media_type: The media type to generate :param data: The data to parse as User Data :return: The generated MediaUserData object """ _id = Id({ IdType.ANILIST: data["media"]["id"], IdType.MYANIMELIST: data["media"]["idMal"] }) serialized = { "media_id": _id.serialize(), "media_type": media_type.name, "username": data["user"]["name"], "score": Score(data["score"], ScoreType.PERCENTAGE).serialize(), "consuming_status": data["status"], "episode_progress": data["progress"], "chapter_progress": data["progress"], "volume_progress": data["progressVolumes"] } for api_key, dict_key in { "startedAt": "consuming_start", "completedAt": "consuming_end" }.items(): try: serialized[dict_key] = Date(data[api_key]["year"], data[api_key]["month"], data[api_key]["day"]).serialize() except (TypeError, ValueError): serialized[dict_key] = None return MediaUserData.deserialize(serialized)
def __generate_media_data(media_type: MediaType, data: Dict[str, Any]) -> MediaData: """ Generates an MediaData object from a GraphQL result :param media_type: The media type to generate :param data: The data to convert into an AnimeData object :return: The generated AnimeData object """ _id = Id({ IdType.ANILIST: data["id"], IdType.MYANIMELIST: data["idMal"] }) title = Title({ TitleType.ROMAJI: data["title"]["romaji"], TitleType.ENGLISH: data["title"]["english"], TitleType.JAPANESE: data["title"]["native"], }) if title.get(TitleType.ENGLISH) is None: title.set(title.get(TitleType.ROMAJI), TitleType.ENGLISH) relations = [] for relation in data["relations"]["edges"]: dest_id = Id({ IdType.ANILIST: relation["node"]["id"], IdType.MYANIMELIST: relation["node"]["idMal"] }) dest_media_type = media_type rel_type = RelationType[relation["relationType"]] if rel_type == RelationType.ADAPTATION: if media_type == MediaType.ANIME: dest_media_type = MediaType.MANGA else: dest_media_type = MediaType.ANIME relations.append( Relation(_id, media_type, dest_id, dest_media_type, rel_type).serialize()) releasing_status = \ data["status"].replace("NOT_YET_RELEASED", "NOT_RELEASED") serialized = { "media_type": media_type.name, "id": _id.serialize(), "title": title.serialize(), "relations": relations, "releasing_status": releasing_status, "cover_url": data["coverImage"]["large"], "episode_count": data["episodes"], "episode_duration": data["duration"], "chapter_count": data["episodes"], "volume_count": data["episodes"] } for api_key, dict_key in { "startDate": "releasing_start", "endDate": "releasing_end" }.items(): try: serialized[dict_key] = Date(data[api_key]["year"], data[api_key]["month"], data[api_key]["day"]).serialize() except (TypeError, ValueError): serialized[dict_key] = None return MediaData.deserialize(serialized)