Skip to content

Instantly share code, notes, and snippets.

@threepointone
Created July 12, 2020 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save threepointone/95eebd82870b26de003fe3215213d96f to your computer and use it in GitHub Desktop.
Save threepointone/95eebd82870b26de003fe3215213d96f to your computer and use it in GitHub Desktop.
import "isomorphic-fetch";
import https from 'https'
import fs from 'fs'
async function get(url) {
return await (
await fetch("https://patchstorage.com/api/alpha/" + url)
).json();
}
// https://stackoverflow.com/questions/11944932/how-to-download-a-file-with-node-js-without-using-third-party-libraries/22907134#22907134
// modified to follow redirects
function download(url, dest, cb) {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(dest);
const request = https.get(url, function(response) {
if(response.statusCode === 302){
return download(response.headers.location, dest, cb)
}
response.pipe(file);
file.on('finish', function() {
file.close(cb); // close() is async, call cb after close completes.
});
}).on('error', function(err) { // Handle errors
fs.unlink(dest); // Delete the file async. (But we don't check the result)
cb(err.message);
});
})
}
async function save(url, dest){
return new Promise((resolve, reject) => {
download(url, dest, (err) => {
err ? reject(err) : resolve()
})
})
}
async function run() {
const all = []
let i = 0,
more = true;
while (true) {
console.log("page ", i + 1);
const summaries = await get(`patches/?search=zoia&offset=${i * 10}`);
if (summaries.length === 0) {
break;
}
for (let summary of summaries) {
const result = await get(`patches/${summary.id}`);
all.push(result)
await save(result.files[0].url, 'patches/' + result.files[0].filename)
}
i++;
}
}
// save details to a csv file
// expand zip files
// handle names?
// make a ui
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment