More effcient replacement of the comment scores from reddit to pushshift

This commit is contained in:
Jesper Wrang 2018-02-19 01:01:59 +01:00
parent 7e5290f438
commit dce106a6c8
4 changed files with 17 additions and 23 deletions

View file

@ -46,7 +46,6 @@
],
"no-use-before-define": "off",
"import/first": "off",
"prefer-destructuring": "off",
"jsx-a11y/click-events-have-key-events": "off",
"jsx-a11y/no-static-element-interactions": "off"
}

View file

@ -92,6 +92,7 @@ const filterCommentTree = (comments, filterFunction) => {
}
const commentSection = (props) => {
console.time('render comment section')
const commentTree = unflatten(props.comments, props.root, props.removed, props.deleted)
if (props.show === SHOW_REMOVED_DELETED) {
@ -111,8 +112,7 @@ const commentSection = (props) => {
} else if (props.sort === SORT_OLD) {
sortCommentTree(commentTree, oldSort)
}
console.log('COMMENT SECTION RENDERED')
console.timeEnd('render comment section')
return (
<div>

View file

@ -3,16 +3,8 @@ import { getRemovedThreadIDs } from 'api/removeddit'
import { getThreads } from 'api/reddit'
import Post from 'components/Post'
const displaySubreddit = props => {
const subreddit = props.match.params.subreddit
if (subreddit === undefined) {
return 'all'
}
return subreddit
}
const getSubredditForAPI = props => {
const subreddit = props.match.params.subreddit
const { subreddit } = props.match.params
if (subreddit === undefined) {
return ''
} else if (subreddit.toLowerCase() === 'all') {
@ -45,16 +37,18 @@ export default class Subreddit extends React.Component {
}
render() {
const subreddit = `/r/${displaySubreddit(this.props)}`
const { subreddit = 'all' } = this.props.match.params
console.log(subreddit)
const subredditLink = `/r/${subreddit}`
return (
<React.Fragment>
<div className='subreddit-box'>
<a href={subreddit} className='subreddit-title'>{subreddit}</a>
<a href={subredditLink} className='subreddit-title'>{subredditLink}</a>
<span className='space' />
<a href={`https://www.reddit.com${subreddit}`} className='subreddit-title-link'>reddit</a>
<a href={`https://www.reddit.com${subredditLink}`} className='subreddit-title-link'>reddit</a>
<span className='space' />
<a href={`https://snew.github.io${subreddit}`} className='subreddit-title-link'>ceddit</a>
<a href={`https://snew.github.io${subredditLink}`} className='subreddit-title-link'>ceddit</a>
</div>
{
this.state.threads.map(thread => (

View file

@ -18,7 +18,6 @@ export default class Thread extends React.Component {
this.state = {
post: {},
pushshiftComments: [],
redditComments: [],
removed: [],
deleted: [],
loadingComments: true,
@ -27,7 +26,6 @@ export default class Thread extends React.Component {
componentDidMount() {
const { subreddit, threadID } = this.props.match.params
console.timeEnd('scripts loaded')
Promise.all([
// Get thread from reddit
@ -49,18 +47,22 @@ export default class Thread extends React.Component {
.then(results => {
const pushshiftComments = results[1]
// Extract ids from pushshift response
const ids = pushshiftComments.map(comment => comment.id)
console.log('pushshift:', ids.length)
console.log('Number of comments from Pushshift:', ids.length)
// Get all the comments from reddit
return (
getRedditComments(ids)
.then(redditComments => {
const redditCommentLookup = {}
redditComments.forEach(comment => {
redditCommentLookup[comment.id] = comment
})
pushshiftComments.forEach(comment => {
// Replace pushshift score with reddit (its usually more accurate)
const redditComment = redditComments.find(rComment => rComment.id === comment.id)
const redditComment = redditCommentLookup[comment.id]
if (redditComment !== undefined) {
comment.score = redditComment.score
}
@ -72,7 +74,7 @@ export default class Thread extends React.Component {
)
})
.then(redditComments => {
console.log('reddit:', redditComments.length)
console.log('Number of comments from Reddit:', redditComments.length)
const removed = []
const deleted = []
@ -88,7 +90,6 @@ export default class Thread extends React.Component {
this.setState({
removed,
deleted,
redditComments,
loadingComments: false,
})
})