def from_dict(cls, data: Dict): try: correction = data.pop("correction") data = correction.pop("track") except KeyError: pass if isinstance(data["artist"], str): data["artist"] = dict(name=data["artist"]) data.update( dict(name=str(data["name"]), artist=Artist.from_dict(data["artist"]))) if "image" in data: data["image"] = list(map(Image.from_dict, data["image"])) if "top_tags" in data: data["top_tags"] = list(map(Tag.from_dict, data["top_tags"]["tag"])) if "wiki" in data: data["wiki"] = Wiki.from_dict(data["wiki"]) if "album" in data: data["album"] = Album.from_dict(data["album"]) if "attr" in data: data.update(data.pop("attr")) if "date" in data: date = data.pop("date") if isinstance(date, dict) and "timestamp" not in data: date.pop("text") data.update(date) return super(Track, cls).from_dict(data)
def setUp(self): self.artist = Artist.from_dict( dict( name="Guns N' Roses", mbid="eeb1195b-f213-4ce1-b28c-8565211f8e43", )) super(ArtistTests, self).setUp()
def test_get_top_artists_chart(self): result = Artist.get_top_artists_chart(limit=10, page=2) expected_params = { "limit": 10, "method": "chart.getTopArtists", "page": 2, } self.assertEqual(expected_params, result.params) self.assertIsInstance(result, ListModel) self.assertFixtureEqual("chart/get_top_artists", result.to_dict())
def test_search(self): result = Artist.search("gun") expected_params = { "artist": "gun", "limit": 50, "method": "artist.search", "page": 1, } self.assertEqual(expected_params, result.params) self.assertIsInstance(result, ListModel) self.assertFixtureEqual("artist/search", result.to_dict())
def test_find(self): result = Artist.find("Guns N' Roses") expected_params = { "artist": "Guns N' Roses", "autocorrect": True, "lang": "en", "method": "artist.getInfo", "username": None, } self.assertEqual(expected_params, result.params) self.assertIsInstance(result, Artist) self.assertFixtureEqual("artist/find", result.to_dict())
def test_get_top_tracks_by_country(self): result = Artist.get_top_artists_by_country(country="greece", page=1, limit=10) expected_params = { "country": "greece", "limit": 10, "method": "geo.getTopArtists", "page": 1, } self.assertEqual(expected_params, result.params) self.assertIsInstance(result, ListModel) self.assertFixtureEqual("geo/get_top_artists", result.to_dict())
def test_find_by_mbid(self): result = Artist.find_by_mbid( "eeb1195b-f213-4ce1-b28c-8565211f8e43", user="******", lang="it", ) expected_params = { "autocorrect": True, "lang": "it", "mbid": "eeb1195b-f213-4ce1-b28c-8565211f8e43", "method": "artist.getInfo", "username": "******", } self.assertEqual(expected_params, result.params) self.assertIsInstance(result, Artist) self.assertFixtureEqual("artist/find_by_mbid", result.to_dict())
def from_dict(cls, data: Dict): if isinstance(data.get("artist"), str): data["artist"] = {"name": data["artist"]} if "name" not in data and "text" in data: data["name"] = data.pop("text") if "artist" in data: data["artist"] = Artist.from_dict(data["artist"]) if "image" in data: data["image"] = list(map(Image.from_dict, data["image"])) if "tags" in data: data["tags"] = list(map(Tag.from_dict, data["tags"]["tag"])) if "tracks" in data: from pydrag.models.track import Track data["tracks"] = list(map(Track.from_dict, data["tracks"]["track"])) if "wiki" in data: data["wiki"] = Wiki.from_dict(data["wiki"]) if "attr" in data: data.update(data.pop("attr")) return super().from_dict(data)
def setUp(self): self.track = Track(artist=Artist(name="AC / DC"), name="Hells Bell") super().setUp()
def setUp(self): self.artist = Artist.from_dict( {"name": "Guns N' Roses", "mbid": "eeb1195b-f213-4ce1-b28c-8565211f8e43"} ) super().setUp()