def test_serialization(self): """ Tests serializing an ID object :return: None """ ob = Title({TitleType.ROMAJI: "SnK", TitleType.ENGLISH: "AoT"}) data = ob.serialize() self.assertEqual( data, { "ROMAJI": "SnK", "ENGLISH": "AoT", "JAPANESE": None, "default": "ROMAJI" } )
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)