lastfm.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import itertools
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import int_or_none, parse_qs, traverse_obj
  5. class LastFMPlaylistBaseIE(InfoExtractor):
  6. def _entries(self, url, playlist_id):
  7. single_page = traverse_obj(parse_qs(url), ('page', -1, {int_or_none}))
  8. for page in itertools.count(single_page or 1):
  9. webpage = self._download_webpage(
  10. url, playlist_id, f'Downloading page {page}', query={'page': page})
  11. videos = re.findall(r'data-youtube-url="([^"]+)"', webpage)
  12. yield from videos
  13. if single_page or not videos:
  14. return
  15. def _real_extract(self, url):
  16. playlist_id = self._match_id(url)
  17. return self.playlist_from_matches(self._entries(url, playlist_id), playlist_id, ie='Youtube')
  18. class LastFMPlaylistIE(LastFMPlaylistBaseIE):
  19. _VALID_URL = r'https?://(?:www\.)?last\.fm/(music|tag)/(?P<id>[^/]+)(?:/[^/]+)?/?(?:[?#]|$)'
  20. _TESTS = [{
  21. 'url': 'https://www.last.fm/music/Oasis/(What%27s+the+Story)+Morning+Glory%3F',
  22. 'info_dict': {
  23. 'id': 'Oasis',
  24. },
  25. 'playlist_mincount': 11,
  26. }, {
  27. 'url': 'https://www.last.fm/music/Oasis',
  28. 'only_matching': True,
  29. }, {
  30. 'url': 'https://www.last.fm/music/Oasis/',
  31. 'only_matching': True,
  32. }, {
  33. 'url': 'https://www.last.fm/music/Oasis?top_tracks_date_preset=ALL#top-tracks',
  34. 'only_matching': True,
  35. }, {
  36. 'url': 'https://www.last.fm/music/Oasis/+tracks',
  37. 'only_matching': True,
  38. }, {
  39. 'url': 'https://www.last.fm/music/Oasis/+tracks?page=2',
  40. 'only_matching': True,
  41. }, {
  42. 'url': 'https://www.last.fm/music/Oasis/+tracks?date_preset=LAST_90_DAYS#top-tracks',
  43. 'only_matching': True,
  44. }, {
  45. 'url': 'https://www.last.fm/tag/rock',
  46. 'only_matching': True,
  47. }, {
  48. 'url': 'https://www.last.fm/tag/rock/tracks',
  49. 'only_matching': True,
  50. }]
  51. class LastFMUserIE(LastFMPlaylistBaseIE):
  52. _VALID_URL = r'https?://(?:www\.)?last\.fm/user/[^/]+/playlists/(?P<id>[^/#?]+)'
  53. _TESTS = [{
  54. 'url': 'https://www.last.fm/user/mehq/playlists/12319471',
  55. 'info_dict': {
  56. 'id': '12319471',
  57. },
  58. 'playlist_count': 30,
  59. }, {
  60. 'url': 'https://www.last.fm/user/naamloos1/playlists/12543760',
  61. 'info_dict': {
  62. 'id': '12543760',
  63. },
  64. 'playlist_mincount': 80,
  65. }, {
  66. 'url': 'https://www.last.fm/user/naamloos1/playlists/12543760?page=3',
  67. 'info_dict': {
  68. 'id': '12543760',
  69. },
  70. 'playlist_count': 32,
  71. }]
  72. class LastFMIE(InfoExtractor):
  73. _VALID_URL = r'https?://(?:www\.)?last\.fm/music(?:/[^/]+){2}/(?P<id>[^/#?]+)'
  74. _TESTS = [{
  75. 'url': 'https://www.last.fm/music/Oasis/_/Wonderwall',
  76. 'md5': '9c4a70c2e84c03d54fe24229b9e13b7b',
  77. 'info_dict': {
  78. 'id': '6hzrDeceEKc',
  79. 'ext': 'mp4',
  80. 'title': 'Oasis - Wonderwall (Official Video)',
  81. 'thumbnail': r're:^https?://i.ytimg.com/.*\.jpg$',
  82. 'description': 'md5:0848669853c10687cc28e88b5756738f',
  83. 'uploader': 'Oasis',
  84. 'uploader_id': 'oasisinetofficial',
  85. 'upload_date': '20080207',
  86. 'album': '(What\'s The Story) Morning Glory? (Remastered)',
  87. 'track': 'Wonderwall (Remastered)',
  88. 'channel_id': 'UCUDVBtnOQi4c7E8jebpjc9Q',
  89. 'view_count': int,
  90. 'live_status': 'not_live',
  91. 'channel_url': 'https://www.youtube.com/channel/UCUDVBtnOQi4c7E8jebpjc9Q',
  92. 'tags': 'count:39',
  93. 'creator': 'Oasis',
  94. 'uploader_url': 're:^https?://www.youtube.com/user/oasisinetofficial',
  95. 'duration': 279,
  96. 'alt_title': 'Wonderwall (Remastered)',
  97. 'age_limit': 0,
  98. 'channel': 'Oasis',
  99. 'channel_follower_count': int,
  100. 'categories': ['Music'],
  101. 'availability': 'public',
  102. 'like_count': int,
  103. 'playable_in_embed': True,
  104. 'artist': 'Oasis',
  105. },
  106. 'add_ie': ['Youtube'],
  107. }, {
  108. 'url': 'https://www.last.fm/music/Oasis/_/Don%27t+Look+Back+In+Anger+-+Remastered/',
  109. 'only_matching': True,
  110. }, {
  111. 'url': 'https://www.last.fm/music/Guns+N%27+Roses/_/Sweet+Child+o%27+Mine',
  112. 'only_matching': True,
  113. }]
  114. def _real_extract(self, url):
  115. video_id = self._match_id(url)
  116. webpage = self._download_webpage(url, video_id)
  117. player_url = self._search_regex(r'(?s)class="header-new-playlink"\s+href="([^"]+)"', webpage, 'player_url')
  118. return self.url_result(player_url, 'Youtube')