add raw JSON parameter for teddit api (#220)

This commit is contained in:
teddit 2021-07-28 22:05:42 +02:00
parent bf70b63b51
commit 795aa70aa1
1 changed files with 21 additions and 15 deletions

View File

@ -697,14 +697,16 @@ module.exports = (app, redis, fetch, RedditAPI) => {
else else
api_req = false api_req = false
let key = `/after:${after}:before:${before}:sort:${sortby}:past:${past}` let raw_json = (api_req && req.query.raw_json == '1' ? 1 : 0)
let key = `/after:${after}:before:${before}:sort:${sortby}:past:${past}:raw_json:${raw_json}`
let subbed_subreddits = req.cookies.subbed_subreddits let subbed_subreddits = req.cookies.subbed_subreddits
let get_subbed_subreddits = false let get_subbed_subreddits = false
if(subbed_subreddits && Array.isArray(subbed_subreddits)) { if(subbed_subreddits && Array.isArray(subbed_subreddits)) {
get_subbed_subreddits = true get_subbed_subreddits = true
subbed_subreddits = subbed_subreddits.join('+') subbed_subreddits = subbed_subreddits.join('+')
key = `${subbed_subreddits.toLowerCase()}:${after}:${before}:sort:${sortby}:past:${past}` key = `${subbed_subreddits.toLowerCase()}:${after}:${before}:sort:${sortby}:past:${past}:raw_json:${raw_json}`
} }
redis.get(key, (error, json) => { redis.get(key, (error, json) => {
@ -732,14 +734,14 @@ module.exports = (app, redis, fetch, RedditAPI) => {
let url = '' let url = ''
if(config.use_reddit_oauth) { if(config.use_reddit_oauth) {
if(get_subbed_subreddits) if(get_subbed_subreddits)
url = `https://oauth.reddit.com/r/${subbed_subreddits}/${sortby}?api_type=json&count=25&g=GLOBAL&t=${past}${d}` url = `https://oauth.reddit.com/r/${subbed_subreddits}/${sortby}?api_type=json&count=25&g=GLOBAL&t=${past}${d}&raw_json=${raw_json}`
else else
url = `https://oauth.reddit.com/${sortby}?api_type=json&g=GLOBAL&t=${past}${d}` url = `https://oauth.reddit.com/${sortby}?api_type=json&g=GLOBAL&t=${past}${d}&raw_json=${raw_json}`
} else { } else {
if(get_subbed_subreddits) if(get_subbed_subreddits)
url = `https://reddit.com/r/${subbed_subreddits}/${sortby}.json?api_type=json&count=25&g=GLOBAL&t=${past}${d}` url = `https://reddit.com/r/${subbed_subreddits}/${sortby}.json?api_type=json&count=25&g=GLOBAL&t=${past}${d}&raw_json=${raw_json}`
else else
url = `https://reddit.com/${sortby}.json?g=GLOBAL&t=${past}${d}` url = `https://reddit.com/${sortby}.json?g=GLOBAL&t=${past}${d}&raw_json=${raw_json}`
} }
fetch(encodeURI(url), redditApiGETHeaders()) fetch(encodeURI(url), redditApiGETHeaders())
.then(result => { .then(result => {
@ -1159,6 +1161,8 @@ module.exports = (app, redis, fetch, RedditAPI) => {
else else
api_req = false api_req = false
let raw_json = (api_req && req.query.raw_json == '1' ? 1 : 0)
let d = `&after=${after}` let d = `&after=${after}`
if(before) { if(before) {
d = `&before=${before}` d = `&before=${before}`
@ -1188,7 +1192,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
} }
} }
let key = `${subreddit.toLowerCase()}:${after}:${before}:sort:${sortby}:past:${past}` let key = `${subreddit.toLowerCase()}:${after}:${before}:sort:${sortby}:past:${past}:raw_json:${raw_json}`
redis.get(key, (error, json) => { redis.get(key, (error, json) => {
if(error) { if(error) {
console.error(`Error getting the ${subreddit} key from redis.`, error) console.error(`Error getting the ${subreddit} key from redis.`, error)
@ -1229,9 +1233,9 @@ module.exports = (app, redis, fetch, RedditAPI) => {
} else { } else {
let url = '' let url = ''
if(config.use_reddit_oauth) if(config.use_reddit_oauth)
url = `https://oauth.reddit.com/r/${subreddit}/${sortby}?api_type=json&count=25&g=GLOBAL&t=${past}${d}` url = `https://oauth.reddit.com/r/${subreddit}/${sortby}?api_type=json&count=25&g=GLOBAL&t=${past}${d}&raw_json=${raw_json}`
else else
url = `https://reddit.com/r/${subreddit}/${sortby}.json?api_type=json&count=25&g=GLOBAL&t=${past}${d}` url = `https://reddit.com/r/${subreddit}/${sortby}.json?api_type=json&count=25&g=GLOBAL&t=${past}${d}&raw_json=${raw_json}`
fetch(encodeURI(url), redditApiGETHeaders()) fetch(encodeURI(url), redditApiGETHeaders())
.then(result => { .then(result => {
if(result.status === 200) { if(result.status === 200) {
@ -1454,6 +1458,8 @@ module.exports = (app, redis, fetch, RedditAPI) => {
else else
api_req = false api_req = false
let raw_json = (api_req && req.query.raw_json == '1' ? 1 : 0)
if(!after) { if(!after) {
after = '' after = ''
} }
@ -1507,7 +1513,7 @@ module.exports = (app, redis, fetch, RedditAPI) => {
} }
} }
let key = `${user}:${after}:${before}:sort:${sortby}:past:${past}:post_type:${post_type}` let key = `${user}:${after}:${before}:sort:${sortby}:past:${past}:post_type:${post_type}:raw_json:${raw_json}`
redis.get(key, (error, json) => { redis.get(key, (error, json) => {
if(error) { if(error) {
console.error(`Error getting the user ${key} key from redis.`, error) console.error(`Error getting the user ${key} key from redis.`, error)
@ -1531,9 +1537,9 @@ module.exports = (app, redis, fetch, RedditAPI) => {
} else { } else {
let url = '' let url = ''
if(config.use_reddit_oauth) if(config.use_reddit_oauth)
url = `https://oauth.reddit.com/user/${user}/about` url = `https://oauth.reddit.com/user/${user}/about?raw_json=${raw_json}`
else else
url = `https://reddit.com/user/${user}/about.json` url = `https://reddit.com/user/${user}/about.json?raw_json=${raw_json}`
fetch(encodeURI(url), redditApiGETHeaders()) fetch(encodeURI(url), redditApiGETHeaders())
.then(result => { .then(result => {
if(result.status === 200) { if(result.status === 200) {
@ -1545,9 +1551,9 @@ module.exports = (app, redis, fetch, RedditAPI) => {
let endpoint = '/overview' let endpoint = '/overview'
if(post_type !== '') if(post_type !== '')
endpoint = post_type endpoint = post_type
url = `https://oauth.reddit.com/user/${user}${post_type}?limit=26${d}&sort=${sortby}&t=${past}` url = `https://oauth.reddit.com/user/${user}${post_type}?limit=26${d}&sort=${sortby}&t=${past}&raw_json=${raw_json}`
} else { } else {
url = `https://reddit.com/user/${user}${post_type}.json?limit=26${d}&sort=${sortby}&t=${past}` url = `https://reddit.com/user/${user}${post_type}.json?limit=26${d}&sort=${sortby}&t=${past}&raw_json=${raw_json}`
} }
fetch(encodeURI(url), redditApiGETHeaders()) fetch(encodeURI(url), redditApiGETHeaders())
.then(result => { .then(result => {