我正在尝试从服务器获取 mp3 的最终下载文件名,具体来说是 audiotool.com
例如http://api.audiotool.com/track/haunt_opaque/mixdown.mp3
当你在网络浏览器中导航到此 URL 时,它会开始下载一个文件,其名称包含该歌曲的完整信息
例如“opaqity - Haunt (opaque).mp3”
是否可以使用标准 Linux 命令(例如 curl)从顶部链接获取最终下载名称?我已经尝试使用几个重定向查找器命令和 curl,但都没有奏效。
感谢您的帮助
答案1
echo -e "GET /track/haunt_opaque/mixdown.mp3 HTTP/1.1\nHost: api.audiotool.com\n\n" | nc -q 3 api.audiotool.com 80 | head -n 20 | grep -a "^Content-Disposition:" | cut -d = -f 2
输出:
"opaqity - Haunt (opaque).mp3"
答案2
使用wget
,文件将按照 URL 指定的方式保存为mixdown.mp3
。如果wget -S
使用 ,则如果需要,全名将显示在Content-Disposition
处置标题下:
$ wget -S http://api.audiotool.com/track/haunt_opaque/mixdown.mp3
--2014-09-26 15:34:19-- http://api.audiotool.com/track/haunt_opaque/mixdown.mp3
Resolving api.audiotool.com (api.audiotool.com)... 144.76.222.84
Connecting to api.audiotool.com (api.audiotool.com)|144.76.222.84|:80... connected.
HTTP request sent, awaiting response...
HTTP/1.1 200 OK
Server: nginx/1.5.6
Date: Fri, 26 Sep 2014 22:34:20 GMT
Content-Type: audio/mpeg
Transfer-Encoding: chunked
Connection: keep-alive
Keep-Alive: timeout=65
Access-Control-Allow-Origin: *
Expires: Fri, 26 Sep 2014 22:34:20 GMT
Cache-Control: no-cache, private, no-store
Content-Disposition: attachment; filename="opaqity - Haunt (opaque).mp3"
Pragma: no-cache
X-Lift-Version: 2.4
Length: unspecified [audio/mpeg]
Saving to: `mixdown.mp3'
[ <=> ] 4,499,633 784K/s in 7.0s
2014-09-26 15:34:27 (630 KB/s) - `mixdown.mp3' saved [4499633]
答案3
无论如何,在文件传输完成之前,这不会告诉您远程文件名:
user@server ~
$ curl --remote-header-name --remote-name http://api.audiotool.com/track/haunt_opaque/mixdown.mp3
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 4394k 0 4394k 0 0 167k 0 --:--:-- 0:00:26 --:--:-- 294k
curl: Saved to filename 'opaqity - Haunt (opaque).mp3'
我能够使用 Content-Disposition 标头值获取 Chrome 开发人员工具的响应标头。不确定 curl 出现问题的原因,但我怀疑这与 nginx 性能原因有关。
HTTP/1.1 200 OK
Server: nginx/1.5.6
Date: Fri, 26 Sep 2014 22:08:05 GMT
Content-Type: audio/mpeg
Transfer-Encoding: chunked
Connection: keep-alive
Keep-Alive: timeout=65
Access-Control-Allow-Origin: *
Expires: Fri, 26 Sep 2014 22:08:05 GMT
Cache-Control: no-cache, private, no-store
Content-Disposition: attachment; filename="opaqity - Haunt (opaque).mp3"
Pragma: no-cache
X-Lift-Version: 2.4