logo

youtube-dl

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

vbox7.py (8014B)


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