]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/newstube.py
[README.md] Make download URLs consistent
[youtube-dl] / youtube_dl / extractor / newstube.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     ExtractorError,
9     int_or_none,
10 )
11
12
13 class NewstubeIE(InfoExtractor):
14     _VALID_URL = r'https?://(?:www\.)?newstube\.ru/media/(?P<id>.+)'
15     _TEST = {
16         'url': 'http://www.newstube.ru/media/telekanal-cnn-peremestil-gorod-slavyansk-v-krym',
17         'md5': '801eef0c2a9f4089fa04e4fe3533abdc',
18         'info_dict': {
19             'id': '728e0ef2-e187-4012-bac0-5a081fdcb1f6',
20             'ext': 'mp4',
21             'title': 'Телеканал CNN переместил город Славянск в Крым',
22             'description': 'md5:419a8c9f03442bc0b0a794d689360335',
23             'duration': 31.05,
24         },
25     }
26
27     def _real_extract(self, url):
28         mobj = re.match(self._VALID_URL, url)
29         video_id = mobj.group('id')
30
31         page = self._download_webpage(url, video_id, 'Downloading page')
32
33         video_guid = self._html_search_regex(
34             r'<meta property="og:video:url" content="https?://(?:www\.)?newstube\.ru/freshplayer\.swf\?guid=(?P<guid>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})',
35             page, 'video GUID')
36
37         player = self._download_xml(
38             'http://p.newstube.ru/v2/player.asmx/GetAutoPlayInfo6?state=&url=%s&sessionId=&id=%s&placement=profile&location=n2' % (url, video_guid),
39             video_guid, 'Downloading player XML')
40
41         def ns(s):
42             return s.replace('/', '/%(ns)s') % {'ns': '{http://app1.newstube.ru/N2SiteWS/player.asmx}'}
43
44         error_message = player.find(ns('./ErrorMessage'))
45         if error_message is not None:
46             raise ExtractorError('%s returned error: %s' % (self.IE_NAME, error_message.text), expected=True)
47
48         session_id = player.find(ns('./SessionId')).text
49         media_info = player.find(ns('./Medias/MediaInfo'))
50         title = media_info.find(ns('./Name')).text
51         description = self._og_search_description(page)
52         thumbnail = media_info.find(ns('./KeyFrame')).text
53         duration = int(media_info.find(ns('./Duration')).text) / 1000.0
54
55         formats = []
56
57         for stream_info in media_info.findall(ns('./Streams/StreamInfo')):
58             media_location = stream_info.find(ns('./MediaLocation'))
59             if media_location is None:
60                 continue
61
62             server = media_location.find(ns('./Server')).text
63             app = media_location.find(ns('./App')).text
64             media_id = stream_info.find(ns('./Id')).text
65             name = stream_info.find(ns('./Name')).text
66             width = int(stream_info.find(ns('./Width')).text)
67             height = int(stream_info.find(ns('./Height')).text)
68
69             formats.append({
70                 'url': 'rtmp://%s/%s' % (server, app),
71                 'app': app,
72                 'play_path': '01/%s' % video_guid.upper(),
73                 'rtmp_conn': ['S:%s' % session_id, 'S:%s' % media_id, 'S:n2'],
74                 'page_url': url,
75                 'ext': 'flv',
76                 'format_id': 'rtmp' + ('-%s' % name if name else ''),
77                 'width': width,
78                 'height': height,
79             })
80
81         sources_data = self._download_json(
82             'http://www.newstube.ru/player2/getsources?guid=%s' % video_guid,
83             video_guid, fatal=False)
84         if sources_data:
85             for source in sources_data.get('Sources', []):
86                 source_url = source.get('Src')
87                 if not source_url:
88                     continue
89                 height = int_or_none(source.get('Height'))
90                 f = {
91                     'format_id': 'http' + ('-%dp' % height if height else ''),
92                     'url': source_url,
93                     'width': int_or_none(source.get('Width')),
94                     'height': height,
95                 }
96                 source_type = source.get('Type')
97                 if source_type:
98                     mobj = re.search(r'codecs="([^,]+),\s*([^"]+)"', source_type)
99                     if mobj:
100                         vcodec, acodec = mobj.groups()
101                         f.update({
102                             'vcodec': vcodec,
103                             'acodec': acodec,
104                         })
105                 formats.append(f)
106
107         self._check_formats(formats, video_guid)
108         self._sort_formats(formats)
109
110         return {
111             'id': video_guid,
112             'title': title,
113             'description': description,
114             'thumbnail': thumbnail,
115             'duration': duration,
116             'formats': formats,
117         }