lastfm.py 4.7 KB

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