-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
34 lines (28 loc) · 918 Bytes
/
Copy pathindex.js
File metadata and controls
34 lines (28 loc) · 918 Bytes
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
import snoostorm from 'snoostorm';
import snoowrap from 'snoowrap';
import credentials from './credentials.json';
import request from 'request';
import fs from 'fs';
// Check for images dir
const dir = './images';
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
const client = new snoostorm(new snoowrap(credentials));
const submissionStream = client.SubmissionStream({
'subreddit': 'hmmm',
'results': 5
});
submissionStream.on("submission", (post) => {
console.log(`New post! ${post.id}`);
request.head(post.url, function(err, res, body){
request(post.url).pipe(fs.createWriteStream(`${dir}/${post.title}_${post.id}.png`)).on('close', ()=>{console.log(`Downloaded ${post.id}`)});
});
});
process.on('SIGINT', function() {
console.log('Shutting down, this will take a second...');
submissionStream.emit('stop');
setTimeout(()=> {
process.exit();
}, 5000);
});