]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/myspace.py
[ie/matchtv] Fix extractor (#10190)
[yt-dlp.git] / yt_dlp / extractor / myspace.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5 ExtractorError,
6 int_or_none,
7 parse_iso8601,
8 )
9
10
11 class MySpaceIE(InfoExtractor):
12 _VALID_URL = r'''(?x)
13 https?://
14 myspace\.com/[^/]+/
15 (?P<mediatype>
16 video/[^/]+/(?P<video_id>\d+)|
17 music/song/[^/?#&]+-(?P<song_id>\d+)-\d+(?:[/?#&]|$)
18 )
19 '''
20
21 _TESTS = [{
22 'url': 'https://myspace.com/fiveminutestothestage/video/little-big-town/109594919',
23 'md5': '9c1483c106f4a695c47d2911feed50a7',
24 'info_dict': {
25 'id': '109594919',
26 'ext': 'mp4',
27 'title': 'Little Big Town',
28 'description': 'This country quartet was all smiles while playing a sold out show at the Pacific Amphitheatre in Orange County, California.',
29 'uploader': 'Five Minutes to the Stage',
30 'uploader_id': 'fiveminutestothestage',
31 'timestamp': 1414108751,
32 'upload_date': '20141023',
33 },
34 }, {
35 # songs
36 'url': 'https://myspace.com/killsorrow/music/song/of-weakened-soul...-93388656-103880681',
37 'md5': '1d7ee4604a3da226dd69a123f748b262',
38 'info_dict': {
39 'id': '93388656',
40 'ext': 'm4a',
41 'title': 'Of weakened soul...',
42 'uploader': 'Killsorrow',
43 'uploader_id': 'killsorrow',
44 },
45 }, {
46 'url': 'https://myspace.com/starset2/music/song/first-light-95799905-106964426',
47 'only_matching': True,
48 }, {
49 'url': 'https://myspace.com/thelargemouthbassband/music/song/02-pure-eyes.mp3-94422330-105113388',
50 'only_matching': True,
51 }]
52
53 def _real_extract(self, url):
54 mobj = self._match_valid_url(url)
55 video_id = mobj.group('video_id') or mobj.group('song_id')
56 is_song = mobj.group('mediatype').startswith('music/song')
57 webpage = self._download_webpage(url, video_id)
58 player_url = self._search_regex(
59 r'videoSwf":"([^"?]*)', webpage, 'player URL', fatal=False)
60
61 def formats_from_stream_urls(stream_url, hls_stream_url, http_stream_url, width=None, height=None):
62 formats = []
63 vcodec = 'none' if is_song else None
64 if hls_stream_url:
65 formats.append({
66 'format_id': 'hls',
67 'url': hls_stream_url,
68 'protocol': 'm3u8_native',
69 'ext': 'm4a' if is_song else 'mp4',
70 'vcodec': vcodec,
71 })
72 if stream_url and player_url:
73 rtmp_url, play_path = stream_url.split(';', 1)
74 formats.append({
75 'format_id': 'rtmp',
76 'url': rtmp_url,
77 'play_path': play_path,
78 'player_url': player_url,
79 'protocol': 'rtmp',
80 'ext': 'flv',
81 'width': width,
82 'height': height,
83 'vcodec': vcodec,
84 })
85 if http_stream_url:
86 formats.append({
87 'format_id': 'http',
88 'url': http_stream_url,
89 'width': width,
90 'height': height,
91 'vcodec': vcodec,
92 })
93 return formats
94
95 if is_song:
96 # songs don't store any useful info in the 'context' variable
97 song_data = self._search_regex(
98 rf'''<button.*data-song-id=(["\']){video_id}\1.*''',
99 webpage, 'song_data', default=None, group=0)
100 if song_data is None:
101 # some songs in an album are not playable
102 self.report_warning(
103 f'{video_id}: No downloadable song on this page')
104 return
105
106 def search_data(name):
107 return self._search_regex(
108 rf'''data-{name}=([\'"])(?P<data>.*?)\1''',
109 song_data, name, default='', group='data')
110 formats = formats_from_stream_urls(
111 search_data('stream-url'), search_data('hls-stream-url'),
112 search_data('http-stream-url'))
113 if not formats:
114 vevo_id = search_data('vevo-id')
115 youtube_id = search_data('youtube-id')
116 if vevo_id:
117 self.to_screen(f'Vevo video detected: {vevo_id}')
118 return self.url_result(f'vevo:{vevo_id}', ie='Vevo')
119 elif youtube_id:
120 self.to_screen(f'Youtube video detected: {youtube_id}')
121 return self.url_result(youtube_id, ie='Youtube')
122 else:
123 raise ExtractorError(
124 'Found song but don\'t know how to download it')
125 return {
126 'id': video_id,
127 'title': self._og_search_title(webpage),
128 'uploader': search_data('artist-name'),
129 'uploader_id': search_data('artist-username'),
130 'thumbnail': self._og_search_thumbnail(webpage),
131 'duration': int_or_none(search_data('duration')),
132 'formats': formats,
133 }
134 else:
135 video = self._parse_json(self._search_regex(
136 r'context = ({.*?});', webpage, 'context'),
137 video_id)['video']
138 formats = formats_from_stream_urls(
139 video.get('streamUrl'), video.get('hlsStreamUrl'),
140 video.get('mp4StreamUrl'), int_or_none(video.get('width')),
141 int_or_none(video.get('height')))
142 return {
143 'id': video_id,
144 'title': video['title'],
145 'description': video.get('description'),
146 'thumbnail': video.get('imageUrl'),
147 'uploader': video.get('artistName'),
148 'uploader_id': video.get('artistUsername'),
149 'duration': int_or_none(video.get('duration')),
150 'timestamp': parse_iso8601(video.get('dateAdded')),
151 'formats': formats,
152 }
153
154
155 class MySpaceAlbumIE(InfoExtractor):
156 IE_NAME = 'MySpace:album'
157 _VALID_URL = r'https?://myspace\.com/([^/]+)/music/album/(?P<title>.*-)(?P<id>\d+)'
158
159 _TESTS = [{
160 'url': 'https://myspace.com/starset2/music/album/transmissions-19455773',
161 'info_dict': {
162 'title': 'Transmissions',
163 'id': '19455773',
164 },
165 'playlist_count': 14,
166 'skip': 'this album is only available in some countries',
167 }, {
168 'url': 'https://myspace.com/killsorrow/music/album/the-demo-18596029',
169 'info_dict': {
170 'title': 'The Demo',
171 'id': '18596029',
172 },
173 'playlist_count': 5,
174 }]
175
176 def _real_extract(self, url):
177 mobj = self._match_valid_url(url)
178 playlist_id = mobj.group('id')
179 display_id = mobj.group('title') + playlist_id
180 webpage = self._download_webpage(url, display_id)
181 tracks_paths = re.findall(r'"music:song" content="(.*?)"', webpage)
182 if not tracks_paths:
183 raise ExtractorError(
184 f'{display_id}: No songs found, try using proxy',
185 expected=True)
186 entries = [
187 self.url_result(t_path, ie=MySpaceIE.ie_key())
188 for t_path in tracks_paths]
189 return {
190 '_type': 'playlist',
191 'id': playlist_id,
192 'display_id': display_id,
193 'title': self._og_search_title(webpage),
194 'entries': entries,
195 }