Implement geo.getMetroHypeTrackChart, geo.getMetroTrackChart and geo.getMetroUniqueTrackChart. Closes #44.
This commit is contained in:
parent
a97c584059
commit
daa590b11d
55
pylast.py
55
pylast.py
|
@ -2158,7 +2158,7 @@ class Metro(_BaseObject):
|
||||||
|
|
||||||
return self.country
|
return self.country
|
||||||
|
|
||||||
def _get_chart(self, method, limit=None, from_date=None, to_date=None, cacheable=True):
|
def _get_chart(self, method, tag="artist", limit=None, from_date=None, to_date=None, cacheable=True):
|
||||||
"""Internal helper for getting geo charts."""
|
"""Internal helper for getting geo charts."""
|
||||||
params = self._get_params()
|
params = self._get_params()
|
||||||
if limit: params["limit"] = limit
|
if limit: params["limit"] = limit
|
||||||
|
@ -2166,49 +2166,76 @@ class Metro(_BaseObject):
|
||||||
params["from"] = from_date
|
params["from"] = from_date
|
||||||
params["to"] = to_date
|
params["to"] = to_date
|
||||||
|
|
||||||
doc = self._request("geo.getMetroArtistChart", cacheable, params)
|
doc = self._request(method, cacheable, params)
|
||||||
|
|
||||||
seq = []
|
seq = []
|
||||||
for node in doc.getElementsByTagName("artist"):
|
for node in doc.getElementsByTagName(tag):
|
||||||
item = Artist(_extract(node, "name"), self.network)
|
if tag == "artist":
|
||||||
|
item = Artist(_extract(node, "name"), self.network)
|
||||||
|
elif tag == "track":
|
||||||
|
title = _extract(node, "name")
|
||||||
|
artist = _extract_element_tree(node).get('artist')['name']
|
||||||
|
item = Track(artist, title, self.network)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
weight = _number(_extract(node, "listeners"))
|
weight = _number(_extract(node, "listeners"))
|
||||||
seq.append(TopItem(item, weight))
|
seq.append(TopItem(item, weight))
|
||||||
|
|
||||||
return seq
|
return seq
|
||||||
|
|
||||||
def get_artist_chart(self, limit=None, from_date=None, to_date=None, cacheable=True):
|
def get_artist_chart(self, tag="artist", limit=None, from_date=None, to_date=None, cacheable=True):
|
||||||
"""Get a chart of artists for a metro.
|
"""Get a chart of artists for a metro.
|
||||||
Parameters:
|
Parameters:
|
||||||
from_date (Optional) : Beginning timestamp of the weekly range requested
|
from_date (Optional) : Beginning timestamp of the weekly range requested
|
||||||
to_date (Optional) : Ending timestamp of the weekly range requested
|
to_date (Optional) : Ending timestamp of the weekly range requested
|
||||||
limit (Optional) : The number of results to fetch per page. Defaults to 50.
|
limit (Optional) : The number of results to fetch per page. Defaults to 50.
|
||||||
"""
|
"""
|
||||||
return self._get_chart("geo.getMetroArtistChart", limit, from_date, to_date, cacheable)
|
return self._get_chart("geo.getMetroArtistChart", tag = tag, limit = limit, from_date = from_date, to_date = to_date, cacheable = cacheable)
|
||||||
|
|
||||||
def get_hype_artist_chart(self, limit=None, from_date=None, to_date=None, cacheable=True):
|
def get_hype_artist_chart(self, tag="artist", limit=None, from_date=None, to_date=None, cacheable=True):
|
||||||
"""Get a chart of hyped (up and coming) artists for a metro.
|
"""Get a chart of hyped (up and coming) artists for a metro.
|
||||||
Parameters:
|
Parameters:
|
||||||
from_date (Optional) : Beginning timestamp of the weekly range requested
|
from_date (Optional) : Beginning timestamp of the weekly range requested
|
||||||
to_date (Optional) : Ending timestamp of the weekly range requested
|
to_date (Optional) : Ending timestamp of the weekly range requested
|
||||||
limit (Optional) : The number of results to fetch per page. Defaults to 50.
|
limit (Optional) : The number of results to fetch per page. Defaults to 50.
|
||||||
"""
|
"""
|
||||||
return self._get_chart("geo.getMetroHypeArtistChart", limit, from_date, to_date, cacheable)
|
return self._get_chart("geo.getMetroHypeArtistChart", tag = tag, limit = limit, from_date = from_date, to_date = to_date, cacheable = cacheable)
|
||||||
|
|
||||||
def get_unique_artist_chart(self, limit=None, from_date=None, to_date=None, cacheable=True):
|
def get_unique_artist_chart(self, tag="artist", limit=None, from_date=None, to_date=None, cacheable=True):
|
||||||
"""Get a chart of the artists which make that metro unique.
|
"""Get a chart of the artists which make that metro unique.
|
||||||
Parameters:
|
Parameters:
|
||||||
from_date (Optional) : Beginning timestamp of the weekly range requested
|
from_date (Optional) : Beginning timestamp of the weekly range requested
|
||||||
to_date (Optional) : Ending timestamp of the weekly range requested
|
to_date (Optional) : Ending timestamp of the weekly range requested
|
||||||
limit (Optional) : The number of results to fetch per page. Defaults to 50.
|
limit (Optional) : The number of results to fetch per page. Defaults to 50.
|
||||||
"""
|
"""
|
||||||
return self._get_chart("geo.getMetroUniqueArtistChart", limit, from_date, to_date, cacheable)
|
return self._get_chart("geo.getMetroUniqueArtistChart", tag = tag, limit = limit, from_date = from_date, to_date = to_date, cacheable = cacheable)
|
||||||
|
|
||||||
|
def get_track_chart(self, tag="track", limit=None, from_date=None, to_date=None, cacheable=True):
|
||||||
|
"""Get a chart of tracks for a metro.
|
||||||
|
Parameters:
|
||||||
|
from_date (Optional) : Beginning timestamp of the weekly range requested
|
||||||
|
to_date (Optional) : Ending timestamp of the weekly range requested
|
||||||
|
limit (Optional) : The number of results to fetch per page. Defaults to 50.
|
||||||
|
"""
|
||||||
|
return self._get_chart("geo.getMetroTrackChart", tag = tag, limit = limit, from_date = from_date, to_date = to_date, cacheable = cacheable)
|
||||||
|
|
||||||
# TODO?
|
def get_hype_track_chart(self, tag="track", limit=None, from_date=None, to_date=None, cacheable=True):
|
||||||
# geo.getMetroHypeTrackChart
|
"""Get a chart of tracks for a metro.
|
||||||
# geo.getMetroTrackChart
|
Parameters:
|
||||||
# geo.getMetroUniqueTrackChart
|
from_date (Optional) : Beginning timestamp of the weekly range requested
|
||||||
|
to_date (Optional) : Ending timestamp of the weekly range requested
|
||||||
|
limit (Optional) : The number of results to fetch per page. Defaults to 50.
|
||||||
|
"""
|
||||||
|
return self._get_chart("geo.getMetroHypeTrackChart", tag = tag, limit = limit, from_date = from_date, to_date = to_date, cacheable = cacheable)
|
||||||
|
|
||||||
|
def get_unique_track_chart(self, tag="track", limit=None, from_date=None, to_date=None, cacheable=True):
|
||||||
|
"""Get a chart of tracks for a metro.
|
||||||
|
Parameters:
|
||||||
|
from_date (Optional) : Beginning timestamp of the weekly range requested
|
||||||
|
to_date (Optional) : Ending timestamp of the weekly range requested
|
||||||
|
limit (Optional) : The number of results to fetch per page. Defaults to 50.
|
||||||
|
"""
|
||||||
|
return self._get_chart("geo.getMetroUniqueTrackChart", tag = tag, limit = limit, from_date = from_date, to_date = to_date, cacheable = cacheable)
|
||||||
|
|
||||||
class Library(_BaseObject):
|
class Library(_BaseObject):
|
||||||
"""A user's Last.fm library."""
|
"""A user's Last.fm library."""
|
||||||
|
|
|
@ -916,9 +916,9 @@ class TestPyLast(unittest.TestCase):
|
||||||
self.assertLess(start, end)
|
self.assertLess(start, end)
|
||||||
|
|
||||||
|
|
||||||
def helper_get_metro_and_dates(self, function_name):
|
def helper_geo_chart(self, function_name, expected_type = pylast.Artist):
|
||||||
# Arrange
|
# Arrange
|
||||||
metro = self.network.get_metro("Salamanca", "Spain")
|
metro = self.network.get_metro("Madrid", "Spain")
|
||||||
dates = self.network.get_metro_weekly_chart_dates()
|
dates = self.network.get_metro_weekly_chart_dates()
|
||||||
(from_date, to_date) = dates[0]
|
(from_date, to_date) = dates[0]
|
||||||
|
|
||||||
|
@ -931,22 +931,37 @@ class TestPyLast(unittest.TestCase):
|
||||||
# Assert
|
# Assert
|
||||||
self.assertEqual(len(chart), 1)
|
self.assertEqual(len(chart), 1)
|
||||||
self.assertEqual(type(chart[0]), pylast.TopItem)
|
self.assertEqual(type(chart[0]), pylast.TopItem)
|
||||||
self.assertEqual(type(chart[0].item), pylast.Artist)
|
self.assertEqual(type(chart[0].item), expected_type)
|
||||||
|
|
||||||
|
|
||||||
def test_get_metro_artist_chart(self):
|
def test_get_metro_artist_chart(self):
|
||||||
# Arrange/Act/Assert
|
# Arrange/Act/Assert
|
||||||
self.helper_get_metro_and_dates("get_artist_chart")
|
self.helper_geo_chart("get_artist_chart")
|
||||||
|
|
||||||
|
|
||||||
def test_get_metro_hype_artist_chart(self):
|
def test_get_metro_hype_artist_chart(self):
|
||||||
# Arrange/Act/Assert
|
# Arrange/Act/Assert
|
||||||
self.helper_get_metro_and_dates("get_hype_artist_chart")
|
self.helper_geo_chart("get_hype_artist_chart")
|
||||||
|
|
||||||
|
|
||||||
def test_get_metro_unique_artist_chart(self):
|
def test_get_metro_unique_artist_chart(self):
|
||||||
# Arrange/Act/Assert
|
# Arrange/Act/Assert
|
||||||
self.helper_get_metro_and_dates("get_unique_artist_chart")
|
self.helper_geo_chart("get_unique_artist_chart")
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_metro_track_chart(self):
|
||||||
|
# Arrange/Act/Assert
|
||||||
|
self.helper_geo_chart("get_track_chart", expected_type = pylast.Track)
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_metro_hype_track_chart(self):
|
||||||
|
# Arrange/Act/Assert
|
||||||
|
self.helper_geo_chart("get_hype_track_chart", expected_type = pylast.Track)
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_metro_unique_track_chart(self):
|
||||||
|
# Arrange/Act/Assert
|
||||||
|
self.helper_geo_chart("get_unique_track_chart", expected_type = pylast.Track)
|
||||||
|
|
||||||
|
|
||||||
def test_geo_get_metros(self):
|
def test_geo_get_metros(self):
|
||||||
|
|
Loading…
Reference in a new issue