el.xwx.moe/lib/shared/getTitle.ts

42 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-12-31 09:46:09 -06:00
import fetch from "node-fetch";
import https from "https";
import { SocksProxyAgent } from "socks-proxy-agent";
export default async function getTitle(url: string) {
2023-06-26 18:35:12 -05:00
try {
2023-12-31 09:46:09 -06:00
const httpsAgent = new https.Agent({
rejectUnauthorized:
process.env.IGNORE_UNAUTHORIZED_CA === "true" ? false : true,
});
// fetchOpts allows a proxy to be defined
let fetchOpts = {
2023-12-31 09:46:09 -06:00
agent: httpsAgent,
};
2024-02-19 14:38:36 -06:00
if (process.env.PROXY) {
// parse proxy url
2024-02-19 14:38:36 -06:00
let proxy = new URL(process.env.PROXY);
// if authentication set, apply to proxy URL
2024-02-19 14:38:36 -06:00
if (process.env.PROXY_USERNAME) {
proxy.username = process.env.PROXY_USERNAME;
proxy.password = process.env.PROXY_PASSWORD || "";
}
// add socks5 proxy to fetchOpts
fetchOpts = { agent: new SocksProxyAgent(proxy.toString()) }; //TODO: add support for http/https proxies
}
const response = await fetch(url, fetchOpts);
2023-06-26 18:35:12 -05:00
const text = await response.text();
2023-06-26 18:35:12 -05:00
// regular expression to find the <title> tag
let match = text.match(/<title.*>([^<]*)<\/title>/);
2023-07-24 08:39:51 -05:00
if (match) return match[1];
2023-06-26 18:35:12 -05:00
else return "";
} catch (err) {
console.log(err);
}
}