Created
November 7, 2017 15:43
-
-
Save mpj/ed3b57c6c1f2c65b51587d1b73483cf8 to your computer and use it in GitHub Desktop.
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('node-fetch') | |
const fs = require('fs') | |
require('dotenv').config(); | |
const accessToken = process.env.PATREON_ACCESS_TOKEN | |
const pageCount = 100 | |
const url = `https://www.patreon.com/api/oauth2/api/campaigns/1137737/pledges` + | |
`?type=pledge&sort=created&page%5Bcount%5D=${pageCount}&access_token=${accessToken}` | |
const getPatreonData = function (url, pledges = []) { | |
console.log(`Fetching: ${url}`) | |
return fetch(url) | |
.then(response => response.json()) | |
.then(function(body) { | |
if (body.data && body.data.length) pledges = pledges.concat(body.data) | |
return body.links && body.links.next | |
? getPatreonData(`${body.links.next}&type=pledge&access_token=${accessToken}`, pledges) | |
: pledges | |
}) | |
} | |
getPatreonData(url, pledges => { | |
const filename = `pledges-${pledges.length}.json` | |
fs.writeFile(filename, JSON.stringify(pledges, null, 2), 'utf8', () => console.log(`We're done folks! Saved date in ${filename}.`)) | |
}) | |
.catch(function(error) { | |
console.error(error) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment