-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
75 lines (67 loc) · 1.73 KB
/
router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const { response } = require('express');
var express = require('express');
var router = express.Router();
const snoowrap = require('snoowrap');
const r = new snoowrap({
userAgent: 'zach"s reddit',
clientId: 'FTEzEhIr0XB_mw',
clientSecret: 'lpySAc_TvUA8VT2JaiVTRMSIbf7uiw',
refreshToken: '12899792823-eZJoRdm5HDdF8y-XlKszVLOgVpsofw'
});
var subreddits = subreddits = ['aww', 'eyebleach', 'wholesomememes', 'blessedimages', 'wholesome', 'animalsthatlovemagic']
var currentPostIndex = 0
var s = subreddits.join('+')
var p = Promise.resolve(r.getSubreddit(s).getTop({
limit: 10,
time: 'day'
}));
p.then(function(posts) {
function getMore() {
posts.fetchMore({amount: 10}).then(extendedPosts => {
posts = extendedPosts
})
}
router.post('/', function(req, res, next) {
const holdIndex = currentPostIndex
currentPostIndex += 1;
if (currentPostIndex >= posts.length - 3) {
getMore();
}
const post = parsePost(posts[holdIndex])
return res.json(post)
})
})
router.get('/', function(req, res, next) {
return res.sendFile('index.html', {
root: __dirname
})
});
function parsePost(post) {
let ret = {}
if (post.url.includes("v.redd")) {
ret = {
'title': post.title,
'url': post.url,
'is_video': true,
'plink': post.permalink,
'v_url': post.media.reddit_video.fallback_url
}
} else if (post.url.includes("gfycat")) {
ret = {
'title': post.title,
'url': post.url,
'plink': post.permalink,
'gfy': post.media.oembed.html,
'is_video': false
}
} else {
ret = {
'title': post.title,
'url': post.url,
'plink': post.permalink,
'is_video': false
}
}
return ret
}
module.exports = router;