[ie/twitter:spaces] Support video spaces (#10789)
[yt-dlp3.git] / yt_dlp / extractor / lastfm.py
blob67103352e703945b546bd9126ca2d989676984f0
1 import itertools
2 import re
4 from .common import InfoExtractor
5 from ..utils import int_or_none, parse_qs, traverse_obj
8 class LastFMPlaylistBaseIE(InfoExtractor):
9 def _entries(self, url, playlist_id):
10 single_page = traverse_obj(parse_qs(url), ('page', -1, {int_or_none}))
11 for page in itertools.count(single_page or 1):
12 webpage = self._download_webpage(
13 url, playlist_id, f'Downloading page {page}', query={'page': page})
14 videos = re.findall(r'data-youtube-url="([^"]+)"', webpage)
15 yield from videos
16 if single_page or not videos:
17 return
19 def _real_extract(self, url):
20 playlist_id = self._match_id(url)
21 return self.playlist_from_matches(self._entries(url, playlist_id), playlist_id, ie='Youtube')
24 class LastFMPlaylistIE(LastFMPlaylistBaseIE):
25 _VALID_URL = r'https?://(?:www\.)?last\.fm/(music|tag)/(?P<id>[^/]+)(?:/[^/]+)?/?(?:[?#]|$)'
26 _TESTS = [{
27 'url': 'https://www.last.fm/music/Oasis/(What%27s+the+Story)+Morning+Glory%3F',
28 'info_dict': {
29 'id': 'Oasis',
31 'playlist_mincount': 11,
32 }, {
33 'url': 'https://www.last.fm/music/Oasis',
34 'only_matching': True,
35 }, {
36 'url': 'https://www.last.fm/music/Oasis/',
37 'only_matching': True,
38 }, {
39 'url': 'https://www.last.fm/music/Oasis?top_tracks_date_preset=ALL#top-tracks',
40 'only_matching': True,
41 }, {
42 'url': 'https://www.last.fm/music/Oasis/+tracks',
43 'only_matching': True,
44 }, {
45 'url': 'https://www.last.fm/music/Oasis/+tracks?page=2',
46 'only_matching': True,
47 }, {
48 'url': 'https://www.last.fm/music/Oasis/+tracks?date_preset=LAST_90_DAYS#top-tracks',
49 'only_matching': True,
50 }, {
51 'url': 'https://www.last.fm/tag/rock',
52 'only_matching': True,
53 }, {
54 'url': 'https://www.last.fm/tag/rock/tracks',
55 'only_matching': True,
59 class LastFMUserIE(LastFMPlaylistBaseIE):
60 _VALID_URL = r'https?://(?:www\.)?last\.fm/user/[^/]+/playlists/(?P<id>[^/#?]+)'
61 _TESTS = [{
62 'url': 'https://www.last.fm/user/mehq/playlists/12319471',
63 'info_dict': {
64 'id': '12319471',
66 'playlist_count': 30,
67 }, {
68 'url': 'https://www.last.fm/user/naamloos1/playlists/12543760',
69 'info_dict': {
70 'id': '12543760',
72 'playlist_mincount': 80,
73 }, {
74 'url': 'https://www.last.fm/user/naamloos1/playlists/12543760?page=3',
75 'info_dict': {
76 'id': '12543760',
78 'playlist_count': 32,
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',
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,
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')