genius.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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<article>a))/(?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. 'url': 'https://genius.com/a/cordae-anderson-paak-break-down-the-meaning-of-two-tens',
  44. 'md5': 'f98a4e03b16b0a2821bd6e52fb3cc9d7',
  45. 'info_dict': {
  46. 'id': '6321509903112',
  47. 'ext': 'mp4',
  48. 'title': 'Cordae & Anderson .Paak Breaks Down The Meaning Of “Two Tens”',
  49. 'description': 'md5:1255f0e1161d07342ce56a8464ac339d',
  50. 'tags': ['song id: 5457554'],
  51. 'uploader_id': '4863540648001',
  52. 'duration': 361.813,
  53. 'upload_date': '20230301',
  54. 'timestamp': 1677703908,
  55. 'thumbnail': r're:^https?://.*\.jpg$',
  56. },
  57. }]
  58. def _real_extract(self, url):
  59. display_id, is_article = self._match_valid_url(url).group('id', 'article')
  60. webpage = self._download_webpage(url, display_id)
  61. metadata = self._search_json(
  62. r'<meta content="', webpage, 'metadata', display_id,
  63. end_pattern=r'"\s+itemprop="page_data"', transform_source=unescapeHTML)
  64. video_id = traverse_obj(metadata, (
  65. (('article', 'media', ...), ('video', None)),
  66. ('provider_id', ('dfp_kv', lambda _, v: v['name'] == 'brightcove_video_id', 'values', ...))),
  67. get_all=False)
  68. if not video_id:
  69. # Not all article pages have videos, expect the error
  70. raise ExtractorError('Brightcove video ID not found in webpage', expected=bool(is_article))
  71. config = self._search_json(r'var\s*APP_CONFIG\s*=', webpage, 'config', video_id, default={})
  72. account_id = config.get('brightcove_account_id', '4863540648001')
  73. player_id = traverse_obj(
  74. config, 'brightcove_standard_web_player_id', 'brightcove_standard_no_autoplay_web_player_id',
  75. 'brightcove_modal_web_player_id', 'brightcove_song_story_web_player_id', default='S1ZcmcOC1x')
  76. return self.url_result(
  77. smuggle_url(
  78. f'https://players.brightcove.net/{account_id}/{player_id}_default/index.html?videoId={video_id}',
  79. {'referrer': url}), 'BrightcoveNew', video_id)
  80. class GeniusLyricsIE(InfoExtractor):
  81. _VALID_URL = r'https?://(?:www\.)?genius\.com/(?P<id>[^?/#]+)-lyrics(?:[?/#]|$)'
  82. _TESTS = [{
  83. 'url': 'https://genius.com/Lil-baby-heyy-lyrics',
  84. 'playlist_mincount': 2,
  85. 'info_dict': {
  86. 'id': '8454545',
  87. 'title': 'Heyy',
  88. 'description': 'Heyy by Lil Baby',
  89. },
  90. }, {
  91. 'url': 'https://genius.com/Outkast-two-dope-boyz-in-a-cadillac-lyrics',
  92. 'playlist_mincount': 1,
  93. 'info_dict': {
  94. 'id': '36239',
  95. 'title': 'Two Dope Boyz (In a Cadillac)',
  96. 'description': 'Two Dope Boyz (In a Cadillac) by OutKast',
  97. },
  98. }, {
  99. 'url': 'https://genius.com/Playboi-carti-rip-lyrics',
  100. 'playlist_mincount': 1,
  101. 'info_dict': {
  102. 'id': '3710582',
  103. 'title': 'R.I.P.',
  104. 'description': 'R.I.P. by Playboi Carti',
  105. },
  106. }]
  107. def _real_extract(self, url):
  108. display_id = self._match_id(url)
  109. webpage = self._download_webpage(url, display_id)
  110. json_string = self._search_json(
  111. r'window\.__PRELOADED_STATE__\s*=\s*JSON\.parse\(', webpage, 'json string',
  112. display_id, transform_source=js_to_json, contains_pattern=r'\'{(?s:.+)}\'')
  113. song_info = self._parse_json(json_string, display_id)
  114. song_id = str_or_none(traverse_obj(song_info, ('songPage', 'song')))
  115. if not song_id:
  116. raise ExtractorError('Song id not found in webpage')
  117. title = traverse_obj(
  118. song_info, ('songPage', 'trackingData', lambda _, x: x['key'] == 'Title', 'value'),
  119. get_all=False, default='untitled')
  120. artist = traverse_obj(
  121. song_info, ('songPage', 'trackingData', lambda _, x: x['key'] == 'Primary Artist', 'value'),
  122. get_all=False, default='unknown artist')
  123. media = traverse_obj(
  124. song_info, ('entities', 'songs', song_id, 'media'), expected_type=list, default=[])
  125. entries = []
  126. for m in media:
  127. if m.get('type') in ('video', 'audio') and m.get('url'):
  128. if m.get('provider') == 'spotify':
  129. self.to_screen(f'{song_id}: Skipping Spotify audio embed')
  130. else:
  131. entries.append(self.url_result(m['url']))
  132. return self.playlist_result(entries, song_id, title, f'{title} by {artist}')