···11+import logger from "./logger.js";
22+13/* Normalize the Unicode characters: this doesn't consistently work yet, there is something about certain bluesky strings that causes it to fail. */
24export function normalizeUnicode(text: string): string {
35 // First decompose the characters (NFD)
···3133 // Final NFKC normalization to handle any remaining special characters
3234 return withoutMath.normalize("NFKC");
3335}
3636+3737+export async function getFinalUrl(url: string): Promise<string> {
3838+ const controller = new AbortController();
3939+ const timeoutId = setTimeout(() => controller.abort(), 10000); // 10-second timeout
4040+4141+ try {
4242+ const response = await fetch(url, {
4343+ method: "HEAD",
4444+ redirect: "follow", // This will follow redirects automatically
4545+ signal: controller.signal, // Pass the abort signal to fetch
4646+ });
4747+ clearTimeout(timeoutId); // Clear the timeout if fetch completes
4848+ return response.url; // This will be the final URL after redirects
4949+ } catch (error) {
5050+ clearTimeout(timeoutId); // Clear the timeout if fetch fails
5151+ // Log the error with more specific information if it's a timeout
5252+ if (error instanceof Error && error.name === "AbortError") {
5353+ logger.warn(`Timeout fetching URL: ${url}`, error);
5454+ } else {
5555+ logger.warn(`Error fetching URL: ${url}`, error);
5656+ }
5757+ throw error; // Re-throw the error to be caught by the caller
5858+ }
5959+}
6060+6161+export async function getLanguage(profile: string): Promise<string> {
6262+ if (typeof profile !== "string" || profile === null) {
6363+ logger.warn(
6464+ "[GETLANGUAGE] getLanguage called with invalid profile data, defaulting to 'eng'.",
6565+ profile,
6666+ );
6767+ return "eng"; // Default or throw an error
6868+ }
6969+7070+ const profileText = profile.trim();
7171+7272+ if (profileText.length === 0) {
7373+ return "eng";
7474+ }
7575+7676+ const lande = (await import("lande")).default;
7777+ let langsProbabilityMap = lande(profileText);
7878+7979+ // Sort by probability in descending order
8080+ langsProbabilityMap.sort(
8181+ (a: [string, number], b: [string, number]) => b[1] - a[1],
8282+ );
8383+8484+ // Return the language code with the highest probability
8585+ return langsProbabilityMap[0][0];
8686+}