-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
95 lines (82 loc) · 3.02 KB
/
app.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const snooper = require('reddit-snooper')
const chokidar = require('chokidar')
const request = require('request')
const fs = require('fs')
const imgur = require('imgur')
const axios = require('axios')
require('dotenv').config()
// Set global vars
let author
let imageTitle
let permalink
let imageId
let postUrl
let albumId = process.env.IMGUR_ALBUM_ID
imgur.setCredentials(
process.env.IMGUR_USERNAME,
process.env.IMGUR_PASSWORD,
process.env.IMGUR_CLIENT_ID
)
Snooper = new snooper({
username: process.env.REDDIT_USERNAME,
password: process.env.REDDIT_PASSWORD,
app_id: process.env.REDDIT_CLIENT_ID,
api_secret: process.env.REDDIT_CLIENT_SECRET,
user_agent: process.env.REDDIT_USER_AGENT,
automatic_retries: true, // automatically handles condition when reddit says 'you are doing this too much'
api_requests_per_minuite: 60 // api requests will be spread out in order to play nicely with Reddit
})
Snooper.watcher.getPostWatcher('AmoledBackgrounds') // blank argument or 'all' looks at the entire website
.on('post', function(post) {
// comment is a object containing all comment data
console.log(post)
console.log('posted by: ' + post.data.author)
let urlMatch = post.data.url.match('\.([a-zA-Z]+$)')
// Check to make sure the post is not stickied, that it's an image post, and that it contains a link
if (!post.data.stickied && post.kind === 't3' && urlMatch) {
// Grab the author, title, and URL
author = post.data.author
imageTitle = post.data.title
permalink = post.data.permalink
// Download the new post
console.log('New post detected')
request(post.data.url).pipe(fs.createWriteStream("./pics/newestPost.jpg"))
}
})
.on('error', console.error)
// event listener to watch for new files in pics dir
chokidar.watch('./pics/newestPost.jpg', {
ignored: /(^|[\/\\])\../,
usepolling: true,
interval: 10000,
awaitWriteFinish: {
stabilityThreshold: 10000,
pollInterval: 10000
}}).on('change', (event, path) => {
console.log(event);
// read the media
var b64content = fs.readFileSync('./pics/newestPost.jpg', { encoding: 'base64' })
let description = `New post from /r/AmoledBackgrounds from Reddit user /u/${author}. Upvote the post here: https://www.reddit.com${permalink} #wallpapers #backgrounds #amoled`
imgur.uploadBase64(b64content, null, imageTitle, description)
.then(function (json) {
imageId = json.data.id
axios({
method: 'post',
url: `https://api.imgur.com/3/gallery/image/${imageId}`,
headers: {
'Authorization': `Bearer ${process.env.IMGUR_ACCESS_TOKEN}`
},
data: {
title: imageTitle,
tags: 'amoled, wallpapers, backgrounds'
}
}).then((response) => {
console.log(response);
}, (error) => {
console.log(error);
});
})
.catch(function (err) {
console.error(err.message);
});
})