genius.py 2.7 KB

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