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