Skip to content
This repository has been archived by the owner on Jan 22, 2022. It is now read-only.

Commit

Permalink
Merge pull request #615 from thebigmunch/new-search-response-format
Browse files Browse the repository at this point in the history
Update search call for new response format [Closes #614]
  • Loading branch information
simon-weber committed Oct 1, 2018
2 parents 23ce9cf + 3d6a4e1 commit 037fff3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
12 changes: 8 additions & 4 deletions gmusicapi/clients/mobileclient.py
Expand Up @@ -1681,7 +1681,7 @@ def search(self, query, max_results=None):
A value of ``None`` allows up to 999 results per type. Default is ``None``.
The results are returned in a dictionary with keys:
``album_hits, artist_hits, playlist_hits, podcast_hits,
``album_hits, artist_hits, genre_hits, playlist_hits, podcast_hits,
situation_hits, song_hits, station_hits, video_hits``
containing lists of results of that type.
Expand Down Expand Up @@ -1879,15 +1879,19 @@ def search(self, query, max_results=None):

res = self._make_call(mobileclient.Search, query, max_results)

hits = res.get('entries', [])
clusters = res.get('clusterDetail', [])

hits_by_type = defaultdict(list)
for hit in hits:
hits_by_type[hit['type']].append(hit)
for cluster in clusters:
hit_type = cluster['cluster']['type']
hits = cluster.get('entries', [])
for hit in hits:
hits_by_type[hit_type].append(hit)

return {'album_hits': hits_by_type['3'],
'artist_hits': hits_by_type['2'],
'playlist_hits': hits_by_type['4'],
'genre_hits': hits_by_type['5'],
'podcast_hits': hits_by_type['9'],
'situation_hits': hits_by_type['7'],
'song_hits': hits_by_type['1'],
Expand Down
47 changes: 37 additions & 10 deletions gmusicapi/protocol/mobileclient.py
Expand Up @@ -595,6 +595,16 @@
'items': sj_situation
}

sj_search_result_cluster_info = {
'type': 'object',
'additionalProperties': False,
'properties': {
'category': {'type': 'string'},
'id': {'type': 'string'},
'type': {'type': 'string'}
}
}

sj_search_result = {
'type': 'object',
'additionalProperties': False,
Expand All @@ -604,10 +614,16 @@
'best_result': {'type': 'boolean', 'required': False},
'navigational_result': {'type': 'boolean', 'required': False},
'navigational_confidence': {'type': 'number', 'required': False},
'cluster': {
'type': 'array',
'required': False,
'items': sj_search_result_cluster_info
},
'artist': sj_artist.copy(),
'album': sj_album.copy(),
'track': sj_track.copy(),
'playlist': sj_playlist.copy(),
'genre': sj_genre.copy(),
'series': sj_podcast_series.copy(),
'station': sj_station.copy(),
'situation': sj_situation.copy(),
Expand All @@ -619,11 +635,27 @@
sj_search_result['properties']['album']['required'] = False
sj_search_result['properties']['track']['required'] = False
sj_search_result['properties']['playlist']['required'] = False
sj_search_result['properties']['genre']['required'] = False
sj_search_result['properties']['series']['required'] = False
sj_search_result['properties']['station']['required'] = False
sj_search_result['properties']['situation']['required'] = False
sj_search_result['properties']['youtube_video']['required'] = False

sj_search_result_cluster = {
'type': 'object',
'additionalProperties': False,
'properties': {
'cluster': {'type': sj_search_result_cluster_info},
'displayName': {'type': 'string', 'required': False},
'entries': {
'type': 'array',
'items': sj_search_result,
'required': False
},
'resultToken': {'type': 'string', 'required': False}
}
}


class McCall(Call):
"""Abstract base for mobile client calls."""
Expand Down Expand Up @@ -865,22 +897,17 @@ class Search(McCall):

# The result types returned are requested in the `ct` parameter.
# Free account search is restricted so may not contain hits for all result types.
# 1: Song, 2: Artist, 3: Album, 4: Playlist, 6: Station, 7: Situation, 8: Video
# 9: Podcast Series
static_params = {'ct': '1,2,3,4,6,7,8,9'}
# 1: Song, 2: Artist, 3: Album, 4: Playlist, 5: Genre,
# 6: Station, 7: Situation, 8: Video, 9: Podcast Series
static_params = {'ct': '1,2,3,4,5,6,7,8,9', 'ic': True}

_res_schema = {
'type': 'object',
'additionalProperties': False,
'properties': {
'kind': {'type': 'string'},
'clusterOrder': {'type': 'array',
'items': {'type': 'string'},
'required': False},
'entries': {'type': 'array',
'items': sj_search_result,
'required': False},
'suggestedQuery': {'type': 'string', 'required': False}
'clusterDetail': {'type': 'array',
'items': {'type': sj_search_result_cluster}}
},
}

Expand Down

0 comments on commit 037fff3

Please sign in to comment.