UNPKG

snazzy-crawler

Version:

A snazzy light Node.js image crawler laced with TypeScript goodness! ๐Ÿ•ต๏ธ๐Ÿฆพ

80 lines (72 loc) โ€ข 1.98 kB
import chalk from 'chalk' import { writeFileSync } from 'fs' import { type CrawlerResult } from './core.js' export const DEPTH_LIMIT = 100 export function normalizeURL (url: string): string { try { const urlObj = url.includes('http') ? new URL(url) : new URL(`https://${url}`) const path: string = `${urlObj.origin}${urlObj.pathname}` if (path.length > 0 && path.slice(-1) === '/') { return path.slice(0, -1) } return path } catch (e) { throw new Error(chalk.red(`${(e as Error).message}`)) } } export function getOnlyHostURLs (urls: string[], baseURL: string): string[] { const validURLs = new Set<string>() for (const url of urls) { if (url.slice(0, 1) === '/') { try { const { origin, pathname } = new URL(`${baseURL}${url}`) validURLs.add(`${origin}${pathname}`) } catch (e) { console.log(chalk.red(`Invalid absolute url ${(e as Error).message}`)) } } else { try { const { origin, pathname } = new URL(url) validURLs.add(`${origin}${pathname}`) } catch (e) { console.log(chalk.red(`Invalid absolute url ${(e as Error).message}`)) } } } return [...validURLs].filter((url) => url.includes(baseURL)) } export function getUnique (value: any[]): any[] { return [...new Set(value)] } export function exportToJson (jsonData: CrawlerResult, baseURL: string): void { const { hostname } = new URL(baseURL) const path = `./imagesFrom[${hostname}].json` try { writeFileSync(path, JSON.stringify(jsonData, null, 2), 'utf8') console.log(chalk.green('Saved!')) } catch (error) { console.log(error) } } export function getRandomEmoji (): string { const emojies = [ '๐Ÿ˜ณ', '๐Ÿ’ฉ', '๐Ÿ‘€', '๐Ÿ‘ฝ', '๐Ÿ˜ฎ', '๐Ÿค“', '๐Ÿค ', '๐Ÿคข', '๐Ÿฅด', '๐Ÿคฎ', '๐Ÿ˜ฎ', '๐Ÿ˜’', '๐Ÿ˜', '๐Ÿคจ', '๐Ÿ˜ง' ] return emojies[Math.floor(Math.random() * emojies.length)] }