]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/livestream.py
d04da98c89ed582e83e8bb905b15ff04c78d3018
[youtubedl] / 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 get_meta_content,
9 ExtractorError,
10 )
11
12
13 class LivestreamIE(InfoExtractor):
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 player = get_meta_content('twitter:player', webpage)
44 if player is None:
45 raise ExtractorError('Couldn\'t extract event api url')
46 api_url = player.replace('/player', '')
47 api_url = re.sub(r'^(https?://)(new\.)', r'\1api.\2', api_url)
48 info = json.loads(self._download_webpage(api_url, event_name,
49 u'Downloading event info'))
50 videos = [self._extract_video_info(video_data['data'])
51 for video_data in info['feed']['data'] if video_data['type'] == u'video']
52 return self.playlist_result(videos, info['id'], info['full_name'])
53 else:
54 og_video = self._og_search_video_url(webpage, name=u'player url')
55 query_str = compat_urllib_parse_urlparse(og_video).query
56 query = compat_urlparse.parse_qs(query_str)
57 api_url = query['play_url'][0].replace('.smil', '')
58 info = json.loads(self._download_webpage(api_url, video_id,
59 u'Downloading video info'))
60 return self._extract_video_info(info)