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