firsttv.py 6.2 KB

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