def test_generating_anime_media_list_entry(self): """ Tests generating an anime media list entry object :return: None """ data = self.generate_sample_anime_entry() self.assertEqual(data.id, Id({IdType.MYANIMELIST: 1})) self.assertEqual(data.title, Title({TitleType.ROMAJI: "Test"})) self.assertEqual(data.relations, [ Relation(Id({IdType.MYANIMELIST: 1}), MediaType.ANIME, Id({IdType.MYANIMELIST: 2}), MediaType.MANGA, RelationType.SEQUEL) ]) self.assertEqual(data.releasing_status, ReleasingStatus.FINISHED) self.assertEqual(data.releasing_start, Date(2018, 1, 1)) self.assertEqual(data.releasing_end, Date(2018, 4, 4)) self.assertEqual(data.episode_count, 12) self.assertEqual(data.episode_duration, 25) self.assertEqual(data.cover_url, "https://example.com/image.png") self.assertEqual(data.username, "namboy94") self.assertEqual(data.score, Score(55, ScoreType.PERCENTAGE)) self.assertEqual(data.consuming_status, ConsumingStatus.COMPLETED) self.assertEqual(data.episode_progress, 12) self.assertEqual(data.consuming_start, Date(2018, 1, 1)) self.assertEqual(data.consuming_end, Date(2018, 4, 4)) self.assertEqual(data.media_type, MediaType.ANIME)
def generate_sample_manga_user_data() -> MangaUserData: """ Generates a sample MangaUserData object :return: The generated MangaUserData object """ return MangaUserData(Id({IdType.MYANIMELIST: 1}), "namboy94", Score(55, ScoreType.PERCENTAGE), ConsumingStatus.COMPLETED, Date(2018, 1, 1), Date(2018, 4, 4), 100, 10)
def generate_sample_manga_data() -> MangaData: """ Generates a generic MangaData object :return: The generated manga data object """ return MangaData(Id( {IdType.MYANIMELIST: 1}), Title({TitleType.ROMAJI: "Test"}), [ Relation(Id({IdType.MYANIMELIST: 1}), MediaType.MANGA, Id({IdType.MYANIMELIST: 2}), MediaType.MANGA, RelationType.SEQUEL) ], ReleasingStatus.FINISHED, Date(2018, 1, 1), Date(2018, 4, 4), "https://example.com/image.png", 100, 10)
def test_generating_anime_user_entry_data(self): """ Tests generating an anime user entry data object :return: None """ data = self.generate_sample_anime_user_data() self.assertEqual(data.username, "namboy94") self.assertEqual(data.score, Score(55, ScoreType.PERCENTAGE)) self.assertEqual(data.consuming_status, ConsumingStatus.COMPLETED) self.assertEqual(data.episode_progress, 12) self.assertEqual(data.consuming_start, Date(2018, 1, 1)) self.assertEqual(data.consuming_end, Date(2018, 4, 4)) self.assertEqual(data.media_type, MediaType.ANIME)
def test_generating_manga_user_entry_data(self): """ Tests generating an manga user entry data object :return: None """ data = self.generate_sample_manga_user_data() self.assertEqual(data.username, "namboy94") self.assertEqual(data.score, Score(55, ScoreType.PERCENTAGE)) self.assertEqual(data.consuming_status, ConsumingStatus.COMPLETED) self.assertEqual(data.chapter_progress, 100) self.assertEqual(data.volume_progress, 10) self.assertEqual(data.consuming_start, Date(2018, 1, 1)) self.assertEqual(data.consuming_end, Date(2018, 4, 4)) self.assertEqual(data.media_type, MediaType.MANGA)
def _get_common_deserialized_components( cls, data: Dict[str, Optional[str or int or float or bool or Dict or List or Tuple or Set]]) \ -> Dict[str, Optional[str or int or float or bool or Dict or List or Tuple or Set]]: """ Deserializes the common child components of the data dictionary :param data: The data to deserialize :return: The deserialized dictionary """ deserialized = { "media_type": MediaType[data["media_type"]], "id": Id.deserialize(data["id"]), "title": Title.deserialize(data["title"]), "relations": list(map(lambda x: Relation.deserialize(x), data["relations"])), "releasing_status": ReleasingStatus[data["releasing_status"]], "cover_url": data["cover_url"] } for date in ["releasing_start", "releasing_end"]: date_data = data[date] if date_data is not None: deserialized[date] = Date.deserialize(date_data) else: deserialized[date] = None return deserialized
def _get_common_deserialized_components( cls, data: Dict[str, Optional[str or int or float or bool or Dict or List or Tuple or Set]]) \ -> Dict[str, Optional[str or int or float or bool or Dict or List or Tuple or Set]]: """ Deserializes the common child components of the data dictionary :param data: The data to deserialize :return: The deserialized dictionary """ deserialized = { "media_id": Id.deserialize(data["media_id"]), "media_type": MediaType[data["media_type"]], "username": data["username"], "score": Score.deserialize(data["score"]), "consuming_status": ConsumingStatus[data["consuming_status"]] } for date in ["consuming_start", "consuming_end"]: if data[date] is not None: deserialized[date] = Date.deserialize(data[date]) else: deserialized[date] = None return deserialized
def generate_sample_serialized_anime_user_data() \ -> Dict[str, Optional[str or int or float or bool or Dict or List or Tuple or Set]]: """ Generates a sample serialized AnimeUserData object :return: The serialized data """ return { "media_id": Id({ IdType.MYANIMELIST: 1 }).serialize(), "media_type": "ANIME", "username": "******", "score": Score(55, ScoreType.PERCENTAGE).serialize(), "consuming_status": "COMPLETED", "episode_progress": 12, "consuming_start": Date(2018, 1, 1).serialize(), "consuming_end": Date(2018, 4, 4).serialize() }
def test_generating_manga_data(self): """ Tests generating a manga data object :return: None """ data = self.generate_sample_manga_data() self.assertEqual(data.id, Id({IdType.MYANIMELIST: 1})) self.assertEqual(data.title, Title({TitleType.ROMAJI: "Test"})) self.assertEqual(data.relations, [ Relation(Id({IdType.MYANIMELIST: 1}), MediaType.MANGA, Id({IdType.MYANIMELIST: 2}), MediaType.MANGA, RelationType.SEQUEL) ]) self.assertEqual(data.releasing_status, ReleasingStatus.FINISHED) self.assertEqual(data.releasing_start, Date(2018, 1, 1)) self.assertEqual(data.releasing_end, Date(2018, 4, 4)) self.assertEqual(data.chapter_count, 100) self.assertEqual(data.volume_count, 10) self.assertEqual(data.cover_url, "https://example.com/image.png") self.assertEqual(data.media_type, MediaType.MANGA)
def generate_sample_serialized_anime_data() -> \ Dict[str, Optional[str or int or float or bool or Dict or List or Tuple or Set]]: """ Generates some sample serialized anime data :return: The serialized sample data """ return { "media_type": "ANIME", "id": Id({ IdType.MYANIMELIST: 1 }).serialize(), "title": Title({ TitleType.ROMAJI: "Test" }).serialize(), "relations": [ Relation(Id({IdType.MYANIMELIST: 1}), MediaType.ANIME, Id({IdType.MYANIMELIST: 2}), MediaType.MANGA, RelationType.SEQUEL).serialize() ], "releasing_status": ReleasingStatus.FINISHED.name, "releasing_start": Date(2018, 1, 1).serialize(), "releasing_end": Date(2018, 4, 4).serialize(), "episode_count": 12, "episode_duration": 25, "cover_url": "https://example.com/image.png" }
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)