logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

firsttv.py (6457B)


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