vbox7.py 6.6 KB

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