diff --git a/lib/apify/scrapeProfileUrl.ts b/lib/apify/scrapeProfileUrl.ts index b786dd6..2ca5934 100644 --- a/lib/apify/scrapeProfileUrl.ts +++ b/lib/apify/scrapeProfileUrl.ts @@ -4,6 +4,7 @@ import startTwitterProfileScraping from "../twitter/startTwitterProfileScraping" import startThreadsProfileScraping from "../threads/startThreadsProfileScraping"; import startYoutubeProfileScraping from "../youtube/startYoutubeProfileScraping"; import startFacebookProfileScraping from "../facebook/startFacebookProfileScraping"; +import { getUsernameFromProfileUrl } from "../socials/getUsernameFromProfileUrl"; type ScrapeRunner = (handle: string) => Promise<{ runId: string; @@ -71,8 +72,10 @@ export const scrapeProfileUrl = async ( return null; } + const finalUsername = username || getUsernameFromProfileUrl(profileUrl); + try { - const result = await platform.scraper(username); + const result = await platform.scraper(finalUsername); if (!result) { return { diff --git a/lib/socials/getUsernameFromProfileUrl.ts b/lib/socials/getUsernameFromProfileUrl.ts new file mode 100644 index 0000000..c416728 --- /dev/null +++ b/lib/socials/getUsernameFromProfileUrl.ts @@ -0,0 +1,21 @@ +/** + * Extracts username from a profile URL by pulling text after .com/ or .net/ until ? or / + * @param profileUrl - The profile URL to extract username from + * @returns The username or empty string if unable to extract + */ +export const getUsernameFromProfileUrl = ( + profileUrl: string | null | undefined +): string => { + if (!profileUrl) { + return ""; + } + + try { + const normalizedUrl = profileUrl.toLowerCase().trim(); + const match = normalizedUrl.match(/(?:\.com|\.net)\/([^/?]+)/); + return match ? match[1] : ""; + } catch (error) { + console.error("[ERROR] Error extracting username from profile URL:", error); + return ""; + } +};