Skip to content

Commit

Permalink
improve header reading
Browse files Browse the repository at this point in the history
  • Loading branch information
Last-Order committed Aug 25, 2021
1 parent a12d8e9 commit 39b5d63
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "minyami",
"version": "4.2.8",
"version": "4.2.9",
"description": "",
"main": "dist/exports.js",
"types": "dist/exports.d.ts",
Expand Down
12 changes: 8 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Help:
--cookies <cookies> Cookies to download
<cookies>
--headers <headers> HTTP Headers used to download
<headers> Multiple headers should be splited with \n. eg. --headers "Cookie: a=1\nUser-Agent: X-UA". Don't forget to escape. This option will override --cookies.
<headers> Custom headers. eg. --headers "User-Agent: xxxxx". This option will override --cookies.
--live Download live
--format <format_name> (Optional) Set output format. default: ts
<format_name> Format name. ts or mkv.
Expand Down Expand Up @@ -76,6 +76,10 @@ Q: How to set temporary file location.

A: You can use environment variables to set the directory of temporary files. See [Issue #80](https://github.com/Last-Order/Minyami/issues/80#issuecomment-869132412).

Q: How to set multiple HTTP headers.

A: By providing multiple --headers option. For example, `minyami -d xxxx --headers "Cookie: xxxx" --headers "User-Agent: yyy"`.

## Use as a library (3.1.0+)

```TypeScript
Expand All @@ -91,7 +95,7 @@ The `'chunk-downloaded'` event is emitted when every media chunk is downloaded.

### Event: `chunk-error`

- `error: Error`
- `error: Error`

The `'chunk-error'` event is emitted when failed to download or decrypt media chunks.

Expand All @@ -105,13 +109,13 @@ The `'finished'` event is emitted after all the works are done. CLI program exit

### Event: `merge-error`

- `error: Error`
- `error: Error`

The `merge-error` event is emitted when a merge progress is failed.

### Event: `critical-error`

- `error: Error`
- `error: Error`

The `critical-error` is emitted when a error that Minyami can't handle happens.

Expand Down
8 changes: 6 additions & 2 deletions readme.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Help:
--cookies <cookies> (可选) 视频下载 Cookies
<cookies>
--headers <headers> 手动设定 HTTP Headers
<headers> 多个 Header 使用 "\n" 分隔,例如:"Cookie: a=1\nUser-Agent: X-UA"
<headers> 自定义 HTTP Header,例如:"User-Agent: X-UA"
--live 直播下载模式
--format <format_name> (可选) 输出格式,默认为 ts
<format_name> 格式名称,ts 或 mkv
Expand Down Expand Up @@ -68,4 +68,8 @@ A: 可以使用`--proxy`参数设置代理,详见上方用法。目前支持`H

Q: 如何设置临时文件目录?

A: 通过环境变量可以设置临时文件目录。详见 [Issue #80](https://github.com/Last-Order/Minyami/issues/80#issuecomment-869132412)
A: 通过环境变量可以设置临时文件目录。详见 [Issue #80](https://github.com/Last-Order/Minyami/issues/80#issuecomment-869132412)

Q: 如何设置多个 HTTP Header?

A: 通过设置多个`--headers`,例如`minyami -d xxxx --headers "Cookie: xxxx" --headers "User-Agent: yyy"`
17 changes: 10 additions & 7 deletions src/core/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface DownloaderConfig {
key?: string;
verbose?: boolean;
cookies?: string;
headers?: string;
headers?: string | string[];
retries?: number;
proxy?: string;
format?: string;
Expand Down Expand Up @@ -163,12 +163,15 @@ class Downloader extends EventEmitter {
}

if (headers) {
for (const h of headers.split("\\n")) {
try {
const header = /^([^ :]+):(.+)$/.exec(h).slice(1);
this.headers[header[0]] = header[1].trim();
} catch (e) {
logger.warning(`HTTP Headers invalid. Ignored.`);
const headerConfigArr = Array.isArray(headers) ? headers : [headers];
for (const headerConfig of headerConfigArr) {
for (const h of headerConfig.split("\\n")) {
try {
const header = /^([^ :]+):(.+)$/.exec(h).slice(1);
this.headers[header[0]] = header[1].trim();
} catch (e) {
logger.warning(`HTTP Headers invalid. Ignored.`);
}
}
}
// Apply global custom headers
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,7 @@ Erii.addOption({
description: "HTTP Headers used to download",
argument: {
name: "headers",
description:
'Multiple headers should be splited with \\n. eg. --headers "Cookie: a=1\\nUser-Agent: X-UA". Don\'t forget to escape. This option will override --cookies.',
description: 'Custom headers. eg. --headers "User-Agent: xxxxx". This option will override --cookies.',
},
});

Expand Down

0 comments on commit 39b5d63

Please sign in to comment.