Back to Library
Scrape Github
js • 1/26/2026
6
const fetch = require("node-fetch");
async function github(url) {
const regex = /(?:https|git)(?::\/\/|@)github\.com[\/:]([^\/:]+)\/(.+)/i;
const match = url.match(regex);
if (!match) throw new Error("Invalid GitHub URL");
const user = match[1];
const repo = match[2].replace(/\.git$/, "");
const apiUrl = `https://api.github.com/repos/${user}/${repo}/zipball`;
const head = await fetch(apiUrl, { method: "HEAD" });
if (!head.ok) throw new Error("Failed to fetch repository");
let disposition = head.headers.get("content-disposition");
let filename = "repository.zip";
if (disposition) {
const m = disposition.match(/attachment; filename=(.*)/);
if (m && m[1]) filename = m[1];
}
return {
user,
repo,
url: apiUrl,
filename: filename
};
}
module.exports = { github };