ntv.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. RegexNotFoundError,
  7. unescapeHTML
  8. )
  9. class NTVIE(InfoExtractor):
  10. _VALID_URL = r'http://(?:www\.)?ntv\.ru/(?P<id>.+)'
  11. _TESTS = [
  12. {
  13. 'url': 'http://www.ntv.ru/novosti/863142/',
  14. 'info_dict': {
  15. 'id': '746000',
  16. 'ext': 'flv',
  17. 'title': 'Командующий Черноморским флотом провел переговоры в штабе ВМС Украины',
  18. 'description': 'Командующий Черноморским флотом провел переговоры в штабе ВМС Украины',
  19. 'duration': 136,
  20. },
  21. 'params': {
  22. # rtmp download
  23. 'skip_download': True,
  24. },
  25. },
  26. {
  27. 'url': 'http://www.ntv.ru/video/novosti/750370/',
  28. 'info_dict': {
  29. 'id': '750370',
  30. 'ext': 'flv',
  31. 'title': 'Родные пассажиров пропавшего Boeing не верят в трагический исход',
  32. 'description': 'Родные пассажиров пропавшего Boeing не верят в трагический исход',
  33. 'duration': 172,
  34. },
  35. 'params': {
  36. # rtmp download
  37. 'skip_download': True,
  38. },
  39. },
  40. {
  41. 'url': 'http://www.ntv.ru/peredacha/segodnya/m23700/o232416',
  42. 'info_dict': {
  43. 'id': '747480',
  44. 'ext': 'flv',
  45. 'title': '«Сегодня». 21 марта 2014 года. 16:00 ',
  46. 'description': '«Сегодня». 21 марта 2014 года. 16:00 ',
  47. 'duration': 1496,
  48. },
  49. 'params': {
  50. # rtmp download
  51. 'skip_download': True,
  52. },
  53. },
  54. {
  55. 'url': 'http://www.ntv.ru/kino/Koma_film',
  56. 'info_dict': {
  57. 'id': '750783',
  58. 'ext': 'flv',
  59. 'title': 'Остросюжетный фильм «Кома» — 4 апреля вечером на НТВ',
  60. 'description': 'Остросюжетный фильм «Кома» — 4 апреля вечером на НТВ',
  61. 'duration': 28,
  62. },
  63. 'params': {
  64. # rtmp download
  65. 'skip_download': True,
  66. },
  67. },
  68. {
  69. 'url': 'http://www.ntv.ru/serial/Delo_vrachey/m31760/o233916/',
  70. 'info_dict': {
  71. 'id': '751482',
  72. 'ext': 'flv',
  73. 'title': '«Дело врачей»: «Деревце жизни»',
  74. 'description': '«Дело врачей»: «Деревце жизни»',
  75. 'duration': 2590,
  76. },
  77. 'params': {
  78. # rtmp download
  79. 'skip_download': True,
  80. },
  81. },
  82. ]
  83. _VIDEO_ID_REGEXES = [
  84. r'<meta property="og:url" content="http://www\.ntv\.ru/video/(\d+)',
  85. r'<video embed=[^>]+><id>(\d+)</id>',
  86. r'<video restriction[^>]+><key>(\d+)</key>'
  87. ]
  88. def _real_extract(self, url):
  89. mobj = re.match(self._VALID_URL, url)
  90. video_id = mobj.group('id')
  91. page = self._download_webpage(url, video_id, 'Downloading page')
  92. def extract(patterns, name, page, fatal=False):
  93. for pattern in patterns:
  94. mobj = re.search(pattern, page)
  95. if mobj:
  96. return mobj.group(1)
  97. if fatal:
  98. raise RegexNotFoundError(u'Unable to extract %s' % name)
  99. return None
  100. video_id = extract(self._VIDEO_ID_REGEXES, 'video id', page, fatal=True)
  101. player = self._download_xml('http://www.ntv.ru/vi%s/' % video_id, video_id, 'Downloading video XML')
  102. title = unescapeHTML(player.find('./data/title').text)
  103. description = unescapeHTML(player.find('./data/description').text)
  104. video = player.find('./data/video')
  105. video_id = video.find('./id').text
  106. thumbnail = video.find('./splash').text
  107. duration = int(video.find('./totaltime').text)
  108. view_count = int(video.find('./views').text)
  109. puid22 = video.find('./puid22').text
  110. apps = {
  111. '4': 'video1',
  112. '7': 'video2',
  113. }
  114. app = apps[puid22] if puid22 in apps else apps['4']
  115. formats = []
  116. for format_id in ['', 'hi', 'webm']:
  117. file = video.find('./%sfile' % format_id)
  118. if file is None:
  119. continue
  120. size = video.find('./%ssize' % format_id)
  121. formats.append({
  122. 'url': 'rtmp://media.ntv.ru/%s' % app,
  123. 'app': app,
  124. 'play_path': file.text,
  125. 'rtmp_conn': 'B:1',
  126. 'player_url': 'http://www.ntv.ru/swf/vps1.swf?update=20131128',
  127. 'page_url': 'http://www.ntv.ru',
  128. 'flash_ver': 'LNX 11,2,202,341',
  129. 'rtmp_live': True,
  130. 'ext': 'flv',
  131. 'filesize': int(size.text),
  132. })
  133. self._sort_formats(formats)
  134. return {
  135. 'id': video_id,
  136. 'title': title,
  137. 'description': description,
  138. 'thumbnail': thumbnail,
  139. 'duration': duration,
  140. 'view_count': view_count,
  141. 'formats': formats,
  142. }