removeddit/src/api/pushshift/index.js

53 lines
1.6 KiB
JavaScript
Raw Normal View History

import { fetchJson } from '../../utils'
const chunkSize = 100;
const postURL = 'https://api.pushshift.io/reddit/submission/search/?ids='
const commentURL = `https://api.pushshift.io/reddit/comment/search/?size=${chunkSize}&sort=asc&fields=author,body,created_utc,id,link_id,parent_id,retrieved_on,retrieved_utc,score,subreddit&q=*&link_id=`
2018-01-14 13:41:00 +00:00
const sleep = ms =>
new Promise(slept => setTimeout(slept, ms))
2018-01-13 17:20:08 +00:00
export const getPost = async threadID => {
try {
return (await fetchJson(`${postURL}${threadID}`)).data[0]
} catch (error) {
console.error('pushshift.getPost: ' + error)
throw new Error('Could not get removed post')
}
}
2018-01-14 19:21:53 +00:00
export const getComments = async (threadID, maxComments) => {
let chunks = Math.ceil(maxComments / chunkSize)
let after = 0, delay = 0, comments
const allComments = new Map()
while (true) {
2018-02-06 03:07:30 +00:00
while (true) {
try {
comments = (await fetchJson(`${commentURL}${threadID}&after=${after}`)).data
break
} catch (error) {
if (delay > 4000) {
console.error('pushshift.getComments: ' + error)
throw new Error('Could not get removed comments')
}
delay = delay * 2 || 500
}
await sleep(delay)
}
comments.forEach(c => allComments.set(c.id, {
...c,
parent_id: c.parent_id?.substring(3) || threadID,
link_id: c.link_id?.substring(3) || threadID
}))
if (comments.length < chunkSize/2 || chunks <= 1)
break
chunks -= 1
after = Math.max(comments[comments.length - 1].created_utc - 1, after + 1)
if (delay)
await sleep(delay)
}
return allComments
}