Commit 13c8a9e2 authored by Jean-Francois Dockes's avatar Jean-Francois Dockes
Browse files

hra: support lang. Use alleditorplaylists instead of genre/moods which are all empty.

parent 12755026
......@@ -72,8 +72,11 @@ def maybelogin():
if not username or not password:
raise Exception("hrauser and/or hrapass " +
"not set in configuration")
lang = upconfig.get('hralang')
if not lang:
lang = 'en'
setMimeAndSamplerate("application/flac", "44100")
return sess.login(username, password)
return sess.login(username, password, lang)
@dispatcher.record('trackuri')
......@@ -160,9 +163,11 @@ def root():
return
add_directory('Categories', allCategories)
add_directory('Genres', allGenres)
add_directory('Editor playlists: Moods', allEditorMoods)
add_directory('Editor playlists: Genres', allEditorGenres)
add_directory('Editor playlists: Themes', allEditorThemes)
# These seem mostly empty. The hra app uses alleditor
# add_directory('Editor playlists: Moods', allEditorMoods)
# add_directory('Editor playlists: Genres', allEditorGenres)
# add_directory('Editor playlists: Themes', allEditorThemes)
add_directory('Editor Playlists', allEditorPlaylists)
add_directory('My Playlists', allUserPlaylists)
add_directory('My Albums', allUserAlbums)
add_directory('My Tracks', allUserTracks)
......@@ -198,10 +203,16 @@ def allEditorThemes():
items = sess.get_editorthemes()
view(items, urls_from_id(edplaylist_view, items))
@plugin.route('/alleditorplaylists')
def allEditorPlaylists():
items = sess.get_alleditorplaylists()
view(items, urls_from_id(edplaylist_view, items))
@plugin.route('/editorplaylist/<id>')
def edplaylist_view(id):
track_list(sess.get_editor_playlist(id))
@plugin.route('/alluserplaylists')
def allUserPlaylists():
items = sess.get_alluserplaylists()
......@@ -241,11 +252,12 @@ def search(a):
maybelogin()
idpath = objid.replace(_rootid, '', 1)
catg = ""
# Both genres and categories have an objid path beginning with category
if idpath.startswith('/category'):
catg = session.decode_prefix(idpath.replace('/category/', '', 1))
uplog("SEARCH CATG %s" % catg)
# HRA search always returns albums anyway.
if catg:
albums = sess.searchCategory(value, catg)
......
......@@ -36,6 +36,7 @@ from upmplgutils import *
#requests_log.setLevel(logging.DEBUG)
#requests_log.propagate = True
# Bogus logging class initially to avoid to change the qobuz module code. Could
# get rid of it here...
class MLog(object):
......@@ -55,6 +56,7 @@ class MLog(object):
self._doprint(msg)
log = MLog()
class HRAAPI(object):
def __init__(self):
......@@ -66,6 +68,12 @@ class HRAAPI(object):
self.error = None
self.status_code = None
self.session = requests.Session()
self.lang = 'en'
def setlang(self, lang):
self.lang = lang
def _api_error_string(self, request, url='', params={}, json=''):
return '{reason} (code={status_code})\n' \
......@@ -103,11 +111,13 @@ class HRAAPI(object):
if 'method' in opt and opt['method'] == 'POST':
dopost = True
if self.user_data:
params = {'userData' : self.user_data}
params = {'userData' : self.user_data, 'lang':self.lang}
params.update(iparams)
if 'limit' not in params:
params['limit'] = 300
log.debug('request: url {}, params: {}'.format(url, str(params)))
r = None
time1 = time.time()
try:
if dopost:
r = self.session.post(url, data=params, headers=headers)
......@@ -117,7 +127,10 @@ class HRAAPI(object):
self.error = 'HTTP request failed'
log.warn(self.error)
return None
time2 = time.time()
log.info('Request took {:.3f} ms'.format((time2-time1)*1000.0))
self.status_code = int(r.status_code)
if self.status_code != 200:
self.error = self._api_error_string(r, url, params)
......@@ -218,7 +231,7 @@ class HRAAPI(object):
def listCategoryContent(self, **ka):
if not self.isloggedin():
return None
self._check_ka(ka, ['category', 'limit', 'offset'])
self._check_ka(ka, ['category'])
data = self._api_request(ka, '/vault/categories/ListCategorieContent/')
if not data or 'data' not in data or 'results' not in data['data']:
return {}
......@@ -247,20 +260,22 @@ class HRAAPI(object):
return None
return data['data']['results']['tracks']
def quickSearch(self, **ka):
self._check_ka(ka, ['search'])
params = ka
data = self._api_request(params, '/vault/search/quickSearch/')
if not data or 'data' not in data:
log.info("quickSearch: %s" % ka)
data = self._api_request(ka, '/vault/search/quickSearch/')
if not data or 'data' not in data or not data['data']:
return {}
# The data is a dict key:albumdata, the key can be used as album_id
# with /vault/album
return data['data']
def searchCategory(self, **ka):
self._check_ka(ka, ['search', 'category'])
params = ka
data = self._api_request(params, '/vault/SearchInCategory/')
log.info("searchCategory: %s" % ka)
data = self._api_request(ka, '/vault/SearchInCategory/')
if not data or 'data' not in data or 'results' not in data['data']:
return {}
# The data is a dict key:albumdata, the key can be used as album_id
......@@ -285,12 +300,18 @@ class HRAAPI(object):
def getAvailableThemes(self, **ka):
params = ka
data = self._api_request(params, '/vault/getEditorPlaylistsThemes/')
data = self._api_request(ka, '/vault/getEditorPlaylistsThemes/')
if not data or 'data' not in data or 'results' not in data['data']:
return {}
return data['data']['results']
def getAllEditorPlaylists(self, **ka):
data = self._api_request(ka, '/vault/editorPlaylists/')
if not data or 'data' not in data or 'results' not in data['data']:
return None
ret = data['data']['results']
return ret
def getEditorPlaylist(self, **ka):
if not self.isloggedin():
return None
......@@ -298,7 +319,7 @@ class HRAAPI(object):
data = self._api_request(ka, '/vault/getSingleEditorPlaylists/')
if not data or 'data' not in data or 'results' not in data['data'] or \
'tracks' not in data['data']['results'][0]:
log.info("getEditorPlaylist: RETURNING NONE")
log.info("getEditorPlaylist: RETURNING NONE for id %s"%ka['id'])
return None
return data['data']['results'][0]
......@@ -306,12 +327,10 @@ class HRAAPI(object):
def listAllUserPlaylists(self, **ka):
if not self.isloggedin():
return {}
params = ka
data = self._api_request(params, '/user/ListAllUserPlaylists')
data = self._api_request(ka, '/user/ListAllUserPlaylists')
if not data or 'data' not in data or 'data' not in data['data']:
return None
ret = data['data']['data']
log.info(ret)
return ret
......
......@@ -18,7 +18,8 @@ class Session(object):
def __init__(self):
self.api = hraapi.HRAAPI()
def login(self, username, password):
def login(self, username, password, lang):
self.api.setlang(lang)
data = self.api.user_login(username=username, password=password)
if data:
return True
......@@ -40,7 +41,7 @@ class Session(object):
def get_catg_albums(self, prefix):
data = self.api.listCategoryContent(category=prefix, offset=0, limit=30)
data = self.api.listCategoryContent(category=prefix)
return [_parse_album(a) for a in data]
......@@ -66,7 +67,6 @@ class Session(object):
data = self.api.listAllUserTracks()
if not data:
return []
uplog(data)
return [_parse_track(p) for p in data]
......@@ -87,6 +87,7 @@ class Session(object):
return None
return data['url']
def get_editormoods(self):
data = self.api.getAvailableMoods()
return [_parse_album(a) for a in data]
......@@ -97,6 +98,12 @@ class Session(object):
data = self.api.getAvailableThemes()
return [_parse_album(a) for a in data]
def get_alleditorplaylists(self):
data = self.api.getAllEditorPlaylists()
return [_parse_album(a) for a in data]
def get_editor_playlist(self, id):
data = self.api.getEditorPlaylist(id=id)
if not data:
......@@ -159,6 +166,9 @@ def _parse_album(data):
break
else:
kwargs['image'] = data['cover']
elif 'playlistCover' in data:
kwargs['image'] = data['playlistCover']
a = Album(**kwargs)
return a
......@@ -180,11 +190,16 @@ def _parse_track(data, albumarg = None):
else:
kwargs['track_num'] = 0
if albumarg:
kwargs['album'] = albumarg
elif 'cover' in data and 'album' in data:
kwargs['album'] = Album(image=data['cover'], name=data['album'])
# If track has own cover, use it (e.g. for multialbum playlists)
if 'cover' in data:
kwargs['image'] = data['cover']
return Track(**kwargs)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment