You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
4.5 KiB

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