tvigle.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. ExtractorError,
  4. float_or_none,
  5. int_or_none,
  6. parse_age_limit,
  7. try_get,
  8. url_or_none,
  9. )
  10. class TvigleIE(InfoExtractor):
  11. IE_NAME = 'tvigle'
  12. IE_DESC = 'Интернет-телевидение Tvigle.ru'
  13. _VALID_URL = r'https?://(?:www\.)?(?:tvigle\.ru/(?:[^/]+/)+(?P<display_id>[^/]+)/$|cloud\.tvigle\.ru/video/(?P<id>\d+))'
  14. _EMBED_REGEX = [r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//cloud\.tvigle\.ru/video/.+?)\1']
  15. _GEO_BYPASS = False
  16. _GEO_COUNTRIES = ['RU']
  17. _TESTS = [
  18. {
  19. 'url': 'http://www.tvigle.ru/video/sokrat/',
  20. 'info_dict': {
  21. 'id': '1848932',
  22. 'display_id': 'sokrat',
  23. 'ext': 'mp4',
  24. 'title': 'Сократ',
  25. 'description': 'md5:d6b92ffb7217b4b8ebad2e7665253c17',
  26. 'duration': 6586,
  27. 'age_limit': 12,
  28. },
  29. 'skip': 'georestricted',
  30. },
  31. {
  32. 'url': 'http://www.tvigle.ru/video/vladimir-vysotskii/vedushchii-teleprogrammy-60-minut-ssha-o-vladimire-vysotskom/',
  33. 'info_dict': {
  34. 'id': '5142516',
  35. 'ext': 'flv',
  36. 'title': 'Ведущий телепрограммы «60 минут» (США) о Владимире Высоцком',
  37. 'description': 'md5:027f7dc872948f14c96d19b4178428a4',
  38. 'duration': 186.080,
  39. 'age_limit': 0,
  40. },
  41. 'skip': 'georestricted',
  42. }, {
  43. 'url': 'https://cloud.tvigle.ru/video/5267604/',
  44. 'only_matching': True,
  45. },
  46. ]
  47. def _real_extract(self, url):
  48. mobj = self._match_valid_url(url)
  49. video_id = mobj.group('id')
  50. display_id = mobj.group('display_id')
  51. if not video_id:
  52. webpage = self._download_webpage(url, display_id)
  53. video_id = self._html_search_regex(
  54. (r'<div[^>]+class=["\']player["\'][^>]+id=["\'](\d+)',
  55. r'cloudId\s*=\s*["\'](\d+)',
  56. r'class="video-preview current_playing" id="(\d+)"'),
  57. webpage, 'video id')
  58. video_data = self._download_json(
  59. f'http://cloud.tvigle.ru/api/play/video/{video_id}/', display_id)
  60. item = video_data['playlist']['items'][0]
  61. videos = item.get('videos')
  62. error_message = item.get('errorMessage')
  63. if not videos and error_message:
  64. if item.get('isGeoBlocked') is True:
  65. self.raise_geo_restricted(
  66. msg=error_message, countries=self._GEO_COUNTRIES)
  67. else:
  68. raise ExtractorError(
  69. f'{self.IE_NAME} returned error: {error_message}',
  70. expected=True)
  71. title = item['title']
  72. description = item.get('description')
  73. thumbnail = item.get('thumbnail')
  74. duration = float_or_none(item.get('durationMilliseconds'), 1000)
  75. age_limit = parse_age_limit(item.get('ageRestrictions'))
  76. formats = []
  77. for vcodec, url_or_fmts in item['videos'].items():
  78. if vcodec == 'hls':
  79. m3u8_url = url_or_none(url_or_fmts)
  80. if not m3u8_url:
  81. continue
  82. formats.extend(self._extract_m3u8_formats(
  83. m3u8_url, video_id, ext='mp4', entry_protocol='m3u8_native',
  84. m3u8_id='hls', fatal=False))
  85. elif vcodec == 'dash':
  86. mpd_url = url_or_none(url_or_fmts)
  87. if not mpd_url:
  88. continue
  89. formats.extend(self._extract_mpd_formats(
  90. mpd_url, video_id, mpd_id='dash', fatal=False))
  91. else:
  92. if not isinstance(url_or_fmts, dict):
  93. continue
  94. for format_id, video_url in url_or_fmts.items():
  95. if format_id == 'm3u8':
  96. continue
  97. video_url = url_or_none(video_url)
  98. if not video_url:
  99. continue
  100. height = self._search_regex(
  101. r'^(\d+)[pP]$', format_id, 'height', default=None)
  102. filesize = int_or_none(try_get(
  103. item, lambda x: x['video_files_size'][vcodec][format_id]))
  104. formats.append({
  105. 'url': video_url,
  106. 'format_id': f'{vcodec}-{format_id}',
  107. 'vcodec': vcodec,
  108. 'height': int_or_none(height),
  109. 'filesize': filesize,
  110. })
  111. return {
  112. 'id': video_id,
  113. 'display_id': display_id,
  114. 'title': title,
  115. 'description': description,
  116. 'thumbnail': thumbnail,
  117. 'duration': duration,
  118. 'age_limit': age_limit,
  119. 'formats': formats,
  120. }