rutv.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import ExtractorError, int_or_none, str_to_int
  4. class RUTVIE(InfoExtractor):
  5. IE_DESC = 'RUTV.RU'
  6. _VALID_URL = r'''(?x)
  7. https?://
  8. (?:test)?player\.(?:rutv\.ru|vgtrk\.com)/
  9. (?P<path>
  10. flash\d+v/container\.swf\?id=|
  11. iframe/(?P<type>swf|video|live)/id/|
  12. index/iframe/cast_id/
  13. )
  14. (?P<id>\d+)
  15. '''
  16. _EMBED_URLS = [
  17. r'<iframe[^>]+?src=(["\'])(?P<url>https?://(?:test)?player\.(?:rutv\.ru|vgtrk\.com)/(?:iframe/(?:swf|video|live)/id|index/iframe/cast_id)/.+?)\1',
  18. r'<meta[^>]+?property=(["\'])og:video\1[^>]+?content=(["\'])(?P<url>https?://(?:test)?player\.(?:rutv\.ru|vgtrk\.com)/flash\d+v/container\.swf\?id=.+?\2)',
  19. ]
  20. _TESTS = [
  21. {
  22. 'url': 'http://player.rutv.ru/flash2v/container.swf?id=774471&sid=kultura&fbv=true&isPlay=true&ssl=false&i=560&acc_video_id=episode_id/972347/video_id/978186/brand_id/31724',
  23. 'info_dict': {
  24. 'id': '774471',
  25. 'ext': 'mp4',
  26. 'title': 'Монологи на все времена',
  27. 'description': 'md5:18d8b5e6a41fb1faa53819471852d5d5',
  28. 'duration': 2906,
  29. },
  30. 'params': {
  31. # m3u8 download
  32. 'skip_download': True,
  33. },
  34. },
  35. {
  36. 'url': 'https://player.vgtrk.com/flash2v/container.swf?id=774016&sid=russiatv&fbv=true&isPlay=true&ssl=false&i=560&acc_video_id=episode_id/972098/video_id/977760/brand_id/57638',
  37. 'info_dict': {
  38. 'id': '774016',
  39. 'ext': 'mp4',
  40. 'title': 'Чужой в семье Сталина',
  41. 'description': '',
  42. 'duration': 2539,
  43. },
  44. 'params': {
  45. # m3u8 download
  46. 'skip_download': True,
  47. },
  48. },
  49. {
  50. 'url': 'http://player.rutv.ru/iframe/swf/id/766888/sid/hitech/?acc_video_id=4000',
  51. 'info_dict': {
  52. 'id': '766888',
  53. 'ext': 'mp4',
  54. 'title': 'Вести.net: интернет-гиганты начали перетягивание программных "одеял"',
  55. 'description': 'md5:65ddd47f9830c4f42ed6475f8730c995',
  56. 'duration': 279,
  57. },
  58. 'params': {
  59. # m3u8 download
  60. 'skip_download': True,
  61. },
  62. },
  63. {
  64. 'url': 'http://player.rutv.ru/iframe/video/id/771852/start_zoom/true/showZoomBtn/false/sid/russiatv/?acc_video_id=episode_id/970443/video_id/975648/brand_id/5169',
  65. 'info_dict': {
  66. 'id': '771852',
  67. 'ext': 'mp4',
  68. 'title': 'Прямой эфир. Жертвы загадочной болезни: смерть от старости в 17 лет',
  69. 'description': 'md5:b81c8c55247a4bd996b43ce17395b2d8',
  70. 'duration': 3096,
  71. },
  72. 'params': {
  73. # m3u8 download
  74. 'skip_download': True,
  75. },
  76. },
  77. {
  78. 'url': 'http://player.rutv.ru/iframe/live/id/51499/showZoomBtn/false/isPlay/true/sid/sochi2014',
  79. 'info_dict': {
  80. 'id': '51499',
  81. 'ext': 'flv',
  82. 'title': 'Сочи-2014. Биатлон. Индивидуальная гонка. Мужчины ',
  83. 'description': 'md5:9e0ed5c9d2fa1efbfdfed90c9a6d179c',
  84. },
  85. 'skip': 'Translation has finished',
  86. },
  87. {
  88. 'url': 'http://player.rutv.ru/iframe/live/id/21/showZoomBtn/false/isPlay/true/',
  89. 'info_dict': {
  90. 'id': '21',
  91. 'ext': 'mp4',
  92. 'title': 're:^Россия 24. Прямой эфир [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  93. 'is_live': True,
  94. },
  95. 'params': {
  96. # m3u8 download
  97. 'skip_download': True,
  98. },
  99. },
  100. {
  101. 'url': 'https://testplayer.vgtrk.com/iframe/live/id/19201/showZoomBtn/false/isPlay/true/',
  102. 'only_matching': True,
  103. },
  104. ]
  105. def _real_extract(self, url):
  106. mobj = self._match_valid_url(url)
  107. video_id = mobj.group('id')
  108. video_path = mobj.group('path')
  109. if re.match(r'flash\d+v', video_path):
  110. video_type = 'video'
  111. elif video_path.startswith('iframe'):
  112. video_type = mobj.group('type')
  113. if video_type == 'swf':
  114. video_type = 'video'
  115. elif video_path.startswith('index/iframe/cast_id'):
  116. video_type = 'live'
  117. is_live = video_type == 'live'
  118. json_data = self._download_json(
  119. 'http://player.vgtrk.com/iframe/data{}/id/{}'.format('live' if is_live else 'video', video_id),
  120. video_id, 'Downloading JSON')
  121. if json_data['errors']:
  122. raise ExtractorError('{} said: {}'.format(self.IE_NAME, json_data['errors']), expected=True)
  123. playlist = json_data['data']['playlist']
  124. medialist = playlist['medialist']
  125. media = medialist[0]
  126. if media['errors']:
  127. raise ExtractorError('{} said: {}'.format(self.IE_NAME, media['errors']), expected=True)
  128. view_count = int_or_none(playlist.get('count_views'))
  129. priority_transport = playlist['priority_transport']
  130. thumbnail = media['picture']
  131. width = int_or_none(media['width'])
  132. height = int_or_none(media['height'])
  133. description = media['anons']
  134. title = media['title']
  135. duration = int_or_none(media.get('duration'))
  136. formats = []
  137. subtitles = {}
  138. for transport, links in media['sources'].items():
  139. for quality, url in links.items():
  140. preference = -1 if priority_transport == transport else -2
  141. if transport == 'rtmp':
  142. mobj = re.search(r'^(?P<url>rtmp://[^/]+/(?P<app>.+))/(?P<playpath>.+)$', url)
  143. if not mobj:
  144. continue
  145. fmt = {
  146. 'url': mobj.group('url'),
  147. 'play_path': mobj.group('playpath'),
  148. 'app': mobj.group('app'),
  149. 'page_url': 'http://player.rutv.ru',
  150. 'player_url': 'http://player.rutv.ru/flash3v/osmf.swf?i=22',
  151. 'rtmp_live': True,
  152. 'ext': 'flv',
  153. 'vbr': str_to_int(quality),
  154. }
  155. elif transport == 'm3u8':
  156. fmt, subs = self._extract_m3u8_formats_and_subtitles(
  157. url, video_id, 'mp4', quality=preference, m3u8_id='hls')
  158. formats.extend(fmt)
  159. self._merge_subtitles(subs, target=subtitles)
  160. continue
  161. else:
  162. fmt = {
  163. 'url': url,
  164. }
  165. fmt.update({
  166. 'width': int_or_none(quality, default=height, invscale=width, scale=height),
  167. 'height': int_or_none(quality, default=height),
  168. 'format_id': f'{transport}-{quality}',
  169. 'source_preference': preference,
  170. })
  171. formats.append(fmt)
  172. return {
  173. 'id': video_id,
  174. 'title': title,
  175. 'description': description,
  176. 'thumbnail': thumbnail,
  177. 'view_count': view_count,
  178. 'duration': duration,
  179. 'formats': formats,
  180. 'subtitles': subtitles,
  181. 'is_live': is_live,
  182. '_format_sort_fields': ('source', ),
  183. }