diff options
| author | Joe Mou <dev@mou.fo> | 2026-01-22 13:27:36 -0500 |
|---|---|---|
| committer | Joe Mou <dev@mou.fo> | 2026-01-23 00:54:48 -0500 |
| commit | d0dccc7a910a579d8c172c3d5a5a02ea3abd964d (patch) | |
| tree | d10be3141653130f6e89cbdf0181c2e1ec07dd82 /cgithub/github-scraper.js | |
| parent | 5e45f900974589c8eead15fc352d50dc45b1a25a (diff) | |
Simple web server for viewing trees
Diffstat (limited to 'cgithub/github-scraper.js')
| -rwxr-xr-x | cgithub/github-scraper.js | 74 |
1 files changed, 0 insertions, 74 deletions
diff --git a/cgithub/github-scraper.js b/cgithub/github-scraper.js deleted file mode 100755 index 7e07adc..0000000 --- a/cgithub/github-scraper.js +++ /dev/null @@ -1,74 +0,0 @@ -#!/usr/bin/env node -/** - * GitHub Directory Listing Scraper - * - * Gets directory listings from GitHub without using the API (no rate limits!) - * Works by parsing the server-rendered HTML that GitHub sends to browsers. - */ - -async function getGitHubDirectory(owner, repo, path = '') { - // Always use the tree URL pattern, even for root directory - const url = `https://github.com/${owner}/${repo}/tree/main/${path}`; - - console.error(`Fetching: ${url}`); - - const response = await fetch(url); - const html = await response.text(); - - // GitHub embeds the directory data in a JSON script tag - // Using the tree (subdirectory) pattern for all cases - const subdirMatch = html.match(/<script type="application\/json" data-target="react-app\.embeddedData">([^<]+)<\/script>/); - - if (!subdirMatch) { - throw new Error('Could not find embedded data in HTML'); - } - - const data = JSON.parse(subdirMatch[1]); - const payload = data.payload; - - return { - path: payload.path, - branch: payload.refInfo.name, - items: payload.tree.items, - repo: { - name: payload.repo.name, - owner: payload.repo.ownerLogin, - isPublic: payload.repo.public - } - }; -} - -// Example usage -if (require.main === module) { - const [owner, repo, path] = process.argv.slice(2); - - if (!owner || !repo) { - console.log('Usage: node github-scraper.js <owner> <repo> [path]'); - console.log('Example: node github-scraper.js torvalds linux'); - console.log('Example: node github-scraper.js torvalds linux Documentation'); - process.exit(1); - } - - getGitHubDirectory(owner, repo, path) - .then(result => { - console.log('\nš Directory listing'); - console.log(`Repository: ${result.repo.owner}/${result.repo.name}`); - console.log(`Branch: ${result.branch}`); - console.log(`Path: ${result.path}`); - console.log(`Items: ${result.items.length}`); - console.log('\nContents:'); - - result.items.forEach(item => { - const icon = item.contentType === 'directory' ? 'š' : 'š'; - console.log(` ${icon} ${item.name}`); - }); - - console.log('\nā No API rate limits - this scrapes the HTML!'); - }) - .catch(err => { - console.error('Error:', err.message); - process.exit(1); - }); -} - -module.exports = { getGitHubDirectory }; |
