]>
Commit | Line | Data |
---|---|---|
1 | import json | |
2 | import urllib.parse | |
3 | ||
4 | from .common import InfoExtractor | |
5 | from ..utils import ( | |
6 | ExtractorError, | |
7 | parse_iso8601, | |
8 | update_url_query, | |
9 | url_or_none, | |
10 | ) | |
11 | from ..utils.traversal import traverse_obj | |
12 | ||
13 | ||
14 | class BoxIE(InfoExtractor): | |
15 | _VALID_URL = r'https?://(?:[^.]+\.)?app\.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', | |
27 | }, | |
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', | |
39 | }, | |
40 | 'params': {'skip_download': 'dash fragment too small'}, | |
41 | }] | |
42 | ||
43 | def _real_extract(self, url): | |
44 | shared_name, file_id = self._match_valid_url(url).groups() | |
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'] | |
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={ | |
75 | 'fields': 'authenticated_download_url,created_at,created_by,description,extension,is_download_available,name,representations,size' | |
76 | }) | |
77 | title = f['name'] | |
78 | ||
79 | query = { | |
80 | 'access_token': access_token, | |
81 | 'shared_link': shared_link | |
82 | } | |
83 | ||
84 | formats = [] | |
85 | ||
86 | for url_tmpl in traverse_obj(f, ( | |
87 | 'representations', 'entries', lambda _, v: v['representation'] == 'dash', | |
88 | 'content', 'url_template', {url_or_none} | |
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) | |
95 | ||
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 | } |