Skip to content

Instantly share code, notes, and snippets.

@Bestulo
Created June 11, 2020 16:41
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 Bestulo/7d1b6057758bfd915314eabcd8156619 to your computer and use it in GitHub Desktop.
Save Bestulo/7d1b6057758bfd915314eabcd8156619 to your computer and use it in GitHub Desktop.
Node.js PoliticalCompassMemes flair counting tool
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))
})
@Bestulo
Copy link
Author

Bestulo commented Jun 11, 2020

Results 11/6/2020, 25 pages, 650 posts

[
  {
    "flair": ":lib: - LibCenter",
    "counter": 72
  },
  {
    "flair": ":libright2: - LibRight",
    "counter": 18
  },
  {
    "flair": ":libright: - LibRight",
    "counter": 124
  },
  {
    "flair": ":centrist: - Centrist",
    "counter": 85
  },
  {
    "flair": ":right: - Right",
    "counter": 50
  },
  {
    "flair": ":libleft: - LibLeft",
    "counter": 90
  },
  {
    "flair": ":auth: - AuthCenter",
    "counter": 42
  },
  {
    "flair": ":authright: - AuthRight",
    "counter": 61
  },
  {
    "flair": ":left: - Left",
    "counter": 41
  },
  {
    "flair": ":authleft: - AuthLeft",
    "counter": 23
  },
  {
    "flair": null,
    "counter": 44
  }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment