logo

youtube-dl

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

shared.py (4385B)


  1. from __future__ import unicode_literals
  2. from .common import InfoExtractor
  3. from ..compat import (
  4. compat_b64decode,
  5. compat_urllib_parse_unquote_plus,
  6. )
  7. from ..utils import (
  8. determine_ext,
  9. ExtractorError,
  10. int_or_none,
  11. js_to_json,
  12. KNOWN_EXTENSIONS,
  13. parse_filesize,
  14. rot47,
  15. url_or_none,
  16. urlencode_postdata,
  17. )
  18. class SharedBaseIE(InfoExtractor):
  19. def _real_extract(self, url):
  20. video_id = self._match_id(url)
  21. webpage, urlh = self._download_webpage_handle(url, video_id)
  22. if self._FILE_NOT_FOUND in webpage:
  23. raise ExtractorError(
  24. 'Video %s does not exist' % video_id, expected=True)
  25. video_url = self._extract_video_url(webpage, video_id, url)
  26. title = self._extract_title(webpage)
  27. filesize = int_or_none(self._extract_filesize(webpage))
  28. return {
  29. 'id': video_id,
  30. 'url': video_url,
  31. 'ext': 'mp4',
  32. 'filesize': filesize,
  33. 'title': title,
  34. }
  35. def _extract_title(self, webpage):
  36. return compat_b64decode(self._html_search_meta(
  37. 'full:title', webpage, 'title')).decode('utf-8')
  38. def _extract_filesize(self, webpage):
  39. return self._html_search_meta(
  40. 'full:size', webpage, 'file size', fatal=False)
  41. class SharedIE(SharedBaseIE):
  42. IE_DESC = 'shared.sx'
  43. _VALID_URL = r'https?://shared\.sx/(?P<id>[\da-z]{10})'
  44. _FILE_NOT_FOUND = '>File does not exist<'
  45. _TEST = {
  46. 'url': 'http://shared.sx/0060718775',
  47. 'md5': '106fefed92a8a2adb8c98e6a0652f49b',
  48. 'info_dict': {
  49. 'id': '0060718775',
  50. 'ext': 'mp4',
  51. 'title': 'Bmp4',
  52. 'filesize': 1720110,
  53. },
  54. }
  55. def _extract_video_url(self, webpage, video_id, url):
  56. download_form = self._hidden_inputs(webpage)
  57. video_page = self._download_webpage(
  58. url, video_id, 'Downloading video page',
  59. data=urlencode_postdata(download_form),
  60. headers={
  61. 'Content-Type': 'application/x-www-form-urlencoded',
  62. 'Referer': url,
  63. })
  64. video_url = self._html_search_regex(
  65. r'data-url=(["\'])(?P<url>(?:(?!\1).)+)\1',
  66. video_page, 'video URL', group='url')
  67. return video_url
  68. class VivoIE(SharedBaseIE):
  69. IE_DESC = 'vivo.sx'
  70. _VALID_URL = r'https?://vivo\.s[xt]/(?P<id>[\da-z]{10})'
  71. _FILE_NOT_FOUND = '>The file you have requested does not exists or has been removed'
  72. _TESTS = [{
  73. 'url': 'http://vivo.sx/d7ddda0e78',
  74. 'md5': '15b3af41be0b4fe01f4df075c2678b2c',
  75. 'info_dict': {
  76. 'id': 'd7ddda0e78',
  77. 'ext': 'mp4',
  78. 'title': 'Chicken',
  79. 'filesize': 515659,
  80. },
  81. }, {
  82. 'url': 'http://vivo.st/d7ddda0e78',
  83. 'only_matching': True,
  84. }]
  85. def _extract_title(self, webpage):
  86. title = self._html_search_regex(
  87. r'data-name\s*=\s*(["\'])(?P<title>(?:(?!\1).)+)\1', webpage,
  88. 'title', default=None, group='title')
  89. if title:
  90. ext = determine_ext(title)
  91. if ext.lower() in KNOWN_EXTENSIONS:
  92. title = title.rpartition('.' + ext)[0]
  93. return title
  94. return self._og_search_title(webpage)
  95. def _extract_filesize(self, webpage):
  96. return parse_filesize(self._search_regex(
  97. r'data-type=["\']video["\'][^>]*>Watch.*?<strong>\s*\((.+?)\)',
  98. webpage, 'filesize', fatal=False))
  99. def _extract_video_url(self, webpage, video_id, url):
  100. def decode_url_old(encoded_url):
  101. return compat_b64decode(encoded_url).decode('utf-8')
  102. stream_url = self._search_regex(
  103. r'data-stream\s*=\s*(["\'])(?P<url>(?:(?!\1).)+)\1', webpage,
  104. 'stream url', default=None, group='url')
  105. if stream_url:
  106. stream_url = url_or_none(decode_url_old(stream_url))
  107. if stream_url:
  108. return stream_url
  109. def decode_url(encoded_url):
  110. return rot47(compat_urllib_parse_unquote_plus(encoded_url))
  111. return decode_url(self._parse_json(
  112. self._search_regex(
  113. r'(?s)InitializeStream\s*\(\s*({.+?})\s*\)\s*;', webpage,
  114. 'stream'),
  115. video_id, transform_source=js_to_json)['source'])