You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.9 KiB

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