|
@ -4,9 +4,9 @@ import https from "https"; |
|
|
/** |
|
|
/** |
|
|
* Perform an RSS/XML request |
|
|
* Perform an RSS/XML request |
|
|
*/ |
|
|
*/ |
|
|
export function rssRequest<T = any>(url: string) { |
|
|
|
|
|
return new Promise<T>((resolve, reject) => { |
|
|
|
|
|
https.get(url, { headers: { "User-Agent": "Node", "Accept": "application/rss+xml" } }, (response) => { |
|
|
|
|
|
|
|
|
export function rssRequest<T = any>(url: string, headers: {[key: string]: string} = {}) { |
|
|
|
|
|
return new Promise<[number|undefined, T]>((resolve, reject) => { |
|
|
|
|
|
https.get(url, { headers: { "User-Agent": "Autoplex", "Accept": "application/rss+xml" } }, (response) => { |
|
|
if (response.statusCode !== 200) { |
|
|
if (response.statusCode !== 200) { |
|
|
reject("Status error: " + response.statusCode); |
|
|
reject("Status error: " + response.statusCode); |
|
|
return; |
|
|
return; |
|
@ -19,7 +19,7 @@ export function rssRequest<T = any>(url: string) { |
|
|
reject(err); |
|
|
reject(err); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
resolve(result); |
|
|
|
|
|
|
|
|
resolve([response.statusCode, result]); |
|
|
})); |
|
|
})); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
@ -28,17 +28,13 @@ export function rssRequest<T = any>(url: string) { |
|
|
/** |
|
|
/** |
|
|
* Perform a generic GET request |
|
|
* Perform a generic GET request |
|
|
*/ |
|
|
*/ |
|
|
export function jsonRequest<T = any>(url: string) { |
|
|
|
|
|
return new Promise<T>((resolve, reject) => { |
|
|
|
|
|
https.get(url, { headers: { "User-Agent": "Node", "Accept": "*/*" } }, (response) => { |
|
|
|
|
|
if (response.statusCode !== 200) { |
|
|
|
|
|
reject("Status error: " + response.statusCode); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
export function jsonRequest<T = any>(url: string, headers: {[key: string]: string} = {}) { |
|
|
|
|
|
return new Promise<[number|undefined, T]>((resolve, reject) => { |
|
|
|
|
|
https.get(url, { headers: { "User-Agent": "Autoplex", "Accept": "*/*", ...headers} }, (response) => { |
|
|
response.setEncoding("utf-8"); |
|
|
response.setEncoding("utf-8"); |
|
|
let body = ""; |
|
|
let body = ""; |
|
|
response.on("data", (chunk) => body += chunk); |
|
|
response.on("data", (chunk) => body += chunk); |
|
|
response.on("end", () => resolve(JSON.parse(body))); |
|
|
|
|
|
|
|
|
response.on("end", () => resolve([response.statusCode, JSON.parse(body)])); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
@ -46,9 +42,9 @@ export function rssRequest<T = any>(url: string) { |
|
|
/** |
|
|
/** |
|
|
* Perform a generic GET request |
|
|
* Perform a generic GET request |
|
|
*/ |
|
|
*/ |
|
|
export function request(url: string, timeout: number = 10000) { |
|
|
|
|
|
return new Promise<string>((resolve, reject) => { |
|
|
|
|
|
https.get(url, { headers: { "User-Agent": "Node", "Accept": "*/*" }, timeout }, (response) => { |
|
|
|
|
|
|
|
|
export function request(url: string, timeout: number = 10000, headers: {[key: string]: string} = {}) { |
|
|
|
|
|
return new Promise<[number|undefined, string]>((resolve, reject) => { |
|
|
|
|
|
https.get(url, { headers: { "User-Agent": "Autoplex", "Accept": "*/*", ...headers }, timeout }, (response) => { |
|
|
if (response.statusCode !== 200) { |
|
|
if (response.statusCode !== 200) { |
|
|
reject("Status error: " + response.statusCode); |
|
|
reject("Status error: " + response.statusCode); |
|
|
return; |
|
|
return; |
|
@ -56,15 +52,11 @@ export function rssRequest<T = any>(url: string) { |
|
|
response.setEncoding("utf-8"); |
|
|
response.setEncoding("utf-8"); |
|
|
let body = ""; |
|
|
let body = ""; |
|
|
response.on("data", (chunk) => body += chunk); |
|
|
response.on("data", (chunk) => body += chunk); |
|
|
response.on("end", () => resolve(body)); |
|
|
|
|
|
|
|
|
response.on("end", () => resolve([response.statusCode, body])); |
|
|
}).on("timeout", () => reject("timeout")); |
|
|
}).on("timeout", () => reject("timeout")); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export function sleep(ms: number) { |
|
|
|
|
|
return new Promise(resolve => setTimeout(resolve, ms)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function convertToBytes(size: number, unit: string, throwUnknownUnit: boolean = true) { |
|
|
export function convertToBytes(size: number, unit: string, throwUnknownUnit: boolean = true) { |
|
|
switch(unit.toUpperCase()) { |
|
|
switch(unit.toUpperCase()) { |
|
|
case "GB": |
|
|
case "GB": |
|
|