Simplified mutiple parts of the api module to reddit/pushshift

This commit is contained in:
JubbeArt 2018-07-15 01:13:31 +02:00
parent 9031a717f7
commit 2f40c15c7f
10 changed files with 78 additions and 116 deletions

View file

@ -1,4 +1,4 @@
import { json, toBase10, toBase36 } from 'utils'
import { toBase10, toBase36 } from 'utils'
const baseURL = 'https://elastic.pushshift.io'
const postURL = `${baseURL}/rs/submissions/_search?source=`
@ -15,9 +15,9 @@ export const getPost = threadID => {
return (
window.fetch(postURL + JSON.stringify(elasticQuery))
.then(json)
.then(jsonData => jsonData.hits.hits[0]._source)
.then(post => {
.then(response => response.json())
.then(response => {
const post = response.hits.hits[0]._source
post.id = toBase36(post.id)
return post
})
@ -52,20 +52,22 @@ export const getComments = threadID => {
return (
window.fetch(commentURL + JSON.stringify(elasticQuery))
.then(json)
.then(jsonData => jsonData.hits.hits)
.then(comments => comments.map(comment => {
comment._source.id = toBase36(comment._id)
comment._source.link_id = toBase36(comment._source.link_id)
.then(response => response.json())
.then(response => {
const comments = response.hits.hits
return comments.map(comment => {
comment._source.id = toBase36(comment._id)
comment._source.link_id = toBase36(comment._source.link_id)
// Missing parent id === direct reply to thread
if (!comment._source.parent_id) {
comment._source.parent_id = threadID
} else {
comment._source.parent_id = toBase36(comment._source.parent_id)
}
// Missing parent id === direct reply to thread
if (!comment._source.parent_id) {
comment._source.parent_id = threadID
} else {
comment._source.parent_id = toBase36(comment._source.parent_id)
}
return comment._source
}))
return comment._source
})
})
)
}

View file

@ -1,11 +1,42 @@
import { getToken } from './token'
// Change this to your own client ID: https://www.reddit.com/prefs/apps
// The app NEEDS TO BE an installed app and NOT a web apps
// Header for general api calls
export const getAuth = () => (
getToken()
// Current using dummy ID from throwaway
const clientID = '33W8M1OOxPv80A'
// Token for reddit API
let token
const getToken = () => {
// We have already gotten a token
if (token !== undefined) {
return Promise.resolve(token)
}
// Headers for getting reddit api token
const tokenInit = {
headers: {
Authorization: `Basic ${window.btoa(`${clientID}:`)}`,
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
},
method: 'POST',
body: `grant_type=${encodeURIComponent('https://oauth.reddit.com/grants/installed_client')}&device_id=DO_NOT_TRACK_THIS_DEVICE`
}
return window.fetch('https://www.reddit.com/api/v1/access_token', tokenInit)
.then(response => response.json())
.then(response => {
token = response.access_token
return token
})
}
// Get header for general api calls
export const getAuth = () => {
return getToken()
.then(token => ({
headers: {
Authorization: `bearer ${token}`
}
}))
)
}

View file

@ -1,5 +0,0 @@
// Change this to your own client ID: https://www.reddit.com/prefs/apps
// The app NEEDS TO BE an installed app and NOT a web apps
// Current using dummy ID from throwaway
export default '33W8M1OOxPv80A'

View file

@ -1,16 +1,18 @@
import { chunk, flatten, json } from 'utils'
import { chunk, flatten } from '../../utils'
import { getAuth } from './auth'
export const getComments = commentIDs => (
getAuth()
export const getComments = commentIDs => {
return getAuth()
.then(auth => (
Promise.all(chunk(commentIDs, 100).map(ids => fetchComments(ids, auth))).then(flatten)
Promise.all(chunk(commentIDs, 100)
.map(ids => fetchComments(ids, auth)))
.then(flatten)
))
)
}
export const fetchComments = (commentIDs, auth) => (
window.fetch(`https://oauth.reddit.com/api/info?id=${commentIDs.map(id => `t1_${id}`).join()}`, auth)
.then(json)
export const fetchComments = (commentIDs, auth) => {
return window.fetch(`https://oauth.reddit.com/api/info?id=${commentIDs.map(id => `t1_${id}`).join()}`, auth)
.then(response => response.json())
.then(results => results.data.children)
.then(commentsData => commentsData.map(commentData => commentData.data))
)
}

View file

@ -1,54 +1,18 @@
import { json } from 'utils'
import { getAuth } from './auth'
const cachedThreads = {}
// Thread = Post + Comments
// Return the post itself
export const getPost = (subreddit, threadID) => (
getThread(subreddit, threadID)
getAuth()
.then(auth => window.fetch(`https://oauth.reddit.com/r/${subreddit}/comments/${threadID}/_/`, auth))
.then(response => response.json())
.then(thread => thread[0].data.children[0].data)
)
export const getThread = (subreddit, threadID, commentID = '') => {
// We have already downloaded the thread and can use a cached copy
if (cachedThreads.hasOwnProperty(threadID)) {
if (cachedThreads[threadID].hasOwnProperty(commentID)) {
return Promise.resolve(cachedThreads[threadID][commentID])
}
}
const url = `https://oauth.reddit.com/r/${subreddit}/comments/${threadID}/_/${commentID}`
// Fetch thread from reddit
return (
getAuth()
.then(auth => window.fetch(url, auth))
.then(json)
.then(thread => {
// Create cache object for thread if it doesn't exists
if (!cachedThreads.hasOwnProperty(threadID)) {
cachedThreads[threadID] = {}
}
// Save the thread for later
cachedThreads[threadID][commentID] = thread
// Return the thread
return thread
})
)
}
// Fetch multiple threads (via the info endpoint)
export const getThreads = threadIDs => {
const threadString = threadIDs.map(id => `t3_${id}`).join()
return (
getAuth()
.then(auth => window.fetch(`https://oauth.reddit.com/api/info?id=${threadString}`, auth))
.then(json)
.then(response => {
const threads = response.data.children
return threads.map(threadData => threadData.data)
})
)
return getAuth()
.then(auth => window.fetch(`https://oauth.reddit.com/api/info?id=${threadIDs.map(id => `t3_${id}`).join()}`, auth))
.then(response => response.json())
.then(response => response.data.children.map(threadData => threadData.data))
}

View file

@ -1,30 +0,0 @@
import { json } from 'utils'
import clientID from './clientID'
// Token for reddit API
let token = null
// Headers for getting reddit api token
const tokenInit = {
headers: {
Authorization: `Basic ${window.btoa(`${clientID}:`)}`,
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
},
method: 'POST',
body: `grant_type=${encodeURIComponent('https://oauth.reddit.com/grants/installed_client')}&device_id=DO_NOT_TRACK_THIS_DEVICE`
}
export const getToken = () => {
if (token !== null) {
return Promise.resolve(token)
}
return (
window.fetch('https://www.reddit.com/api/v1/access_token', tokenInit)
.then(json)
.then(response => {
token = response.access_token
return token
})
)
}

View file

@ -1,8 +1,6 @@
import { json } from 'utils'
const baseURL = 'https://removeddit.com/api'
export const getRemovedThreadIDs = (subreddit = '', page = 1) => (
window.fetch(`${baseURL}/threads?subreddit=${subreddit}&page=${page - 1}`)
.then(json)
)
export const getRemovedThreadIDs = (subreddit = '', page = 1) => {
return window.fetch(`${baseURL}/threads?subreddit=${subreddit}&page=${page - 1}`)
.then(response => response.json())
}