-
Notifications
You must be signed in to change notification settings - Fork 1
/
redArc.js
83 lines (68 loc) · 2.96 KB
/
redArc.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
// SN4T14 2014-11-28
// License: WTFPL
// jshint node: true
'use strict'; // Be strict like my math teacher
var redditStream = require('reddit-stream');
var linkHandler = require('./lib/linkHandler.js'); // Outsource all the boring downloading stuff to another file to keep this one nicer
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
var util = require('util');
var moment = require('moment');
var postStream = new redditStream('posts', 'all', 'RedArc.js/0.3.0 by /u/SN4T14'); // Monitor posts on /r/all because keeping up with it is possible, and lets you check all users every 2 seconds instead of having to spend 2*numUsers seconds manually looping through all users.
var usersToArchive = []; // Don't manually add users to this, add them to users.txt!
var subredditsToArchive = []; // Don't manually add subreddits to this, add them to subreddits.txt!
var NoFileError = {code: "ENOENT"};
function savePost (postData) {
var folderName = postData.over_18 ? "NSFW" : "SFW"; // Save to "./username/NSFW/" or "./username/SFW/" to separate NSFW from SFW pictures (e.g. pics of their puppy)
linkHandler.saveLink(postData.url, "./posts/" + postData.author + "/" + folderName + "/");
}
function addUser (user) {
fs.appendFile('users.txt', user, function (err) {
if (err) {
throw err;
}
});
usersToArchive.push(user);
}
postStream.on('new', function(posts) {
posts.forEach(function (post) {
var save = false;
if (subredditsToArchive.indexOf("all") !== -1) {
save = true;
} else if (usersToArchive.indexOf(post.data.author) !== -1) {
save = true;
} else if (subredditsToArchive.indexOf(post.data.subreddit) !== -1) { // else if to avoid adding users that already exist in usersToArchive
addUser(post.data.author);
save = true;
}
// why do it like this? because when you add more clauses later on, it's still just as easy to read and maintain.
if (save) {
console.log("[" + moment().format() + "]", "Found post!", post.data.url, post.data.author);
savePost(post.data);
}
});
});
postStream.on('error', function(err) {
if (err.error !== 504) {
console.error(err.message);
console.error(err.response);
console.error(err.error);
console.error("If the problem persists, please copy the above output and paste it in an issue on GitHub");
}
});
Promise.try(function() {
return fs.readFileAsync('users.txt');
}).then(function (data) {
usersToArchive = data.toString().split("\n");
return fs.readFileAsync('subreddits.txt');
}).then(function (data) {
subredditsToArchive = data.toString().split("\n");
if (usersToArchive.length === 0 && subredditsToArchive.length === 0) {
console.log("Both users.txt and subreddits.txt are empty");
}
postStream.start();
console.log("RedArc.js started!");
}).catch(NoFileError, function (err) {
fs.appendFile(err.path, "");
console.log("Created", err.path + ", you might want to put something in there");
});