Created
June 11, 2020 16:41
-
-
Save Bestulo/7d1b6057758bfd915314eabcd8156619 to your computer and use it in GitHub Desktop.
Node.js PoliticalCompassMemes flair counting tool
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 rp = require('request-promise') | |
const fs = require('fs') | |
const baseUrl = 'https://www.reddit.com/r/PoliticalCompassMemes/.json' | |
const getPCM = (after = '') => | |
rp(baseUrl + (after ? '?after=' + after : after)).then(JSON.parse) | |
const mergeCounters = (arr1, arr2) => { | |
if (arr1 && arr2) { | |
const summedArr = arr1.map(item => { | |
const found2 = arr2.find(item2 => item2.flair === item.flair) | |
if (found2) { | |
item.counter += found2.counter | |
} | |
return item | |
}) | |
arr2.forEach(item => { | |
const foundSummed1 = summedArr.find(summedItem => item.flair === summedItem.flair) | |
if (!foundSummed1) summedArr.push(item) | |
}) | |
return summedArr | |
} else { | |
return arr1 || arr2 | |
} | |
} | |
async function countFlairs(i = 1, data, _after) { | |
const obj = await getPCM(_after) | |
const { children, after } = obj.data | |
const flairs = children.map(post => | |
post.data.author_flair_text) | |
const counters = flairs.reduce((arr, flair) => { | |
const inArr = arr.find(f => f.flair === flair) | |
if (inArr) { | |
inArr.counter++ | |
} else { | |
arr.push({flair, counter: 1}) | |
} | |
return arr | |
}, []) | |
if (i <= 25) { | |
console.log('got page ' + i) | |
return countFlairs(i + 1, data ? mergeCounters(data, counters) : counters, after) | |
} else { | |
console.log('finished on page ' + i) | |
return mergeCounters(data, counters) | |
} | |
} | |
countFlairs().then(total => { | |
fs.writeFileSync('flairCounters.json', JSON.stringify(total, null, 2)) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results 11/6/2020, 25 pages, 650 posts