mixcloud.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. """
  2. Mixcloud (Music)
  3. @website https://http://www.mixcloud.com/
  4. @provide-api yes (http://www.mixcloud.com/developers/
  5. @using-api yes
  6. @results JSON
  7. @stable yes
  8. @parse url, title, content, embedded, publishedDate
  9. """
  10. from json import loads
  11. from urllib import urlencode
  12. from dateutil import parser
  13. # engine dependent config
  14. categories = ['music']
  15. paging = True
  16. # search-url
  17. url = 'https://api.mixcloud.com/'
  18. search_url = url + 'search/?{query}&type=cloudcast&limit=10&offset={offset}'
  19. embedded_url = '<iframe scrolling="no" frameborder="0" allowTransparency="true" ' +\
  20. 'data-src="https://www.mixcloud.com/widget/iframe/?feed={url}" width="300" height="300"></iframe>'
  21. # do search-request
  22. def request(query, params):
  23. offset = (params['pageno'] - 1) * 10
  24. params['url'] = search_url.format(query=urlencode({'q': query}),
  25. offset=offset)
  26. return params
  27. # get response from search-request
  28. def response(resp):
  29. results = []
  30. search_res = loads(resp.text)
  31. # parse results
  32. for result in search_res.get('data', []):
  33. title = result['name']
  34. url = result['url']
  35. content = result['user']['name']
  36. embedded = embedded_url.format(url=url)
  37. publishedDate = parser.parse(result['created_time'])
  38. # append result
  39. results.append({'url': url,
  40. 'title': title,
  41. 'embedded': embedded,
  42. 'publishedDate': publishedDate,
  43. 'content': content})
  44. # return results
  45. return results