2023-12-31 09:46:09 -06:00
|
|
|
import fetch from "node-fetch";
|
|
|
|
import https from "https";
|
2024-02-18 15:42:51 -06:00
|
|
|
import { SocksProxyAgent } from "socks-proxy-agent";
|
|
|
|
|
2023-06-09 17:31:14 -05:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
2024-02-18 15:42:51 -06:00
|
|
|
// fetchOpts allows a proxy to be defined
|
|
|
|
let fetchOpts = {
|
2023-12-31 09:46:09 -06:00
|
|
|
agent: httpsAgent,
|
2024-02-18 15:42:51 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
if (process.env.ARCHIVER_PROXY) {
|
|
|
|
// parse proxy url
|
|
|
|
let proxy = new URL(process.env.ARCHIVER_PROXY)
|
|
|
|
// if authentication set, apply to proxy URL
|
|
|
|
if (process.env.ARCHIVER_PROXY_USERNAME) {
|
|
|
|
proxy.username = process.env.ARCHIVER_PROXY_USERNAME;
|
|
|
|
proxy.password = process.env.ARCHIVER_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-03-08 15:31:24 -06:00
|
|
|
|
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);
|
|
|
|
}
|
2023-06-09 17:31:14 -05:00
|
|
|
}
|