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