Merge branch 'master' into rtmpdump
[youtube-dl] / youtube_dl / extractor / livestream.py
1 import re
2 import json
3 import xml.etree.ElementTree
4
5 from .common import InfoExtractor
6 from ..utils import (
7     compat_urllib_parse_urlparse,
8     compat_urlparse,
9     get_meta_content,
10     xpath_with_ns,
11     ExtractorError,
12 )
13
14
15 class LivestreamIE(InfoExtractor):
16     IE_NAME = u'livestream'
17     _VALID_URL = r'http://new.livestream.com/.*?/(?P<event_name>.*?)(/videos/(?P<id>\d+))?/?$'
18     _TEST = {
19         u'url': u'http://new.livestream.com/CoheedandCambria/WebsterHall/videos/4719370',
20         u'file': u'4719370.mp4',
21         u'md5': u'0d2186e3187d185a04b3cdd02b828836',
22         u'info_dict': {
23             u'title': u'Live from Webster Hall NYC',
24             u'upload_date': u'20121012',
25         }
26     }
27
28     def _extract_video_info(self, video_data):
29         video_url = video_data.get('progressive_url_hd') or video_data.get('progressive_url')
30         return {'id': video_data['id'],
31                 'url': video_url,
32                 'ext': 'mp4',
33                 'title': video_data['caption'],
34                 'thumbnail': video_data['thumbnail_url'],
35                 'upload_date': video_data['updated_at'].replace('-','')[:8],
36                 }
37
38     def _real_extract(self, url):
39         mobj = re.match(self._VALID_URL, url)
40         video_id = mobj.group('id')
41         event_name = mobj.group('event_name')
42         webpage = self._download_webpage(url, video_id or event_name)
43
44         if video_id is None:
45             # This is an event page:
46             config_json = self._search_regex(r'window.config = ({.*?});',
47                 webpage, u'window config')
48             info = json.loads(config_json)['event']
49             videos = [self._extract_video_info(video_data['data'])
50                 for video_data in info['feed']['data'] if video_data['type'] == u'video']
51             return self.playlist_result(videos, info['id'], info['full_name'])
52         else:
53             og_video = self._og_search_video_url(webpage, name=u'player url')
54             query_str = compat_urllib_parse_urlparse(og_video).query
55             query = compat_urlparse.parse_qs(query_str)
56             api_url = query['play_url'][0].replace('.smil', '')
57             info = json.loads(self._download_webpage(api_url, video_id,
58                                                      u'Downloading video info'))
59             return self._extract_video_info(info)
60
61
62 # The original version of Livestream uses a different system
63 class LivestreamOriginalIE(InfoExtractor):
64     IE_NAME = u'livestream:original'
65     _VALID_URL = r'https?://www\.livestream\.com/(?P<user>[^/]+)/video\?.*?clipId=(?P<id>.*?)(&|$)'
66     _TEST = {
67         u'url': u'http://www.livestream.com/dealbook/video?clipId=pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
68         u'info_dict': {
69             u'id': u'pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
70             u'ext': u'flv',
71             u'title': u'Spark 1 (BitCoin) with Cameron Winklevoss & Tyler Winklevoss of Winklevoss Capital',
72         },
73         u'params': {
74             # rtmp
75             u'skip_download': True,
76         },
77     }
78
79     def _real_extract(self, url):
80         mobj = re.match(self._VALID_URL, url)
81         video_id = mobj.group('id')
82         user = mobj.group('user')
83         api_url = 'http://x{0}x.api.channel.livestream.com/2.0/clipdetails?extendedInfo=true&id={1}'.format(user, video_id)
84
85         api_response = self._download_webpage(api_url, video_id)
86         info = xml.etree.ElementTree.fromstring(api_response.encode('utf-8'))
87         item = info.find('channel').find('item')
88         ns = {'media': 'http://search.yahoo.com/mrss'}
89         thumbnail_url = item.find(xpath_with_ns('media:thumbnail', ns)).attrib['url']
90         # Remove the extension and number from the path (like 1.jpg)
91         path = self._search_regex(r'(user-files/.+)_.*?\.jpg$', thumbnail_url, u'path')
92
93         return {
94             'id': video_id,
95             'title': item.find('title').text,
96             'url': 'rtmp://extondemand.livestream.com/ondemand',
97             'play_path': 'mp4:trans/dv15/mogulus-{0}.mp4'.format(path),
98             'ext': 'flv',
99             'thumbnail': thumbnail_url,
100         }