2021-11-03 20:48:18 +00:00
|
|
|
import { fetchJson } from '../../utils'
|
|
|
|
|
|
2021-10-15 20:59:57 +00:00
|
|
|
const chunkSize = 100;
|
|
|
|
|
const postURL = 'https://api.pushshift.io/reddit/submission/search/?ids='
|
2021-10-23 16:58:06 +00:00
|
|
|
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
|
|
|
|
2021-10-15 20:59:57 +00:00
|
|
|
const sleep = ms =>
|
|
|
|
|
new Promise(slept => setTimeout(slept, ms))
|
2018-01-13 17:20:08 +00:00
|
|
|
|
2022-02-17 01:32:24 +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
|
|
|
|
2022-02-17 01:32:24 +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
|
|
|
|
2022-02-17 01:32:24 +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)
|
|
|
|
|
}
|
2021-12-01 18:06:01 +00:00
|
|
|
|
2022-02-17 01:32:24 +00:00
|
|
|
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
|
|
|
|
|
}
|