[myspace] Use player_url for faster download
[youtube-dl] / youtube_dl / extractor / myspace.py
1 from __future__ import unicode_literals
2
3 import re
4 import json
5
6 from .common import InfoExtractor
7 from ..compat import (
8     compat_str,
9 )
10
11
12 class MySpaceIE(InfoExtractor):
13     _VALID_URL = r'https?://myspace\.com/([^/]+)/(?P<mediatype>video/[^/]+/|music/song/.*?)(?P<id>\d+)'
14
15     _TESTS = [
16         {
17             'url': 'https://myspace.com/coldplay/video/viva-la-vida/100008689',
18             'info_dict': {
19                 'id': '100008689',
20                 'ext': 'flv',
21                 'title': 'Viva La Vida',
22                 'description': 'The official Viva La Vida video, directed by Hype Williams',
23                 'uploader': 'Coldplay',
24                 'uploader_id': 'coldplay',
25             },
26             'params': {
27                 # rtmp download
28                 'skip_download': True,
29             },
30         },
31         # song
32         {
33             'url': 'https://myspace.com/spiderbags/music/song/darkness-in-my-heart-39008454-27041242',
34             'info_dict': {
35                 'id': '39008454',
36                 'ext': 'flv',
37                 'title': 'Darkness In My Heart',
38                 'uploader_id': 'spiderbags',
39             },
40             'params': {
41                 # rtmp download
42                 'skip_download': True,
43             },
44         },
45     ]
46
47     def _real_extract(self, url):
48         mobj = re.match(self._VALID_URL, url)
49         video_id = mobj.group('id')
50         webpage = self._download_webpage(url, video_id)
51         player_url = self._search_regex(
52             r'playerSwf":"([^"?]*)', webpage, 'player URL')
53
54         if mobj.group('mediatype').startswith('music/song'):
55             # songs don't store any useful info in the 'context' variable
56             def search_data(name):
57                 return self._search_regex(
58                     r'data-%s="(.*?)"' % name, webpage, name)
59             streamUrl = search_data('stream-url')
60             info = {
61                 'id': video_id,
62                 'title': self._og_search_title(webpage),
63                 'uploader_id': search_data('artist-username'),
64                 'thumbnail': self._og_search_thumbnail(webpage),
65             }
66         else:
67             context = json.loads(self._search_regex(
68                 r'context = ({.*?});', webpage, 'context'))
69             video = context['video']
70             streamUrl = video['streamUrl']
71             info = {
72                 'id': compat_str(video['mediaId']),
73                 'title': video['title'],
74                 'description': video['description'],
75                 'thumbnail': video['imageUrl'],
76                 'uploader': video['artistName'],
77                 'uploader_id': video['artistUsername'],
78             }
79
80         rtmp_url, play_path = streamUrl.split(';', 1)
81         info.update({
82             'url': rtmp_url,
83             'play_path': play_path,
84             'player_url': player_url,
85             'ext': 'flv',
86         })
87         return info