Skip to content

Instantly share code, notes, and snippets.

@alanrsoares
Created May 27, 2025 02:30
Show Gist options
  • Select an option

  • Save alanrsoares/2a8eb5688c32e611b9757071a2d5f027 to your computer and use it in GitHub Desktop.

Select an option

Save alanrsoares/2a8eb5688c32e611b9757071a2d5f027 to your computer and use it in GitHub Desktop.

Revisions

  1. alanrsoares created this gist May 27, 2025.
    70 changes: 70 additions & 0 deletions download-svgrepo-collection.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    import { writeFile, mkdir } from "fs/promises";
    import { join } from "path";
    import { load } from "cheerio"; // install with `bun add cheerio`

    const COLLECTION_URLS = [
    "https://www.svgrepo.com/collection/finance-6/",
    "https://www.svgrepo.com/collection/finance-6/2",
    ];
    const OUTPUT_DIR = "./public/vectors";

    async function fetchHTML(url: string): Promise<string> {
    const res = await fetch(url);
    if (!res.ok) throw new Error(`Failed to fetch ${url}`);
    return await res.text();
    }

    interface SVGItem {
    url: string;
    name: string;
    }

    function extractSVGsFromHTML(html: string): SVGItem[] {
    const $ = load(html);
    const vectors: SVGItem[] = [];

    $('img[itemprop="contentUrl"]').each((_, el) => {
    const url = $(el).attr("src");
    const alt = $(el).attr("alt") ?? "icon";
    if (url) {
    const name =
    alt
    .replace(/[^a-z0-9_-]/gi, "_")
    .toLowerCase()
    .replace(/_svg_file$/, "") + ".svg";
    vectors.push({ url, name });
    }
    });

    return vectors;
    }

    async function downloadSVG(url: string, filename: string) {
    const res = await fetch(url);
    if (!res.ok) throw new Error(`Failed to download ${url}`);
    const svg = await res.text();
    await writeFile(join(OUTPUT_DIR, filename), svg);
    console.log(`✅ Saved ${filename}`);
    }

    async function run() {
    await mkdir(OUTPUT_DIR, { recursive: true });

    const allIcons = [] as SVGItem[];

    for (const url of COLLECTION_URLS) {
    const html = await fetchHTML(url);
    const icons = extractSVGsFromHTML(html);
    allIcons.push(...icons);
    }

    for (const { url, name } of allIcons) {
    try {
    await downloadSVG(url, name);
    } catch (err) {
    console.error(`❌ Failed to save ${url}:`, err);
    }
    }
    }

    run();