vbox7.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import time
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. determine_ext,
  8. ExtractorError,
  9. float_or_none,
  10. merge_dicts,
  11. T,
  12. traverse_obj,
  13. txt_or_none,
  14. url_or_none,
  15. )
  16. class Vbox7IE(InfoExtractor):
  17. _VALID_URL = r'''(?x)
  18. https?://
  19. (?:[^/]+\.)?vbox7\.com/
  20. (?:
  21. play:|
  22. (?:
  23. emb/external\.php|
  24. player/ext\.swf
  25. )\?.*?\bvid=
  26. )
  27. (?P<id>[\da-fA-F]+)
  28. '''
  29. _EMBED_REGEX = [r'<iframe[^>]+src=(?P<q>["\'])(?P<url>(?:https?:)?//vbox7\.com/emb/external\.php.+?)(?P=q)']
  30. _GEO_COUNTRIES = ['BG']
  31. _GEO_BYPASS = False
  32. _TESTS = [{
  33. 'url': 'https://vbox7.com/play:0946fff23c',
  34. 'md5': '50ca1f78345a9c15391af47d8062d074',
  35. 'info_dict': {
  36. 'id': '0946fff23c',
  37. 'ext': 'mp4',
  38. 'title': 'Борисов: Притеснен съм за бъдещето на България',
  39. 'description': 'По думите му е опасно страната ни да бъде обявена за "сигурна"',
  40. 'thumbnail': r're:^https?://.*\.jpg$',
  41. 'timestamp': 1470982814,
  42. 'upload_date': '20160812',
  43. 'uploader': 'zdraveibulgaria',
  44. },
  45. 'expected_warnings': [
  46. 'Unable to download webpage',
  47. ],
  48. }, {
  49. 'url': 'http://vbox7.com/play:249bb972c2',
  50. 'md5': 'aaf19465e37ec0b30b918df83ec32c50',
  51. 'info_dict': {
  52. 'id': '249bb972c2',
  53. 'ext': 'mp4',
  54. 'title': 'Смях! Чудо - чист за секунди - Скрита камера',
  55. 'description': 'Смях! Чудо - чист за секунди - Скрита камера',
  56. 'timestamp': 1360215023,
  57. 'upload_date': '20130207',
  58. 'uploader': 'svideteliat_ot_varshava',
  59. },
  60. }, {
  61. 'url': 'http://vbox7.com/emb/external.php?vid=a240d20f9c&autoplay=1',
  62. 'only_matching': True,
  63. }, {
  64. 'url': 'http://i49.vbox7.com/player/ext.swf?vid=0946fff23c&autoplay=1',
  65. 'only_matching': True,
  66. }]
  67. @classmethod
  68. def _extract_url(cls, webpage):
  69. mobj = re.search(cls._EMBED_REGEX[0], webpage)
  70. if mobj:
  71. return mobj.group('url')
  72. def _real_extract(self, url):
  73. video_id = self._match_id(url)
  74. url = 'https://vbox7.com/play:%s' % (video_id,)
  75. now = time.time()
  76. response = self._download_json(
  77. 'https://www.vbox7.com/aj/player/item/options?vid=%s' % (video_id,),
  78. video_id, headers={'Referer': url})
  79. # estimate time to which possible `ago` member is relative
  80. now = now + 0.5 * (time.time() - now)
  81. if 'error' in response:
  82. raise ExtractorError(
  83. '%s said: %s' % (self.IE_NAME, response['error']), expected=True)
  84. video_url = traverse_obj(response, ('options', 'src', T(url_or_none)))
  85. if '/na.mp4' in video_url or '':
  86. self.raise_geo_restricted(countries=self._GEO_COUNTRIES)
  87. ext = determine_ext(video_url)
  88. if ext == 'mpd':
  89. # In case MPD cannot be parsed, or anyway, get mp4 combined
  90. # formats usually provided to Safari, iOS, and old Windows
  91. try:
  92. formats, subtitles = self._extract_mpd_formats_and_subtitles(
  93. video_url, video_id, 'dash', fatal=False)
  94. except KeyError:
  95. self.report_warning('Failed to parse MPD manifest')
  96. formats, subtitles = [], {}
  97. video = response['options']
  98. resolutions = (1080, 720, 480, 240, 144)
  99. highest_res = traverse_obj(video, ('highestRes', T(int))) or resolutions[0]
  100. for res in traverse_obj(video, ('resolutions', lambda _, r: int(r) > 0)) or resolutions:
  101. if res > highest_res:
  102. continue
  103. formats.append({
  104. 'url': video_url.replace('.mpd', '_%d.mp4' % res),
  105. 'format_id': '%dp' % res,
  106. 'height': res,
  107. })
  108. # if above formats are flaky, enable the line below
  109. # self._check_formats(formats, video_id)
  110. else:
  111. formats = [{
  112. 'url': video_url,
  113. }]
  114. subtitles = {}
  115. self._sort_formats(formats)
  116. webpage = self._download_webpage(url, video_id, fatal=False) or ''
  117. info = self._search_json_ld(
  118. webpage.replace('"/*@context"', '"@context"'), video_id,
  119. fatal=False) if webpage else {}
  120. if not info.get('title'):
  121. info['title'] = traverse_obj(response, (
  122. 'options', 'title', T(txt_or_none))) or self._og_search_title(webpage)
  123. def if_missing(k):
  124. return lambda x: None if k in info else x
  125. info = merge_dicts(info, {
  126. 'id': video_id,
  127. 'formats': formats,
  128. 'subtitles': subtitles or None,
  129. }, info, traverse_obj(response, ('options', {
  130. 'uploader': ('uploader', T(txt_or_none)),
  131. 'timestamp': ('ago', T(if_missing('timestamp')), T(lambda t: int(round((now - t) / 60.0)) * 60)),
  132. 'duration': ('duration', T(if_missing('duration')), T(float_or_none)),
  133. })))
  134. if 'thumbnail' not in info:
  135. info['thumbnail'] = self._proto_relative_url(
  136. info.get('thumbnail') or self._og_search_thumbnail(webpage),
  137. 'https:'),
  138. return info