Merge remote-tracking branch 'naglis/sockshare'
[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_urllib_parse_urlparse,
9     compat_urlparse,
10     xpath_with_ns,
11     compat_str,
12     orderedSet,
13 )
14
15
16 class LivestreamIE(InfoExtractor):
17     IE_NAME = 'livestream'
18     _VALID_URL = r'http://new\.livestream\.com/.*?/(?P<event_name>.*?)(/videos/(?P<id>\d+))?/?$'
19     _TEST = {
20         'url': 'http://new.livestream.com/CoheedandCambria/WebsterHall/videos/4719370',
21         'md5': '53274c76ba7754fb0e8d072716f2292b',
22         'info_dict': {
23             'id': '4719370',
24             'ext': 'mp4',
25             'title': 'Live from Webster Hall NYC',
26             'upload_date': '20121012',
27         }
28     }
29
30     def _extract_video_info(self, video_data):
31         video_url = (
32             video_data.get('progressive_url_hd') or
33             video_data.get('progressive_url')
34         )
35         return {
36             'id': compat_str(video_data['id']),
37             'url': video_url,
38             'title': video_data['caption'],
39             'thumbnail': video_data['thumbnail_url'],
40             'upload_date': video_data['updated_at'].replace('-', '')[:8],
41         }
42
43     def _real_extract(self, url):
44         mobj = re.match(self._VALID_URL, url)
45         video_id = mobj.group('id')
46         event_name = mobj.group('event_name')
47         webpage = self._download_webpage(url, video_id or event_name)
48
49         if video_id is None:
50             # This is an event page:
51             config_json = self._search_regex(
52                 r'window.config = ({.*?});', webpage, 'window config')
53             info = json.loads(config_json)['event']
54             videos = [self._extract_video_info(video_data['data'])
55                 for video_data in info['feed']['data']
56                 if video_data['type'] == 'video']
57             return self.playlist_result(videos, info['id'], info['full_name'])
58         else:
59             og_video = self._og_search_video_url(webpage, 'player url')
60             query_str = compat_urllib_parse_urlparse(og_video).query
61             query = compat_urlparse.parse_qs(query_str)
62             api_url = query['play_url'][0].replace('.smil', '')
63             info = json.loads(self._download_webpage(
64                 api_url, video_id, 'Downloading video info'))
65             return self._extract_video_info(info)
66
67
68 # The original version of Livestream uses a different system
69 class LivestreamOriginalIE(InfoExtractor):
70     IE_NAME = 'livestream:original'
71     _VALID_URL = r'''(?x)https?://www\.livestream\.com/
72         (?P<user>[^/]+)/(?P<type>video|folder)
73         (?:\?.*?Id=|/)(?P<id>.*?)(&|$)
74         '''
75     _TEST = {
76         'url': 'http://www.livestream.com/dealbook/video?clipId=pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
77         'info_dict': {
78             'id': 'pla_8aa4a3f1-ba15-46a4-893b-902210e138fb',
79             'ext': 'flv',
80             'title': 'Spark 1 (BitCoin) with Cameron Winklevoss & Tyler Winklevoss of Winklevoss Capital',
81         },
82         'params': {
83             # rtmp
84             'skip_download': True,
85         },
86     }
87
88     def _extract_video(self, user, video_id):
89         api_url = 'http://x{0}x.api.channel.livestream.com/2.0/clipdetails?extendedInfo=true&id={1}'.format(user, video_id)
90
91         info = self._download_xml(api_url, video_id)
92         item = info.find('channel').find('item')
93         ns = {'media': 'http://search.yahoo.com/mrss'}
94         thumbnail_url = item.find(xpath_with_ns('media:thumbnail', ns)).attrib['url']
95         # Remove the extension and number from the path (like 1.jpg)
96         path = self._search_regex(r'(user-files/.+)_.*?\.jpg$', thumbnail_url, 'path')
97
98         return {
99             'id': video_id,
100             'title': item.find('title').text,
101             'url': 'rtmp://extondemand.livestream.com/ondemand',
102             'play_path': 'mp4:trans/dv15/mogulus-{0}.mp4'.format(path),
103             'ext': 'flv',
104             'thumbnail': thumbnail_url,
105         }
106
107     def _extract_folder(self, url, folder_id):
108         webpage = self._download_webpage(url, folder_id)
109         urls = orderedSet(re.findall(r'<a href="(https?://livestre\.am/.*?)"', webpage))
110
111         return {
112             '_type': 'playlist',
113             'id': folder_id,
114             'entries': [{
115                 '_type': 'url',
116                 'url': video_url,
117             } for video_url in urls],
118         }
119
120     def _real_extract(self, url):
121         mobj = re.match(self._VALID_URL, url)
122         id = mobj.group('id')
123         user = mobj.group('user')
124         url_type = mobj.group('type')
125         if url_type == 'folder':
126             return self._extract_folder(url, id)
127         else:
128             return self._extract_video(user, id)
129
130
131 # The server doesn't support HEAD request, the generic extractor can't detect
132 # the redirection
133 class LivestreamShortenerIE(InfoExtractor):
134     IE_NAME = 'livestream:shortener'
135     IE_DESC = False  # Do not list
136     _VALID_URL = r'https?://livestre\.am/(?P<id>.+)'
137
138     def _real_extract(self, url):
139         mobj = re.match(self._VALID_URL, url)
140         id = mobj.group('id')
141         webpage = self._download_webpage(url, id)
142
143         return {
144             '_type': 'url',
145             'url': self._og_search_url(webpage),
146         }