Created
October 19, 2018 17:50
-
-
Save pfeilbr/8451c43a1fbc4cd8e375647b3647cda3 to your computer and use it in GitHub Desktop.
fetches all the books history from safaribooksonline.com for the logged in user and outputs as csv string
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
// fetches all the books history from safaribooksonline.com for the logged in user | |
// and outputs as csv string | |
// see screenshot @ https://www.evernote.com/l/AAHItduomfRPHpw03KboGWL5cof7oK1CZjgB/image.png | |
// run in browser console (tested in chrome) | |
(async () => { | |
class Scraper { | |
sleep(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} | |
async fetchURLContent(url) { | |
const resp = await fetch(url) | |
return await resp.text() | |
} | |
async parseDataFromURL(url) { | |
const parser = new DOMParser() | |
const items = [] | |
const jsonString = await this.fetchURLContent(url) | |
return JSON.parse(jsonString) | |
} | |
itemsToCsv(items) { | |
const flattenedItems = items.map((e) => | |
e.titles.map((b) => ( | |
{identifier: b.identifier, title: b.title}) | |
)).flatten() | |
return flattenedItems.map((b) => `"${b.identifier}","${b.title}"`).join("\n") | |
} | |
async run() { | |
let allItems = [] | |
let moreItemsAvailable = true | |
let index = 0 | |
const incrementBy = 10 | |
const baseURL = `https://www.safaribooksonline.com/api/v1/dashboard/recent_items/full/?start=` | |
while (moreItemsAvailable) { | |
const url = `${baseURL}${index}` | |
console.log(url) | |
const items = await this.parseDataFromURL(url) | |
moreItemsAvailable = items.total > 0; | |
if (moreItemsAvailable) { | |
allItems = allItems.concat(items) | |
} | |
index += incrementBy | |
await this.sleep(500) | |
} | |
console.log(JSON.stringify(allItems, null, 2)) | |
console.log(this.itemsToCsv(allItems)) | |
return null | |
} | |
} | |
const scraper = new Scraper(); | |
await scraper.run(); | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment