Fix some compatibility issues, cleanup.

This commit is contained in:
reiv 2015-10-17 21:18:42 +02:00 committed by Sergey M․
parent 2abf7cab80
commit c30943b1c0

View file

@ -488,27 +488,23 @@ class SoundcloudSearchIE(SearchInfoExtractor, SoundcloudIE):
_SEARCH_KEY = 'scsearch' _SEARCH_KEY = 'scsearch'
_RESULTS_PER_PAGE = 50 _RESULTS_PER_PAGE = 50
_API_V2_BASE = 'https://api-v2.soundcloud.com'
def _get_collection(self, endpoint, collection_id, **query): def _get_collection(self, endpoint, collection_id, **query):
import itertools
query['limit'] = self._RESULTS_PER_PAGE query['limit'] = self._RESULTS_PER_PAGE
query['client_id'] = self._CLIENT_ID query['client_id'] = self._CLIENT_ID
query['linked_partitioning'] = '1' query['linked_partitioning'] = '1'
api_base_url = '{0}//api-v2.soundcloud.com'.format(self.http_scheme())
total_results = self._MAX_RESULTS total_results = self._MAX_RESULTS
collected_results = 0 collected_results = 0
next_url = None next_url = None
for i in itertools.count(): for i in itertools.count():
if not next_url: if not next_url:
query['offset'] = i * self._RESULTS_PER_PAGE query['offset'] = i * self._RESULTS_PER_PAGE
data = compat_urllib_parse.urlencode(query) data = compat_urllib_parse.urlencode(query)
next_url = '{0}{1}?{2}'.format(api_base_url, endpoint, data) next_url = '{0}{1}?{2}'.format(self._API_V2_BASE, endpoint, data)
response = self._download_json(next_url, response = self._download_json(next_url,
video_id=collection_id, video_id=collection_id,
@ -516,7 +512,7 @@ class SoundcloudSearchIE(SearchInfoExtractor, SoundcloudIE):
errnote='Unable to download API page') errnote='Unable to download API page')
total_results = int(response.get( total_results = int(response.get(
u'total_results', total_results)) 'total_results', total_results))
collection = response['collection'] collection = response['collection']
collected_results += len(collection) collected_results += len(collection)
@ -527,14 +523,13 @@ class SoundcloudSearchIE(SearchInfoExtractor, SoundcloudIE):
if collected_results >= total_results or not collection: if collected_results >= total_results or not collection:
break break
next_url = response.get(u'next_href', None) next_url = response.get('next_href', None)
def _get_n_results(self, query, n): def _get_n_results(self, query, n):
results = [] results = []
tracks = self._get_collection('/search/tracks', tracks = self._get_collection('/search/tracks',
collection_id='Query "{}"'.format(query), collection_id='Query "{0}"'.format(query),
q=query.encode('utf-8')) q=query.encode('utf-8'))
for _ in range(n): for _ in range(n):
@ -542,12 +537,9 @@ class SoundcloudSearchIE(SearchInfoExtractor, SoundcloudIE):
track = next(tracks) track = next(tracks)
except StopIteration: except StopIteration:
break break
uri = track[u'uri'] uri = track['uri']
title = track[u'title'] title = track['title']
username = track[u'user'][u'username'] results.append(self.url_result(url=uri))
results.append(self.url_result(
url=uri,
video_title='{0} - {1}'.format(username, title)))
if not results: if not results:
raise ExtractorError( raise ExtractorError(