[myspace] Handle non-playable songs
[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             song_data = self._search_regex(
57                 r'''<button.*data-song-id=(["\'])%s\1.*''' % video_id,
58                 webpage, 'song_data', default=None, group=0)
59             if song_data is None:
60                 self.to_screen(
61                     '%s: No downloadable song on this page' % video_id)
62                 return
63             def search_data(name):
64                 return self._search_regex(
65                     r'''data-%s=([\'"])(.*?)\1''' % name,
66                     song_data, name, default='', group=2)
67             streamUrl = search_data('stream-url')
68             info = {
69                 'id': video_id,
70                 'title': self._og_search_title(webpage),
71                 'uploader': search_data('artist-name'),
72                 'uploader_id': search_data('artist-username'),
73                 'playlist': search_data('album-title'),
74                 'thumbnail': self._og_search_thumbnail(webpage),
75             }
76         else:
77             context = json.loads(self._search_regex(
78                 r'context = ({.*?});', webpage, 'context'))
79             video = context['video']
80             streamUrl = video['streamUrl']
81             info = {
82                 'id': compat_str(video['mediaId']),
83                 'title': video['title'],
84                 'description': video['description'],
85                 'thumbnail': video['imageUrl'],
86                 'uploader': video['artistName'],
87                 'uploader_id': video['artistUsername'],
88             }
89
90         rtmp_url, play_path = streamUrl.split(';', 1)
91         info.update({
92             'url': rtmp_url,
93             'play_path': play_path,
94             'player_url': player_url,
95             'ext': 'flv',
96         })
97         return info