genius.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=invalid-name
  3. """Genius
  4. """
  5. from urllib.parse import urlencode
  6. from datetime import datetime
  7. # about
  8. about = {
  9. "website": 'https://genius.com/',
  10. "wikidata_id": 'Q3419343',
  11. "official_api_documentation": 'https://docs.genius.com/',
  12. "use_official_api": True,
  13. "require_api_key": False,
  14. "results": 'JSON',
  15. }
  16. # engine dependent config
  17. categories = ['music', 'lyrics']
  18. paging = True
  19. page_size = 5
  20. url = 'https://genius.com/api/'
  21. search_url = url + 'search/{index}?{query}&page={pageno}&per_page={page_size}'
  22. music_player = 'https://genius.com{api_path}/apple_music_player'
  23. def request(query, params):
  24. params['url'] = search_url.format(
  25. query=urlencode({'q': query}),
  26. index='multi',
  27. page_size=page_size,
  28. pageno=params['pageno'],
  29. )
  30. return params
  31. def parse_lyric(hit):
  32. content = ''
  33. highlights = hit['highlights']
  34. if highlights:
  35. content = hit['highlights'][0]['value']
  36. else:
  37. content = hit['result'].get('title_with_featured', '')
  38. timestamp = hit['result']['lyrics_updated_at']
  39. result = {
  40. 'url': hit['result']['url'],
  41. 'title': hit['result']['full_title'],
  42. 'content': content,
  43. 'thumbnail': hit['result']['song_art_image_thumbnail_url'],
  44. }
  45. if timestamp:
  46. result.update({'publishedDate': datetime.fromtimestamp(timestamp)})
  47. api_path = hit['result'].get('api_path')
  48. if api_path:
  49. # The players are just playing 30sec from the title. Some of the player
  50. # will be blocked because of a cross-origin request and some players will
  51. # link to apple when you press the play button.
  52. result['iframe_src'] = music_player.format(api_path=api_path)
  53. return result
  54. def parse_artist(hit):
  55. result = {
  56. 'url': hit['result']['url'],
  57. 'title': hit['result']['name'],
  58. 'content': '',
  59. 'thumbnail': hit['result']['image_url'],
  60. }
  61. return result
  62. def parse_album(hit):
  63. res = hit['result']
  64. content = res.get('name_with_artist', res.get('name', ''))
  65. x = res.get('release_date_components')
  66. if x:
  67. x = x.get('year')
  68. if x:
  69. content = "%s / %s" % (x, content)
  70. return {
  71. 'url': res['url'],
  72. 'title': res['full_title'],
  73. 'thumbnail': res['cover_art_url'],
  74. 'content': content.strip(),
  75. }
  76. parse = {'lyric': parse_lyric, 'song': parse_lyric, 'artist': parse_artist, 'album': parse_album}
  77. def response(resp):
  78. results = []
  79. for section in resp.json()['response']['sections']:
  80. for hit in section['hits']:
  81. func = parse.get(hit['type'])
  82. if func:
  83. results.append(func(hit))
  84. return results