1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/searx_ynh.git synced 2024-09-03 20:16:30 +02:00
searx_ynh/sources/searx/engines/soundcloud.py

71 lines
2 KiB
Python
Raw Normal View History

2014-12-01 12:26:38 +01:00
## Soundcloud (Music)
2014-12-13 13:56:02 +01:00
#
2014-12-01 12:26:38 +01:00
# @website https://soundcloud.com
# @provide-api yes (https://developers.soundcloud.com/)
2014-12-13 13:56:02 +01:00
#
2014-12-01 12:26:38 +01:00
# @using-api yes
# @results JSON
# @stable yes
2015-01-13 17:13:08 +01:00
# @parse url, title, content, publishedDate, embedded
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
from json import loads
2015-01-13 17:13:08 +01:00
from urllib import urlencode, quote_plus
from dateutil import parser
2014-05-10 11:56:19 +02:00
2014-12-01 12:26:38 +01:00
# engine dependent config
2014-05-10 11:56:19 +02:00
categories = ['music']
2014-12-01 12:26:38 +01:00
paging = True
2014-05-10 11:56:19 +02:00
2014-12-01 12:26:38 +01:00
# api-key
2014-05-10 11:56:19 +02:00
guest_client_id = 'b45b1aa10f1ac2941910a7f0d10f8e28'
2014-12-01 12:26:38 +01:00
# search-url
url = 'https://api.soundcloud.com/'
2015-01-13 16:40:30 +01:00
search_url = url + 'search?{query}'\
'&facet=model'\
'&limit=20'\
'&offset={offset}'\
'&linked_partitioning=1'\
'&client_id={client_id}' # noqa
2014-05-10 11:56:19 +02:00
2015-01-13 17:13:08 +01:00
embedded_url = '<iframe width="100%" height="166" ' +\
'scrolling="no" frameborder="no" ' +\
'data-src="https://w.soundcloud.com/player/?url={uri}"></iframe>'
2014-05-10 11:56:19 +02:00
2014-12-01 12:26:38 +01:00
# do search-request
2014-05-10 11:56:19 +02:00
def request(query, params):
offset = (params['pageno'] - 1) * 20
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
params['url'] = search_url.format(query=urlencode({'q': query}),
2014-12-01 12:26:38 +01:00
offset=offset,
client_id=guest_client_id)
2014-05-10 11:56:19 +02:00
return params
2014-12-01 12:26:38 +01:00
# get response from search-request
2014-05-10 11:56:19 +02:00
def response(resp):
results = []
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
search_res = loads(resp.text)
2014-12-01 12:26:38 +01:00
# parse results
2014-05-10 11:56:19 +02:00
for result in search_res.get('collection', []):
if result['kind'] in ('track', 'playlist'):
title = result['title']
content = result['description']
2015-01-13 17:13:08 +01:00
publishedDate = parser.parse(result['last_modified'])
uri = quote_plus(result['uri'])
embedded = embedded_url.format(uri=uri)
2014-12-01 12:26:38 +01:00
# append result
2014-05-10 11:56:19 +02:00
results.append({'url': result['permalink_url'],
'title': title,
2015-01-13 17:13:08 +01:00
'publishedDate': publishedDate,
'embedded': embedded,
2014-05-10 11:56:19 +02:00
'content': content})
2014-12-01 12:26:38 +01:00
# return results
2014-05-10 11:56:19 +02:00
return results