Last active
October 3, 2021 19:08
-
-
Save djdembeck/02ffe09b875726067728d74661845104 to your computer and use it in GitHub Desktop.
Collect and log all Audible asins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require('isomorphic-fetch'); | |
const url = 'https://api.audible.com/1.0/catalog/products/?num_results=50' | |
const delay = ms => new Promise(resolve => setTimeout(resolve, ms)) | |
// Best to pipe out the results, like `node file.js > asins.txt` | |
function makeGetRequest(path) { | |
return new Promise(function (resolve, reject) { | |
fetch(path).then( | |
(response) => { | |
var result = response.json(); | |
resolve(result); | |
}, | |
(error) => { | |
reject(error); | |
} | |
); | |
}); | |
} | |
async function getProduct() { | |
let pageCount = await makeGetRequest(url) | |
total_results = Math.ceil(pageCount.total_results / 50) | |
for (let i = 1; i < total_results; i++) { | |
const pageRequest = async () => { | |
return makeGetRequest(url + `&page=${i}`).then((response) => { | |
return response | |
}) | |
} | |
page = await pageRequest() | |
page['products'].forEach(async (element) => { | |
console.log(`${element['asin']}`) | |
}); | |
// Delay 1s to not get rate limited | |
await delay(1000) | |
} | |
} | |
getProduct() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment