[ie/wistia] Support password-protected videos (#11100)
[yt-dlp3.git] / yt_dlp / extractor / box.py
blobf06339f70162a22cd3ee5f8ad51880c94a8db21c
1 import json
2 import urllib.parse
4 from .common import InfoExtractor
5 from ..utils import (
6 ExtractorError,
7 parse_iso8601,
8 update_url_query,
9 url_or_none,
11 from ..utils.traversal import traverse_obj
14 class BoxIE(InfoExtractor):
15 _VALID_URL = r'https?://(?:[^.]+\.)?(?P<service>app|ent)\.box\.com/s/(?P<shared_name>[^/?#]+)(?:/file/(?P<id>\d+))?'
16 _TESTS = [{
17 'url': 'https://mlssoccer.app.box.com/s/0evd2o3e08l60lr4ygukepvnkord1o1x/file/510727257538',
18 'md5': '1f81b2fd3960f38a40a3b8823e5fcd43',
19 'info_dict': {
20 'id': '510727257538',
21 'ext': 'mp4',
22 'title': 'Garber St. Louis will be 28th MLS team +scarving.mp4',
23 'uploader': '',
24 'timestamp': 1566320259,
25 'upload_date': '20190820',
26 'uploader_id': '235196876',
28 'params': {'skip_download': 'dash fragment too small'},
29 }, {
30 'url': 'https://utexas.app.box.com/s/2x6vanv85fdl8j2eqlcxmv0gp1wvps6e',
31 'info_dict': {
32 'id': '787379022466',
33 'ext': 'mp4',
34 'title': 'Webinar recording: Take the Leap!.mp4',
35 'uploader': 'Patricia Mosele',
36 'timestamp': 1615824864,
37 'upload_date': '20210315',
38 'uploader_id': '239068974',
40 'params': {'skip_download': 'dash fragment too small'},
41 }, {
42 'url': 'https://thejacksonlaboratory.ent.box.com/s/2x09dm6vcg6y28o0oox1so4l0t8wzt6l/file/1536173056065',
43 'info_dict': {
44 'id': '1536173056065',
45 'ext': 'mp4',
46 'uploader_id': '18523128264',
47 'uploader': 'Lexi Hennigan',
48 'title': 'iPSC Symposium recording part 1.mp4',
49 'timestamp': 1716228343,
50 'upload_date': '20240520',
52 'params': {'skip_download': 'dash fragment too small'},
55 def _real_extract(self, url):
56 shared_name, file_id, service = self._match_valid_url(url).group('shared_name', 'id', 'service')
57 webpage = self._download_webpage(url, file_id or shared_name)
59 if not file_id:
60 post_stream_data = self._search_json(
61 r'Box\.postStreamData\s*=', webpage, 'Box post-stream data', shared_name)
62 shared_item = traverse_obj(
63 post_stream_data, ('/app-api/enduserapp/shared-item', {dict})) or {}
64 if shared_item.get('itemType') != 'file':
65 raise ExtractorError('The requested resource is not a file', expected=True)
67 file_id = str(shared_item['itemID'])
69 request_token = self._search_json(
70 r'Box\.config\s*=', webpage, 'Box config', file_id)['requestToken']
71 access_token = self._download_json(
72 f'https://{service}.box.com/app-api/enduserapp/elements/tokens', file_id,
73 'Downloading token JSON metadata',
74 data=json.dumps({'fileIDs': [file_id]}).encode(), headers={
75 'Content-Type': 'application/json',
76 'X-Request-Token': request_token,
77 'X-Box-EndUser-API': 'sharedName=' + shared_name,
78 })[file_id]['read']
79 shared_link = f'https://{service}.box.com/s/{shared_name}'
80 f = self._download_json(
81 'https://api.box.com/2.0/files/' + file_id, file_id,
82 'Downloading file JSON metadata', headers={
83 'Authorization': 'Bearer ' + access_token,
84 'BoxApi': 'shared_link=' + shared_link,
85 'X-Rep-Hints': '[dash]', # TODO: extract `hls` formats
86 }, query={
87 'fields': 'authenticated_download_url,created_at,created_by,description,extension,is_download_available,name,representations,size',
89 title = f['name']
91 query = {
92 'access_token': access_token,
93 'shared_link': shared_link,
96 formats = []
98 for url_tmpl in traverse_obj(f, (
99 'representations', 'entries', lambda _, v: v['representation'] == 'dash',
100 'content', 'url_template', {url_or_none},
102 manifest_url = update_url_query(url_tmpl.replace('{+asset_path}', 'manifest.mpd'), query)
103 fmts = self._extract_mpd_formats(manifest_url, file_id)
104 for fmt in fmts:
105 fmt['extra_param_to_segment_url'] = urllib.parse.urlparse(manifest_url).query
106 formats.extend(fmts)
108 creator = f.get('created_by') or {}
110 return {
111 'id': file_id,
112 'title': title,
113 'formats': formats,
114 'description': f.get('description') or None,
115 'uploader': creator.get('name'),
116 'timestamp': parse_iso8601(f.get('created_at')),
117 'uploader_id': creator.get('id'),