Skip to content

Commit 382b6b3

Browse files
authored
feat: add torrent download from url (#102)
1 parent 775eb3e commit 382b6b3

File tree

6 files changed

+829
-661
lines changed

6 files changed

+829
-661
lines changed

Diff for: package-lock.json

+776-649
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+11-11
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,29 @@
2121
"lint:fix": "eslint --fix --ext .js,.ts, .",
2222
"prepare": "npm run build",
2323
"build": "tsc -p tsconfig.build.json",
24-
"build:docs": "typedoc --out docs --hideGenerator --target ES6 --mode file src && touch docs/.nojekyll",
24+
"build:docs": "typedoc",
2525
"test": "jest --runInBand",
2626
"test:watch": "jest --watch --runInBand",
2727
"test:ci": "jest --runInBand --coverage --no-cache"
2828
},
2929
"dependencies": {
3030
"@ctrl/shared-torrent": "^3.0.4",
31-
"@ctrl/url-join": "^1.0.2",
32-
"form-data": "^3.0.0",
33-
"got": "^11.8.1",
31+
"@ctrl/url-join": "^1.0.4",
32+
"form-data": "^4.0.0",
33+
"got": "^11.8.2",
3434
"tough-cookie": "^4.0.0"
3535
},
3636
"devDependencies": {
37-
"@babel/plugin-transform-modules-commonjs": "7.12.1",
38-
"@babel/preset-typescript": "7.12.7",
39-
"@ctrl/eslint-config": "1.2.8",
37+
"@babel/plugin-transform-modules-commonjs": "7.13.8",
38+
"@babel/preset-typescript": "7.13.0",
39+
"@ctrl/eslint-config": "1.3.4",
4040
"@jest/globals": "26.6.2",
41-
"@types/node": "14.14.13",
41+
"@types/node": "14.14.41",
4242
"@types/tough-cookie": "4.0.0",
4343
"jest": "26.6.3",
44-
"p-wait-for": "3.1.0",
45-
"typedoc": "0.19.2",
46-
"typescript": "4.1.3"
44+
"p-wait-for": "3.2.0",
45+
"typedoc": "0.20.35",
46+
"typescript": "4.2.4"
4747
},
4848
"jest": {
4949
"testEnvironment": "node"

Diff for: renovate.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"extends": ["github>scttcper/renovate-config"]
2+
"enabled": false
33
}

Diff for: src/index.ts

+16
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,22 @@ export class Deluge implements TorrentClient {
233233
return JSON.parse(res.body) as UploadResponse;
234234
}
235235

236+
/**
237+
* Download a torrent from url, pass the result to {@link Deluge.addTorrent}
238+
* @param url
239+
* @param cookies
240+
* @returns file path
241+
*/
242+
async downloadFromUrl(url: string, cookies = ''): Promise<string> {
243+
const res = await this.request<StringStatus>('web.download_torrent_from_url', [url, cookies]);
244+
245+
if (!res.body.result) {
246+
throw new Error('Failed to download torrent');
247+
}
248+
249+
return res.body.result;
250+
}
251+
236252
async addTorrent(
237253
torrent: string | Buffer,
238254
config: Partial<AddTorrentOptions> = {},

Diff for: test/index.spec.ts

+17
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,21 @@ describe('Deluge', () => {
308308
expect(torrent.totalUploaded).toBeGreaterThanOrEqual(0);
309309
expect(torrent.uploadSpeed).toBeGreaterThanOrEqual(0);
310310
}, 15000);
311+
it('should download from url', async () => {
312+
const client = new Deluge({ baseUrl });
313+
const result = await client.downloadFromUrl(
314+
'https://releases.ubuntu.com/20.10/ubuntu-20.10-desktop-amd64.iso.torrent',
315+
);
316+
expect(result).toContain('/tmp/');
317+
await client.addTorrent(torrentFile, { add_paused: true });
318+
await pWaitFor(
319+
async () => {
320+
const r = await client.listTorrents();
321+
return Object.keys(r.result.torrents).length === 1;
322+
},
323+
{ timeout: 10000 },
324+
);
325+
const res = await client.listTorrents();
326+
expect(Object.keys(res.result.torrents)).toHaveLength(1);
327+
}, 15000);
311328
});

Diff for: typedoc.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"entryPoints": ["src/index.ts"],
3+
"tsconfig": "tsconfig.build.json",
4+
"out": "docs",
5+
"excludePrivate": true,
6+
"excludeProtected": true,
7+
"hideGenerator": true
8+
}

0 commit comments

Comments
 (0)