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