[livestream] Make clipBegin optional in SMIL
[youtube-dl] / youtube_dl / extractor / livestream.py
1 from __future__ import unicode_literals
2
3 import re
4 import json
5
6 from .common import InfoExtractor
7 from ..utils import (
8     compat_str,
9     compat_urllib_parse_urlparse,
10     compat_urlparse,
11     ExtractorError,
12     find_xpath_attr,
13     int_or_none,
14     orderedSet,
15     xpath_with_ns,
16 )
17
18
19 class LivestreamIE(InfoExtractor):
20     IE_NAME = 'livestream'
21     _VALID_URL = r'http://new\.livestream\.com/.*?/(?P<event_name>.*?)(/videos/(?P<id>\d+))?/?$'
22     _TEST = {
23         'url': 'http://new.livestream.com/CoheedandCambria/WebsterHall/videos/4719370',
24         'md5': '53274c76ba7754fb0e8d072716f2292b',
25         'info_dict': {
26             'id': '4719370',
27             'ext': 'mp4',
28             'title': 'Live from Webster Hall NYC',
29             'upload_date': '20121012',
30             'like_count': int,
31             'view_count': int,
32             'thumbnail': 're:^http://.*\.jpg$'
33         }
34     }
35
36     def _extract_video_info(self, video_data):
37         video_id = compat_str(video_data['id'])
38
39         FORMAT_KEYS = (
40             ('sd', 'progressive_url'),
41             ('hd', 'progressive_url_hd'),
42         )
43         formats = [{
44             'format_id': format_id,
45             'url': video_data[key],
46             'quality': i + 1,
47         } for i, (format_id, key) in enumerate(FORMAT_KEYS)
48             if video_data.get(key)]
49
50         smil_url = video_data.get('smil_url')
51         if smil_url:
52             _SWITCH_XPATH = (
53                 './/{http://www.w3.org/2001/SMIL20/Language}body/'
54                 '{http://www.w3.org/2001/SMIL20/Language}switch')
55             smil_doc = self._download_xml(
56                 smil_url, video_id, note='Downloading SMIL information')
57
58             title_node = find_xpath_attr(
59                 smil_doc, './/{http://www.w3.org/2001/SMIL20/Language}meta',
60                 'name', 'title')
61             if title_node is None:
62                 self.report_warning('Cannot find SMIL id')
63                 switch_node = smil_doc.find(_SWITCH_XPATH)
64             else:
65                 title_id = title_node.attrib['content']
66                 switch_node = find_xpath_attr(
67                     smil_doc, _SWITCH_XPATH, 'id', title_id)
68             if switch_node is None:
69                 raise ExtractorError('Cannot find switch node')
70             video_nodes = switch_node.findall(
71                 '{http://www.w3.org/2001/SMIL20/Language}video')
72
73             for vn in video_nodes:
74                 tbr = int_or_none(vn.attrib.get('system-bitrate'))
75                 furl = (
76                     'http://livestream-f.akamaihd.net/%s?v=3.0.3&fp=WIN%%2014,0,0,145' %
77                     (vn.attrib['src']))
78                 if 'clipBegin' in vn.attrib:
79                     furl += '&ssek=' + vn.attrib['clipBegin']
80                 formats.append({
81                     'url': furl,
82                     'format_id': 'smil_%d' % tbr,
83                     'ext': 'flv',
84                     'tbr': tbr,
85                     'preference': -1000,
86                 })
87         self._sort_formats(formats)
88
89         return {
90             'id': video_id,
91             'formats': formats,
92             'title': video_data['caption'],
93             'thumbnail': video_data.get('thumbnail_url'),
94             'upload_date': video_data['updated_at'].replace('-', '')[:8],
95             'like_count': video_data.get('likes', {}).get('total'),
96             'view_count': video_data.get('views'),
97         }
98
99     def _real_extract(self, url):
100         mobj = re.match(self._VALID_URL, url)
101         video_id = mobj.group('id')
102         event_name = mobj.group('event_name')
103         webpage = self._download_webpage(url, video_id or event_name)
104
105         if video_id is None:
106             # This is an event page:
107             config_json = self._search_regex(
108                 r'window.config = ({.*?});', webpage, 'window config')
109             info = json.loads(config_json)['event']
110             videos = [self._extract_video_info(video_data['data'])
111                 for video_data in info['feed']['data']
112                 if video_data['type'] == 'video']
113             return self.playlist_result(videos, info['id'], info['full_name'])
114         else:
115             og_video = self._og_search_video_url(webpage, 'player url')
116             query_str = compat_urllib_parse_urlparse(og_video).query
117             query = compat_urlparse.parse_qs(query_str)
118             api_url = query['play_url'][0].replace('.smil', '')
119             info = json.loads(self._download_webpage(
120                 api_url, video_id, 'Downloading video info'))
121             return self._extract_video_info(info)
122
123
124 # The original version of Livestream uses a different system
125 class LivestreamOriginalIE(InfoExtractor):
126     IE_NAME = 'livestream:original'
127     _VALID_URL = r'''(?x)https?://www\.livestream\.com/
128         (?P<user>[^/]+)/(?P<type>video|folder)
129         (?:\?.*?Id=|/)(?P<id>.*?)(&|$)
130         '''
131     _TEST = {
132         'url': 'http://www.livestream.com/dealbook/video?clipId=pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
133         'info_dict': {
134             'id': 'pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
135             'ext': 'flv',
136             'title': 'Spark 1 (BitCoin) with Cameron Winklevoss & Tyler Winklevoss of Winklevoss Capital',
137         },
138         'params': {
139             # rtmp
140             'skip_download': True,
141         },
142     }
143
144     def _extract_video(self, user, video_id):
145         api_url = 'http://x{0}x.api.channel.livestream.com/2.0/clipdetails?extendedInfo=true&id={1}'.format(user, video_id)
146
147         info = self._download_xml(api_url, video_id)
148         item = info.find('channel').find('item')
149         ns = {'media': 'http://search.yahoo.com/mrss'}
150         thumbnail_url = item.find(xpath_with_ns('media:thumbnail', ns)).attrib['url']
151         # Remove the extension and number from the path (like 1.jpg)
152         path = self._search_regex(r'(user-files/.+)_.*?\.jpg$', thumbnail_url, 'path')
153
154         return {
155             'id': video_id,
156             'title': item.find('title').text,
157             'url': 'rtmp://extondemand.livestream.com/ondemand',
158             'play_path': 'mp4:trans/dv15/mogulus-{0}.mp4'.format(path),
159             'ext': 'flv',
160             'thumbnail': thumbnail_url,
161         }
162
163     def _extract_folder(self, url, folder_id):
164         webpage = self._download_webpage(url, folder_id)
165         urls = orderedSet(re.findall(r'<a href="(https?://livestre\.am/.*?)"', webpage))
166
167         return {
168             '_type': 'playlist',
169             'id': folder_id,
170             'entries': [{
171                 '_type': 'url',
172                 'url': video_url,
173             } for video_url in urls],
174         }
175
176     def _real_extract(self, url):
177         mobj = re.match(self._VALID_URL, url)
178         id = mobj.group('id')
179         user = mobj.group('user')
180         url_type = mobj.group('type')
181         if url_type == 'folder':
182             return self._extract_folder(url, id)
183         else:
184             return self._extract_video(user, id)
185
186
187 # The server doesn't support HEAD request, the generic extractor can't detect
188 # the redirection
189 class LivestreamShortenerIE(InfoExtractor):
190     IE_NAME = 'livestream:shortener'
191     IE_DESC = False  # Do not list
192     _VALID_URL = r'https?://livestre\.am/(?P<id>.+)'
193
194     def _real_extract(self, url):
195         mobj = re.match(self._VALID_URL, url)
196         id = mobj.group('id')
197         webpage = self._download_webpage(url, id)
198
199         return {
200             '_type': 'url',
201             'url': self._og_search_url(webpage),
202         }