mirror of
https://github.com/vale981/clay
synced 2025-03-05 09:31:40 -05:00
v2:Added async methods to get stations data
This commit is contained in:
parent
275b826e0c
commit
b38c97cc64
1 changed files with 45 additions and 4 deletions
47
clay/gp.py
47
clay/gp.py
|
@ -12,6 +12,8 @@ from gmusicapi.clients import Mobileclient
|
||||||
from clay.eventhook import EventHook
|
from clay.eventhook import EventHook
|
||||||
from clay.log import logger
|
from clay.log import logger
|
||||||
|
|
||||||
|
STATION_FETCH_LEN = 50
|
||||||
|
|
||||||
def asynchronous(func):
|
def asynchronous(func):
|
||||||
"""
|
"""
|
||||||
Decorates a function to become asynchronous.
|
Decorates a function to become asynchronous.
|
||||||
|
@ -355,7 +357,8 @@ class Station(object):
|
||||||
"""
|
"""
|
||||||
Model that represents specific station on Google Play Music.
|
Model that represents specific station on Google Play Music.
|
||||||
"""
|
"""
|
||||||
def __init__(self, station_id):
|
def __init__(self, station_id, name):
|
||||||
|
self.name = name
|
||||||
self._id = station_id
|
self._id = station_id
|
||||||
self._tracks = []
|
self._tracks = []
|
||||||
self._tracks_loaded = False
|
self._tracks_loaded = False
|
||||||
|
@ -372,9 +375,12 @@ class Station(object):
|
||||||
Fetch tracks related to this station and
|
Fetch tracks related to this station and
|
||||||
populate it with :class:`Track` instances.
|
populate it with :class:`Track` instances.
|
||||||
"""
|
"""
|
||||||
data = gp.mobile_client.get_station_tracks(self.id, 100)
|
data = gp.mobile_client.get_station_tracks(self.id, STATION_FETCH_LEN)
|
||||||
self._tracks = Track.from_data(data, Track.SOURCE_STATION, many=True)
|
self._tracks = Track.from_data(data, Track.SOURCE_STATION, many=True)
|
||||||
self._tracks_loaded = True
|
self._tracks_loaded = True
|
||||||
|
return self
|
||||||
|
|
||||||
|
load_tracks_async = asynchronous(load_tracks)
|
||||||
|
|
||||||
def get_tracks(self):
|
def get_tracks(self):
|
||||||
"""
|
"""
|
||||||
|
@ -383,6 +389,20 @@ class Station(object):
|
||||||
assert self._tracks_loaded, 'Must call ".load_tracks()" before ".get_tracks()"'
|
assert self._tracks_loaded, 'Must call ".load_tracks()" before ".get_tracks()"'
|
||||||
return self._tracks
|
return self._tracks
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_data(cls, data, many=False):
|
||||||
|
"""
|
||||||
|
Construct and return one or many :class:`.Station` instances
|
||||||
|
from Google Play Music API response.
|
||||||
|
"""
|
||||||
|
if many:
|
||||||
|
return [cls.from_data(one) for one in data if one['inLibrary']]
|
||||||
|
|
||||||
|
return Station(
|
||||||
|
station_id=data['id'],
|
||||||
|
name=data['name']
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class SearchResults(object):
|
class SearchResults(object):
|
||||||
"""
|
"""
|
||||||
|
@ -472,6 +492,7 @@ class _GP(object):
|
||||||
# self._last_call_index = 0
|
# self._last_call_index = 0
|
||||||
self.cached_tracks = None
|
self.cached_tracks = None
|
||||||
self.cached_playlists = None
|
self.cached_playlists = None
|
||||||
|
self.cached_stations = None
|
||||||
|
|
||||||
self.invalidate_caches()
|
self.invalidate_caches()
|
||||||
|
|
||||||
|
@ -504,10 +525,11 @@ class _GP(object):
|
||||||
|
|
||||||
def invalidate_caches(self):
|
def invalidate_caches(self):
|
||||||
"""
|
"""
|
||||||
Clear cached tracks & playlists.
|
Clear cached tracks & playlists & stations.
|
||||||
"""
|
"""
|
||||||
self.cached_tracks = None
|
self.cached_tracks = None
|
||||||
self.cached_playlists = None
|
self.cached_playlists = None
|
||||||
|
self.cached_stations = None
|
||||||
self.caches_invalidated.fire()
|
self.caches_invalidated.fire()
|
||||||
|
|
||||||
@synchronized
|
@synchronized
|
||||||
|
@ -576,6 +598,25 @@ class _GP(object):
|
||||||
|
|
||||||
get_stream_url_async = asynchronous(get_stream_url)
|
get_stream_url_async = asynchronous(get_stream_url)
|
||||||
|
|
||||||
|
@synchronized
|
||||||
|
def get_all_user_station_contents(self, **_):
|
||||||
|
"""
|
||||||
|
Return list of :class:`.Station` instances.
|
||||||
|
"""
|
||||||
|
if self.cached_stations:
|
||||||
|
return self.cached_stations
|
||||||
|
self.get_all_tracks()
|
||||||
|
|
||||||
|
self.cached_stations = Station.from_data(
|
||||||
|
self.mobile_client.get_all_stations(),
|
||||||
|
True
|
||||||
|
)
|
||||||
|
return self.cached_stations
|
||||||
|
|
||||||
|
get_all_user_station_contents_async = ( # pylint: disable=invalid-name
|
||||||
|
asynchronous(get_all_user_station_contents)
|
||||||
|
)
|
||||||
|
|
||||||
@synchronized
|
@synchronized
|
||||||
def get_all_user_playlist_contents(self, **_):
|
def get_all_user_playlist_contents(self, **_):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Add table
Reference in a new issue