]>
Commit | Line | Data |
---|---|---|
1 | import json | |
2 | import urllib.parse | |
3 | ||
4 | from .common import InfoExtractor | |
5 | from ..utils import ( | |
6 | parse_iso8601, | |
7 | update_url_query, | |
8 | url_or_none, | |
9 | ) | |
10 | from ..utils.traversal import traverse_obj | |
11 | ||
12 | ||
13 | class BoxIE(InfoExtractor): | |
14 | _VALID_URL = r'https?://(?:[^.]+\.)?app\.box\.com/s/(?P<shared_name>[^/?#]+)/file/(?P<id>\d+)' | |
15 | _TEST = { | |
16 | 'url': 'https://mlssoccer.app.box.com/s/0evd2o3e08l60lr4ygukepvnkord1o1x/file/510727257538', | |
17 | 'md5': '1f81b2fd3960f38a40a3b8823e5fcd43', | |
18 | 'info_dict': { | |
19 | 'id': '510727257538', | |
20 | 'ext': 'mp4', | |
21 | 'title': 'Garber St. Louis will be 28th MLS team +scarving.mp4', | |
22 | 'uploader': '', | |
23 | 'timestamp': 1566320259, | |
24 | 'upload_date': '20190820', | |
25 | 'uploader_id': '235196876', | |
26 | }, | |
27 | 'params': {'skip_download': 'dash fragment too small'}, | |
28 | } | |
29 | ||
30 | def _real_extract(self, url): | |
31 | shared_name, file_id = self._match_valid_url(url).groups() | |
32 | webpage = self._download_webpage(url, file_id) | |
33 | request_token = self._parse_json(self._search_regex( | |
34 | r'Box\.config\s*=\s*({.+?});', webpage, | |
35 | 'Box config'), file_id)['requestToken'] | |
36 | access_token = self._download_json( | |
37 | 'https://app.box.com/app-api/enduserapp/elements/tokens', file_id, | |
38 | 'Downloading token JSON metadata', | |
39 | data=json.dumps({'fileIDs': [file_id]}).encode(), headers={ | |
40 | 'Content-Type': 'application/json', | |
41 | 'X-Request-Token': request_token, | |
42 | 'X-Box-EndUser-API': 'sharedName=' + shared_name, | |
43 | })[file_id]['read'] | |
44 | shared_link = 'https://app.box.com/s/' + shared_name | |
45 | f = self._download_json( | |
46 | 'https://api.box.com/2.0/files/' + file_id, file_id, | |
47 | 'Downloading file JSON metadata', headers={ | |
48 | 'Authorization': 'Bearer ' + access_token, | |
49 | 'BoxApi': 'shared_link=' + shared_link, | |
50 | 'X-Rep-Hints': '[dash]', # TODO: extract `hls` formats | |
51 | }, query={ | |
52 | 'fields': 'authenticated_download_url,created_at,created_by,description,extension,is_download_available,name,representations,size' | |
53 | }) | |
54 | title = f['name'] | |
55 | ||
56 | query = { | |
57 | 'access_token': access_token, | |
58 | 'shared_link': shared_link | |
59 | } | |
60 | ||
61 | formats = [] | |
62 | ||
63 | for url_tmpl in traverse_obj(f, ( | |
64 | 'representations', 'entries', lambda _, v: v['representation'] == 'dash', | |
65 | 'content', 'url_template', {url_or_none} | |
66 | )): | |
67 | manifest_url = update_url_query(url_tmpl.replace('{+asset_path}', 'manifest.mpd'), query) | |
68 | fmts = self._extract_mpd_formats(manifest_url, file_id) | |
69 | for fmt in fmts: | |
70 | fmt['extra_param_to_segment_url'] = urllib.parse.urlparse(manifest_url).query | |
71 | formats.extend(fmts) | |
72 | ||
73 | creator = f.get('created_by') or {} | |
74 | ||
75 | return { | |
76 | 'id': file_id, | |
77 | 'title': title, | |
78 | 'formats': formats, | |
79 | 'description': f.get('description') or None, | |
80 | 'uploader': creator.get('name'), | |
81 | 'timestamp': parse_iso8601(f.get('created_at')), | |
82 | 'uploader_id': creator.get('id'), | |
83 | } |