genius.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. ExtractorError,
  4. js_to_json,
  5. smuggle_url,
  6. str_or_none,
  7. traverse_obj,
  8. unescapeHTML,
  9. )
  10. class GeniusIE(InfoExtractor):
  11. _VALID_URL = r'https?://(?:www\.)?genius\.com/videos/(?P<id>[^?/#]+)'
  12. _TESTS = [{
  13. 'url': 'https://genius.com/videos/Vince-staples-breaks-down-the-meaning-of-when-sparks-fly',
  14. 'md5': '64c2ad98cfafcfda23bfa0ad0c512f4c',
  15. 'info_dict': {
  16. 'id': '6313303597112',
  17. 'ext': 'mp4',
  18. 'title': 'Vince Staples Breaks Down The Meaning Of “When Sparks Fly”',
  19. 'description': 'md5:bc15e00342c537c0039d414423ae5752',
  20. 'tags': 'count:1',
  21. 'uploader_id': '4863540648001',
  22. 'duration': 388.416,
  23. 'upload_date': '20221005',
  24. 'timestamp': 1664982341,
  25. 'thumbnail': r're:^https?://.*\.jpg$',
  26. },
  27. }, {
  28. 'url': 'https://genius.com/videos/Breaking-down-drakes-certified-lover-boy-kanye-beef-way-2-sexy-cudi',
  29. 'md5': 'b8ed87a5efd1473bd027c20a969d4060',
  30. 'info_dict': {
  31. 'id': '6271792014001',
  32. 'ext': 'mp4',
  33. 'title': 'md5:c6355f7fa8a70bc86492a3963919fc15',
  34. 'description': 'md5:1774638c31548b31b037c09e9b821393',
  35. 'tags': 'count:3',
  36. 'uploader_id': '4863540648001',
  37. 'duration': 2685.099,
  38. 'upload_date': '20210909',
  39. 'timestamp': 1631209167,
  40. 'thumbnail': r're:^https?://.*\.jpg$',
  41. },
  42. }]
  43. def _real_extract(self, url):
  44. display_id = self._match_id(url)
  45. webpage = self._download_webpage(url, display_id)
  46. metadata = self._search_json(
  47. r'<meta content="', webpage, 'metadata', display_id, transform_source=unescapeHTML)
  48. video_id = traverse_obj(
  49. metadata, ('video', 'provider_id'),
  50. ('dfp_kv', lambda _, x: x['name'] == 'brightcove_video_id', 'values', 0), get_all=False)
  51. if not video_id:
  52. raise ExtractorError('Brightcove video id not found in webpage')
  53. config = self._search_json(r'var\s*APP_CONFIG\s*=', webpage, 'config', video_id, default={})
  54. account_id = config.get('brightcove_account_id', '4863540648001')
  55. player_id = traverse_obj(
  56. config, 'brightcove_standard_web_player_id', 'brightcove_standard_no_autoplay_web_player_id',
  57. 'brightcove_modal_web_player_id', 'brightcove_song_story_web_player_id', default='S1ZcmcOC1x')
  58. return self.url_result(
  59. smuggle_url(
  60. f'https://players.brightcove.net/{account_id}/{player_id}_default/index.html?videoId={video_id}',
  61. {'referrer': url}), 'BrightcoveNew', video_id)
  62. class GeniusLyricsIE(InfoExtractor):
  63. _VALID_URL = r'https?://(?:www\.)?genius\.com/(?P<id>[^?/#]+)-lyrics[?/#]?'
  64. _TESTS = [{
  65. 'url': 'https://genius.com/Lil-baby-heyy-lyrics',
  66. 'playlist_mincount': 2,
  67. 'info_dict': {
  68. 'id': '8454545',
  69. 'title': 'Heyy',
  70. 'description': 'Heyy by Lil Baby',
  71. },
  72. }, {
  73. 'url': 'https://genius.com/Outkast-two-dope-boyz-in-a-cadillac-lyrics',
  74. 'playlist_mincount': 1,
  75. 'info_dict': {
  76. 'id': '36239',
  77. 'title': 'Two Dope Boyz (In a Cadillac)',
  78. 'description': 'Two Dope Boyz (In a Cadillac) by OutKast',
  79. },
  80. }, {
  81. 'url': 'https://genius.com/Playboi-carti-rip-lyrics',
  82. 'playlist_mincount': 1,
  83. 'info_dict': {
  84. 'id': '3710582',
  85. 'title': 'R.I.P.',
  86. 'description': 'R.I.P. by Playboi Carti',
  87. },
  88. }]
  89. def _real_extract(self, url):
  90. display_id = self._match_id(url)
  91. webpage = self._download_webpage(url, display_id)
  92. json_string = self._search_json(
  93. r'window\.__PRELOADED_STATE__\s*=\s*JSON\.parse\(', webpage, 'json string',
  94. display_id, transform_source=js_to_json, contains_pattern=r'\'{(?s:.+)}\'')
  95. song_info = self._parse_json(json_string, display_id)
  96. song_id = str_or_none(traverse_obj(song_info, ('songPage', 'song')))
  97. if not song_id:
  98. raise ExtractorError('Song id not found in webpage')
  99. title = traverse_obj(
  100. song_info, ('songPage', 'trackingData', lambda _, x: x['key'] == 'Title', 'value'),
  101. get_all=False, default='untitled')
  102. artist = traverse_obj(
  103. song_info, ('songPage', 'trackingData', lambda _, x: x['key'] == 'Primary Artist', 'value'),
  104. get_all=False, default='unknown artist')
  105. media = traverse_obj(
  106. song_info, ('entities', 'songs', song_id, 'media'), expected_type=list, default=[])
  107. entries = []
  108. for m in media:
  109. if m.get('type') in ('video', 'audio') and m.get('url'):
  110. if m.get('provider') == 'spotify':
  111. self.to_screen(f'{song_id}: Skipping Spotify audio embed')
  112. else:
  113. entries.append(self.url_result(m['url']))
  114. return self.playlist_result(entries, song_id, title, f'{title} by {artist}')