2024-05-03 12:13:07 +03:00
|
|
|
import UserAgent from "user-agents";
|
|
|
|
|
2024-04-28 21:21:14 +03:00
|
|
|
import type { Repack } from "@main/entity";
|
2024-04-21 08:26:29 +03:00
|
|
|
import { repackRepository } from "@main/repository";
|
|
|
|
|
2024-04-28 21:21:14 +03:00
|
|
|
import { QueryDeepPartialEntity } from "typeorm/query-builder/QueryPartialEntity";
|
2024-04-21 08:26:29 +03:00
|
|
|
|
2024-04-28 21:21:14 +03:00
|
|
|
export const savePage = async (repacks: QueryDeepPartialEntity<Repack>[]) =>
|
2024-04-21 08:26:29 +03:00
|
|
|
Promise.all(
|
|
|
|
repacks.map((repack) => repackRepository.insert(repack).catch(() => {}))
|
|
|
|
);
|
|
|
|
|
2024-05-03 12:13:07 +03:00
|
|
|
export const requestWebPage = async (url: string) => {
|
|
|
|
const userAgent = new UserAgent();
|
|
|
|
|
|
|
|
return fetch(url, {
|
2024-04-21 08:26:29 +03:00
|
|
|
method: "GET",
|
2024-05-03 12:13:07 +03:00
|
|
|
headers: {
|
|
|
|
"User-Agent": userAgent.toString(),
|
|
|
|
},
|
2024-04-21 08:26:29 +03:00
|
|
|
}).then((response) => response.text());
|
2024-05-03 12:13:07 +03:00
|
|
|
};
|
2024-05-03 20:33:31 +03:00
|
|
|
|
|
|
|
export const decodeNonUtf8Response = async (res: Response) => {
|
|
|
|
const contentType = res.headers.get("content-type");
|
|
|
|
if (!contentType) return res.text();
|
|
|
|
|
|
|
|
const charset = contentType.substring(contentType.indexOf("charset=") + 8);
|
|
|
|
|
|
|
|
const text = await res.arrayBuffer().then((ab) => {
|
|
|
|
const dataView = new DataView(ab);
|
|
|
|
const decoder = new TextDecoder(charset);
|
|
|
|
|
|
|
|
return decoder.decode(dataView);
|
|
|
|
});
|
|
|
|
|
|
|
|
return text;
|
|
|
|
};
|