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