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