mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-03-09 15:40:26 +00:00
feat: Add openWebTorrent service for fetching seeders and peers
This commit is contained in:
parent
09bd7dcc36
commit
b84df28f39
1 changed files with 50 additions and 0 deletions
50
src/main/services/open-web-torrent.ts
Normal file
50
src/main/services/open-web-torrent.ts
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
import axios, { AxiosResponse } from "axios";
|
||||||
|
import { Agent } from "https";
|
||||||
|
|
||||||
|
type OpenWebTorrentResponse =
|
||||||
|
| {
|
||||||
|
seeds: number;
|
||||||
|
peers: number;
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
error: {
|
||||||
|
code: number;
|
||||||
|
message: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const httpsAgent = new Agent({
|
||||||
|
rejectUnauthorized: false,
|
||||||
|
});
|
||||||
|
const axiosRef = axios.create({
|
||||||
|
baseURL: "https://checker.openwebtorrent.com",
|
||||||
|
httpsAgent
|
||||||
|
});
|
||||||
|
|
||||||
|
export const openWebTorrent = {
|
||||||
|
transformResponseError(response: AxiosResponse<OpenWebTorrentResponse>) {
|
||||||
|
if ("error" in response.data) {
|
||||||
|
throw new Error(response.data.error.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.data;
|
||||||
|
},
|
||||||
|
async getSeedersAndPeers(magnet: string) {
|
||||||
|
const endpoint = "/check";
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
params.append("magnet", magnet);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await axiosRef.get(endpoint, { params });
|
||||||
|
const { seeds, peers } = this.transformResponseError(response);
|
||||||
|
|
||||||
|
return {
|
||||||
|
seeders: seeds,
|
||||||
|
peers: peers,
|
||||||
|
};
|
||||||
|
} catch (e) {
|
||||||
|
const error = e as Error;
|
||||||
|
throw new Error(error.message);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
Loading…
Add table
Add a link
Reference in a new issue