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