]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/box.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / yt_dlp / extractor / box.py
CommitLineData
70c5802b 1import json
5a230233 2import urllib.parse
70c5802b 3
4from .common import InfoExtractor
5from ..utils import (
07f5b2f7 6 ExtractorError,
70c5802b 7 parse_iso8601,
70c5802b 8 update_url_query,
5a230233 9 url_or_none,
70c5802b 10)
5a230233 11from ..utils.traversal import traverse_obj
70c5802b 12
13
14class BoxIE(InfoExtractor):
07f5b2f7
SM
15 _VALID_URL = r'https?://(?:[^.]+\.)?app\.box\.com/s/(?P<shared_name>[^/?#]+)(?:/file/(?P<id>\d+))?'
16 _TESTS = [{
70c5802b 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',
5a230233 23 'uploader': '',
70c5802b 24 'timestamp': 1566320259,
25 'upload_date': '20190820',
26 'uploader_id': '235196876',
5a230233 27 },
28 'params': {'skip_download': 'dash fragment too small'},
07f5b2f7
SM
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',
39 },
40 'params': {'skip_download': 'dash fragment too small'},
41 }]
70c5802b 42
43 def _real_extract(self, url):
5ad28e7f 44 shared_name, file_id = self._match_valid_url(url).groups()
07f5b2f7
SM
45 webpage = self._download_webpage(url, file_id or shared_name)
46
47 if not file_id:
48 post_stream_data = self._search_json(
49 r'Box\.postStreamData\s*=', webpage, 'Box post-stream data', shared_name)
50 shared_item = traverse_obj(
51 post_stream_data, ('/app-api/enduserapp/shared-item', {dict})) or {}
52 if shared_item.get('itemType') != 'file':
53 raise ExtractorError('The requested resource is not a file', expected=True)
54
55 file_id = str(shared_item['itemID'])
56
57 request_token = self._search_json(
58 r'Box\.config\s*=', webpage, 'Box config', file_id)['requestToken']
70c5802b 59 access_token = self._download_json(
60 'https://app.box.com/app-api/enduserapp/elements/tokens', file_id,
61 'Downloading token JSON metadata',
62 data=json.dumps({'fileIDs': [file_id]}).encode(), headers={
63 'Content-Type': 'application/json',
64 'X-Request-Token': request_token,
65 'X-Box-EndUser-API': 'sharedName=' + shared_name,
66 })[file_id]['read']
67 shared_link = 'https://app.box.com/s/' + shared_name
68 f = self._download_json(
69 'https://api.box.com/2.0/files/' + file_id, file_id,
70 'Downloading file JSON metadata', headers={
71 'Authorization': 'Bearer ' + access_token,
72 'BoxApi': 'shared_link=' + shared_link,
73 'X-Rep-Hints': '[dash]', # TODO: extract `hls` formats
74 }, query={
add96eb9 75 'fields': 'authenticated_download_url,created_at,created_by,description,extension,is_download_available,name,representations,size',
70c5802b 76 })
77 title = f['name']
78
79 query = {
80 'access_token': access_token,
add96eb9 81 'shared_link': shared_link,
70c5802b 82 }
83
84 formats = []
85
5a230233 86 for url_tmpl in traverse_obj(f, (
87 'representations', 'entries', lambda _, v: v['representation'] == 'dash',
add96eb9 88 'content', 'url_template', {url_or_none},
5a230233 89 )):
90 manifest_url = update_url_query(url_tmpl.replace('{+asset_path}', 'manifest.mpd'), query)
91 fmts = self._extract_mpd_formats(manifest_url, file_id)
92 for fmt in fmts:
93 fmt['extra_param_to_segment_url'] = urllib.parse.urlparse(manifest_url).query
94 formats.extend(fmts)
70c5802b 95
70c5802b 96 creator = f.get('created_by') or {}
97
98 return {
99 'id': file_id,
100 'title': title,
101 'formats': formats,
102 'description': f.get('description') or None,
103 'uploader': creator.get('name'),
104 'timestamp': parse_iso8601(f.get('created_at')),
105 'uploader_id': creator.get('id'),
106 }