-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from farcasterxyz/horsefacts/classify-embeds
feat: classify embeds
- Loading branch information
Showing
5 changed files
with
92 additions
and
4 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import Linkify from 'linkify-it'; | ||
|
||
const imageExtensions = ['.gif', '.jpeg', '.jpg', '.png', '.svg', '.webp']; | ||
const videoExtensions = ['.mp4', '.avi', '.mov', '.mkv', '.wmv', '.flv', '.webm', '.m4v', '.ogg']; | ||
|
||
export function extractUrlsFromText(text: string): string[] { | ||
const li = new Linkify(); | ||
li.set({ fuzzyEmail: false }); | ||
li.add('mailto:', null); | ||
li.add('@',null); | ||
|
||
const matches = li.match(text) || []; | ||
|
||
return matches.map((match) => match.url); | ||
}; | ||
|
||
export function hasExtension(url: string, extensions: string[]): boolean { | ||
const parsedUrl = new URL(url.toLowerCase()); | ||
|
||
return extensions.some((extension) => parsedUrl.pathname.endsWith(extension) || parsedUrl.search.endsWith(extension)); | ||
}; | ||
|
||
export function hasImageExtension(url: string): boolean { | ||
return hasExtension(url, imageExtensions); | ||
} | ||
|
||
export function hasVideoExtension(url: string): boolean { | ||
return hasExtension(url, videoExtensions); | ||
} | ||
|
||
interface UrlEmbed { | ||
url: string; | ||
} | ||
|
||
interface CastEmbed { | ||
castId: { | ||
fid: number; | ||
hash: string; | ||
} | ||
} | ||
|
||
type Embed = UrlEmbed | CastEmbed; | ||
|
||
export function formatCastEmbed(embed: CastEmbed) { | ||
return `${embed.castId.hash}`; | ||
} | ||
|
||
export function processEmbeds(embeds: Embed[]) { | ||
let urls: string[] = []; | ||
let images: string[] = []; | ||
let videos: string[] = []; | ||
let unknowns: string[] = []; | ||
let casts: string[] = []; | ||
|
||
for (const embed of embeds) { | ||
if ('castId' in embed) { | ||
casts.push(formatCastEmbed(embed)); | ||
continue; | ||
} | ||
|
||
const { url } = embed; | ||
const match = extractUrlsFromText(url); | ||
if (match.length !== 1) { | ||
unknowns.push(url); | ||
continue; | ||
} | ||
|
||
if (hasImageExtension(url)) { | ||
images.push(url); | ||
continue; | ||
} | ||
|
||
if (hasVideoExtension(url)) { | ||
videos.push(url); | ||
continue; | ||
} | ||
|
||
urls.push(url); | ||
} | ||
|
||
return { urls, images, videos, casts, unknowns }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters