]>
Commit | Line | Data |
---|---|---|
70c5802b | 1 | import json |
5a230233 | 2 | import urllib.parse |
70c5802b | 3 | |
4 | from .common import InfoExtractor | |
5 | from ..utils import ( | |
70c5802b | 6 | parse_iso8601, |
70c5802b | 7 | update_url_query, |
5a230233 | 8 | url_or_none, |
70c5802b | 9 | ) |
5a230233 | 10 | from ..utils.traversal import traverse_obj |
70c5802b | 11 | |
12 | ||
13 | class BoxIE(InfoExtractor): | |
5a230233 | 14 | _VALID_URL = r'https?://(?:[^.]+\.)?app\.box\.com/s/(?P<shared_name>[^/?#]+)/file/(?P<id>\d+)' |
70c5802b | 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', | |
5a230233 | 22 | 'uploader': '', |
70c5802b | 23 | 'timestamp': 1566320259, |
24 | 'upload_date': '20190820', | |
25 | 'uploader_id': '235196876', | |
5a230233 | 26 | }, |
27 | 'params': {'skip_download': 'dash fragment too small'}, | |
70c5802b | 28 | } |
29 | ||
30 | def _real_extract(self, url): | |
5ad28e7f | 31 | shared_name, file_id = self._match_valid_url(url).groups() |
70c5802b | 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 | ||
5a230233 | 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) | |
70c5802b | 72 | |
70c5802b | 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 | } |