def on_play(self, context, track_id, playlist_id, url): client = self.get_client(context) if track_id: track_item = client.get_track(track_id) url = client.get_track_url(track_id) track_item['uri'] = url return track_item if url: item = client.resolve_url(url) item_type = item['type'] if item_type in ['audio', 'music']: return { 'type': 'uri', 'uri': context.create_uri('/play/', {'audio_id': item['id']}) } if item_type == 'playlist': return { 'type': 'uri', 'uri': context.create_uri('/play/', {'playlist_id': item['id']}) } raise ProviderException('Unknown item kind "%s"' % item_type) if playlist_id: tracks = client.get_playlist(playlist_id).get('items', []) if tracks: playlist = context.get_audio_playlist() playlist.clear() for track in tracks: track['uri'] = context.create_uri( '/play/', {'audio_id': track['id']}) playlist.add(track) pass return tracks[0] raise ProviderException('No tracks found in playlist') return False
def get_trending(self, category='music', page=1): if not category.lower() in ['music', 'audio']: raise ProviderException('Unknown category "%s"' % category) params = self._create_params(page) response = self._request(self._create_url('app/mobileapps/suggestions/tracks/popular/%s' % category), headers={'Accept': 'application/json'}, params=params) self._handle_error(response) return items.convert_to_items(response.json(), mobile_conversion=True)
def search(self, search_text, category='sounds', page=1): if not category in ['sounds', 'people', 'sets']: raise ProviderException('Unknown category "%s"' % category) params = {'limit': str(self._items_per_page), 'q': search_text} if page > 1: params['offset'] = str((page - 1) * self._items_per_page) pass response = self._request(self._create_url('search/%s' % category), headers={'Accept': 'application/json'}, params=params) self._handle_error(response) return items.convert_to_items(response.json())
def on_like(self, context, category, content_id, like): if category == 'track': json_data = self.get_client(context).like_track(content_id, like) pass elif category == 'playlist': json_data = self.get_client(context).like_playlist(content_id, like) pass else: raise ProviderException('Unknown category "%s" in "on_like"' % category) if not like: context.get_ui().refresh_container() pass return True
def search(self, search_text, category='tracks', page=1): if not category in ['tracks', 'users', 'playlists']: raise ProviderException('Unknown category "%s"' % category) params = { 'limit': str(self._items_per_page), 'q': search_text, 'linked_partitioning': page } # if page > 1: # params['offset'] = str((page - 1) * self._items_per_page) response = self._request(self._create_url('%s/' % category), headers={'Accept': 'application/json'}, params=params) self._handle_error(response) return items.convert_to_items(response.json())
def convert_to_item(json_item): kind = json_item.get('kind', '') if kind == 'track': return _convert_to_track_item(json_item) elif kind == 'playlist': return _convert_to_playlist_item(json_item) elif kind == 'user': return _convert_to_artist_item(json_item) elif kind == 'group': # skip groups return None elif kind == 'like': # a like includes the liked track or playlist. if json_item.get('playlist', None): return convert_to_item(json_item['playlist']) if json_item.get('track', None): return convert_to_item(json_item['track']) pass raise ProviderException('Unknown kind of item "%s"' % kind)
def process_result(self, context, result): client = self.get_client(context) items = [] path = context.get_path() for item in result['items']: context_menu = [] item_type = item['type'] if item_type == 'audio': # playback uri item['uri'] = context.create_uri( 'play', {'audio_id': unicode(item['id'])}) # recommended tracks context_menu.append( (context.localize(self.SOUNDCLOUD_LOCAL_RECOMMENDED), 'Container.Update(%s)' % context.create_uri('/explore/recommended/tracks/%s' % unicode(item['id'])))) # like/unlike a track if client.get_access_token(): if path == '/user/favorites/me/': context_menu.append( (context.localize(self.SOUNDCLOUD_LOCAL_UNLIKE), 'RunPlugin(%s)' % context.create_uri( 'like/track/%s' % unicode(item['id']), {'like': '0'}))) else: context_menu.append( (context.localize(self.SOUNDCLOUD_LOCAL_LIKE), 'RunPlugin(%s)' % context.create_uri( 'like/track/%s' % unicode(item['id']), {'like': '1'}))) # go to user username = nightcrawler.utils.strings.to_unicode( item['user']['username']) user_id = nightcrawler.utils.strings.to_unicode( item['user']['id']) if path != '/user/tracks/%s/' % user_id: context_menu.append( (context.localize(self.SOUNDCLOUD_LOCAL_GO_TO_USER) % ('[B]%s[/B]' % username), 'Container.Update(%s)' % context.create_uri('/user/tracks/%s/' % user_id))) elif item_type == 'playlist': item['type'] = 'folder' item['uri'] = context.create_uri('/playlist/%s/' % item['id']) if client.get_access_token(): if path == '/user/favorites/me/': context_menu = [ (context.localize(self.SOUNDCLOUD_LOCAL_UNLIKE), 'RunPlugin(%s)' % context.create_uri( 'like/playlist/%s' % unicode(item['id']), {'like': '0'})) ] else: context_menu = [ (context.localize(self.SOUNDCLOUD_LOCAL_LIKE), 'RunPlugin(%s)' % context.create_uri( 'like/playlist/%s' % unicode(item['id']), {'like': '1'})) ] elif item_type == 'artist': item['type'] = 'folder' item['uri'] = context.create_uri('/user/tracks/%s/' % item['id']) if client.get_access_token(): if path == '/user/following/me/': context_menu = [ (context.localize(self.SOUNDCLOUD_LOCAL_UNFOLLOW), 'RunPlugin(%s)' % context.create_uri( 'follow/%s' % unicode(item['id']), {'follow': '0'})) ] else: context_menu = [ (context.localize(self.SOUNDCLOUD_LOCAL_FOLLOW), 'RunPlugin(%s)' % context.create_uri( 'follow/%s' % unicode(item['id']), {'follow': '1'})) ] else: raise ProviderException('Unknown item type "%s"' % item_type) if context_menu: item['context-menu'] = {'items': context_menu} item['images']['fanart'] = self.get_fanart(context) items.append(item) # create next page item if result.get('continue', False): new_params = {} new_params.update(context.get_params()) if 'cursor' in result: new_params['cursor'] = result['cursor'] new_context = context.clone(new_params=new_params) items.append( nightcrawler.items.create_next_page_item( new_context, fanart=self.get_fanart(context))) return items