]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/box.py
Tolerate failure to `--write-link` due to unknown URL
[yt-dlp.git] / yt_dlp / extractor / box.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5
6 from .common import InfoExtractor
7 from ..utils import (
8 determine_ext,
9 parse_iso8601,
10 # try_get,
11 update_url_query,
12 )
13
14
15 class BoxIE(InfoExtractor):
16 _VALID_URL = r'https?://(?:[^.]+\.)?app\.box\.com/s/(?P<shared_name>[^/]+)/file/(?P<id>\d+)'
17 _TEST = {
18 'url': 'https://mlssoccer.app.box.com/s/0evd2o3e08l60lr4ygukepvnkord1o1x/file/510727257538',
19 'md5': '1f81b2fd3960f38a40a3b8823e5fcd43',
20 'info_dict': {
21 'id': '510727257538',
22 'ext': 'mp4',
23 'title': 'Garber St. Louis will be 28th MLS team +scarving.mp4',
24 'uploader': 'MLS Video',
25 'timestamp': 1566320259,
26 'upload_date': '20190820',
27 'uploader_id': '235196876',
28 }
29 }
30
31 def _real_extract(self, url):
32 shared_name, file_id = self._match_valid_url(url).groups()
33 webpage = self._download_webpage(url, file_id)
34 request_token = self._parse_json(self._search_regex(
35 r'Box\.config\s*=\s*({.+?});', webpage,
36 'Box config'), file_id)['requestToken']
37 access_token = self._download_json(
38 'https://app.box.com/app-api/enduserapp/elements/tokens', file_id,
39 'Downloading token JSON metadata',
40 data=json.dumps({'fileIDs': [file_id]}).encode(), headers={
41 'Content-Type': 'application/json',
42 'X-Request-Token': request_token,
43 'X-Box-EndUser-API': 'sharedName=' + shared_name,
44 })[file_id]['read']
45 shared_link = 'https://app.box.com/s/' + shared_name
46 f = self._download_json(
47 'https://api.box.com/2.0/files/' + file_id, file_id,
48 'Downloading file JSON metadata', headers={
49 'Authorization': 'Bearer ' + access_token,
50 'BoxApi': 'shared_link=' + shared_link,
51 'X-Rep-Hints': '[dash]', # TODO: extract `hls` formats
52 }, query={
53 'fields': 'authenticated_download_url,created_at,created_by,description,extension,is_download_available,name,representations,size'
54 })
55 title = f['name']
56
57 query = {
58 'access_token': access_token,
59 'shared_link': shared_link
60 }
61
62 formats = []
63
64 # for entry in (try_get(f, lambda x: x['representations']['entries'], list) or []):
65 # entry_url_template = try_get(
66 # entry, lambda x: x['content']['url_template'])
67 # if not entry_url_template:
68 # continue
69 # representation = entry.get('representation')
70 # if representation == 'dash':
71 # TODO: append query to every fragment URL
72 # formats.extend(self._extract_mpd_formats(
73 # entry_url_template.replace('{+asset_path}', 'manifest.mpd'),
74 # file_id, query=query))
75
76 authenticated_download_url = f.get('authenticated_download_url')
77 if authenticated_download_url and f.get('is_download_available'):
78 formats.append({
79 'ext': f.get('extension') or determine_ext(title),
80 'filesize': f.get('size'),
81 'format_id': 'download',
82 'url': update_url_query(authenticated_download_url, query),
83 })
84
85 self._sort_formats(formats)
86
87 creator = f.get('created_by') or {}
88
89 return {
90 'id': file_id,
91 'title': title,
92 'formats': formats,
93 'description': f.get('description') or None,
94 'uploader': creator.get('name'),
95 'timestamp': parse_iso8601(f.get('created_at')),
96 'uploader_id': creator.get('id'),
97 }