firsttv.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import urllib.parse
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. int_or_none,
  5. qualities,
  6. unified_strdate,
  7. url_or_none,
  8. )
  9. class FirstTVIE(InfoExtractor):
  10. IE_NAME = '1tv'
  11. IE_DESC = 'Первый канал'
  12. _VALID_URL = r'https?://(?:www\.)?1tv\.ru/(?:[^/]+/)+(?P<id>[^/?#]+)'
  13. _TESTS = [{
  14. # single format
  15. 'url': 'http://www.1tv.ru/shows/naedine-so-vsemi/vypuski/gost-lyudmila-senchina-naedine-so-vsemi-vypusk-ot-12-02-2015',
  16. 'md5': 'a1b6b60d530ebcf8daacf4565762bbaf',
  17. 'info_dict': {
  18. 'id': '40049',
  19. 'ext': 'mp4',
  20. 'title': 'Гость Людмила Сенчина. Наедине со всеми. Выпуск от 12.02.2015',
  21. 'thumbnail': r're:^https?://.*\.(?:jpg|JPG)$',
  22. 'upload_date': '20150212',
  23. 'duration': 2694,
  24. },
  25. }, {
  26. # multiple formats
  27. 'url': 'http://www.1tv.ru/shows/dobroe-utro/pro-zdorove/vesennyaya-allergiya-dobroe-utro-fragment-vypuska-ot-07042016',
  28. 'info_dict': {
  29. 'id': '364746',
  30. 'ext': 'mp4',
  31. 'title': 'Весенняя аллергия. Доброе утро. Фрагмент выпуска от 07.04.2016',
  32. 'thumbnail': r're:^https?://.*\.(?:jpg|JPG)$',
  33. 'upload_date': '20160407',
  34. 'duration': 179,
  35. 'formats': 'mincount:3',
  36. },
  37. 'params': {
  38. 'skip_download': True,
  39. },
  40. }, {
  41. 'url': 'http://www.1tv.ru/news/issue/2016-12-01/14:00',
  42. 'info_dict': {
  43. 'id': '14:00',
  44. 'title': 'Выпуск новостей в 14:00 1 декабря 2016 года. Новости. Первый канал',
  45. 'description': 'md5:2e921b948f8c1ff93901da78ebdb1dfd',
  46. },
  47. 'playlist_count': 13,
  48. }, {
  49. 'url': 'http://www.1tv.ru/shows/tochvtoch-supersezon/vystupleniya/evgeniy-dyatlov-vladimir-vysockiy-koni-priveredlivye-toch-v-toch-supersezon-fragment-vypuska-ot-06-11-2016',
  50. 'only_matching': True,
  51. }]
  52. def _real_extract(self, url):
  53. display_id = self._match_id(url)
  54. webpage = self._download_webpage(url, display_id)
  55. playlist_url = urllib.parse.urljoin(url, self._search_regex(
  56. r'data-playlist-url=(["\'])(?P<url>(?:(?!\1).)+)\1',
  57. webpage, 'playlist url', group='url'))
  58. parsed_url = urllib.parse.urlparse(playlist_url)
  59. qs = urllib.parse.parse_qs(parsed_url.query)
  60. item_ids = qs.get('videos_ids[]') or qs.get('news_ids[]')
  61. items = self._download_json(playlist_url, display_id)
  62. if item_ids:
  63. items = [
  64. item for item in items
  65. if item.get('uid') and str(item['uid']) in item_ids]
  66. else:
  67. items = [items[0]]
  68. entries = []
  69. QUALITIES = ('ld', 'sd', 'hd')
  70. for item in items:
  71. title = item['title']
  72. quality = qualities(QUALITIES)
  73. formats = []
  74. path = None
  75. for f in item.get('mbr', []):
  76. src = url_or_none(f.get('src'))
  77. if not src:
  78. continue
  79. tbr = int_or_none(self._search_regex(
  80. r'_(\d{3,})\.mp4', src, 'tbr', default=None))
  81. if not path:
  82. path = self._search_regex(
  83. r'//[^/]+/(.+?)_\d+\.mp4', src,
  84. 'm3u8 path', default=None)
  85. formats.append({
  86. 'url': src,
  87. 'format_id': f.get('name'),
  88. 'tbr': tbr,
  89. 'source_preference': quality(f.get('name')),
  90. # quality metadata of http formats may be incorrect
  91. 'preference': -10,
  92. })
  93. # m3u8 URL format is reverse engineered from [1] (search for
  94. # master.m3u8). dashEdges (that is currently balancer-vod.1tv.ru)
  95. # is taken from [2].
  96. # 1. http://static.1tv.ru/player/eump1tv-current/eump-1tv.all.min.js?rnd=9097422834:formatted
  97. # 2. http://static.1tv.ru/player/eump1tv-config/config-main.js?rnd=9097422834
  98. if not path and len(formats) == 1:
  99. path = self._search_regex(
  100. r'//[^/]+/(.+?$)', formats[0]['url'],
  101. 'm3u8 path', default=None)
  102. if path:
  103. if len(formats) == 1:
  104. m3u8_path = ','
  105. else:
  106. tbrs = [str(t) for t in sorted(f['tbr'] for f in formats)]
  107. m3u8_path = '_,{},{}'.format(','.join(tbrs), '.mp4')
  108. formats.extend(self._extract_m3u8_formats(
  109. f'http://balancer-vod.1tv.ru/{path}{m3u8_path}.urlset/master.m3u8',
  110. display_id, 'mp4',
  111. entry_protocol='m3u8_native', m3u8_id='hls', fatal=False))
  112. thumbnail = item.get('poster') or self._og_search_thumbnail(webpage)
  113. duration = int_or_none(item.get('duration') or self._html_search_meta(
  114. 'video:duration', webpage, 'video duration', fatal=False))
  115. upload_date = unified_strdate(self._html_search_meta(
  116. 'ya:ovs:upload_date', webpage, 'upload date', default=None))
  117. entries.append({
  118. 'id': str(item.get('id') or item['uid']),
  119. 'thumbnail': thumbnail,
  120. 'title': title,
  121. 'upload_date': upload_date,
  122. 'duration': int_or_none(duration),
  123. 'formats': formats,
  124. })
  125. title = self._html_search_regex(
  126. (r'<div class="tv_translation">\s*<h1><a href="[^"]+">([^<]*)</a>',
  127. r"'title'\s*:\s*'([^']+)'"),
  128. webpage, 'title', default=None) or self._og_search_title(
  129. webpage, default=None)
  130. description = self._html_search_regex(
  131. r'<div class="descr">\s*<div>&nbsp;</div>\s*<p>([^<]*)</p></div>',
  132. webpage, 'description', default=None) or self._html_search_meta(
  133. 'description', webpage, 'description', default=None)
  134. return self.playlist_result(entries, display_id, title, description)