Skip to content
This repository was archived by the owner on Nov 5, 2020. It is now read-only.

Commit 1a84f6b

Browse files
committed
feat: Merge branch dev
2 parents 125cb11 + 1d56cb8 commit 1a84f6b

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ytdl/ytdl",
3-
"version": "1.4.5",
3+
"version": "1.4.6",
44
"description": "A CLI/Library written in typescript/javascript, which allows you to download/play videos from YouTube onto your system.",
55
"keywords": [
66
"youtube",

src/videoDownloader.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,41 @@ export default class VideoDownloader {
2121
* Returns a `Node.js` stream obtained by sending a GET request to `this.url`.
2222
* @param headers Stores optional headers as object
2323
*/
24-
public async stream(headers?: object, preText?: string) {
25-
const stream = await axios({
26-
method: 'get',
27-
url: this.url,
28-
responseType: 'stream',
29-
headers,
30-
});
31-
32-
if (this.logger) {
33-
const progressBar = new ProgressBar(stream.headers['content-length'], preText, this.logger);
34-
stream.data.on('data', (chunk: string) => {
35-
progressBar.update(chunk.length);
36-
});
37-
}
24+
public async stream(headers?: object) {
25+
const stream = await this.request(headers);
3826
return stream.data;
3927
}
4028

4129
/**
4230
* Saves the downloaded stream to a file specified by `filename`.
4331
* @param filename Stores the filename to store the downloaded stream in
4432
*/
45-
public async download(filename: string): Promise<void> {
46-
const videoStream = await this.stream({}, 'Downloading');
33+
public async download(filename: string, headers?: object): Promise<void> {
34+
const stream = await this.request(headers);
35+
36+
if (this.logger) {
37+
const progressBar = new ProgressBar(stream.headers['content-length'], 'Downloading', this.logger);
38+
stream.data.on('data', (chunk: string) => {
39+
progressBar.update(chunk.length);
40+
});
41+
}
4742

4843
return new Promise((resolve, reject) => {
49-
videoStream
44+
stream.data
5045
.pipe(fs.createWriteStream(filename))
5146
.on('finish', (err: Error) => {
5247
if (err) reject(err);
5348
else resolve();
5449
});
5550
});
5651
}
52+
53+
private async request(headers?: object) {
54+
return axios({
55+
method: 'get',
56+
url: this.url,
57+
responseType: 'stream',
58+
headers,
59+
});
60+
}
5761
}

0 commit comments

Comments
 (0)