This repository was archived by the owner on Mar 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreddit.js
More file actions
116 lines (116 loc) · 4.51 KB
/
Copy pathreddit.js
File metadata and controls
116 lines (116 loc) · 4.51 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const { exec } = require("child_process");
const util = require("util");
const reddioExec = util.promisify(exec);
class Reddit {
flags = [];
post_feed = [];
pos = 0;
subreddits = [];
currentId = false;
constructor(flags) {
for (let flag in flags) {
this.flags.push(flags[flag]);
}
reddioExec('reddio', (err, stdout, stderr) => {
if (err) {
console.error("An error occurred with reddio.");
console.error(err, stdout, stderr);
process.exit(1);
}
});
};
async feed(subreddits) {
this.subreddits = subreddits;
this.post_feed = [];
let res = [];
for (let i = 0; i < subreddits.length; i++) {
try {
let { stdout, stderr } = await reddioExec(`reddio print -l 20 ${subreddits[i]}`);
if (stderr) console.error("WARNING: reddio encountered an error: ", stderr);
stdout.split(/\n\s*\n/).forEach(post => { // filter empty
res.push(post);
});
} catch (err) {
console.error("WARNING: reddio encountered an error: ", err);
}
}
res = res.filter(post => post != ""); // filter empty again
res = res.sort(() => Math.random() - 0.5); // randomize order for feed
this.post_feed = res;
return res;
}
async mv(dir) {
switch (dir) {
case "next":
if (this.pos + 1 < this.post_feed.length) this.pos += 1;
else {
this.pos = 0;
await this.feed(this.subreddits);
return this.mv(dir);
}
return this.post_feed[this.pos];
case "prev":
if (this.pos - 1 >= 0) this.pos -= 1;
return this.post_feed[this.pos];
case "rand":
this.pos = Math.floor(Math.random() * this.post_feed.length);
return this.post_feed[this.pos];
default:
throw new Error("unknown mv param " + dir);
}
}
post(pos) {
if(!pos) pos = this.pos;
return this.post_feed[pos];
}
idFromPos(pos) {
if(!pos) pos = this.pos;
let postTitle = this.post_feed[pos].split(" ");
return postTitle[postTitle.length - 1];
}
async open(id) {
this.currentId = this.idFromPos(this.pos);
if(!id) id = this.currentId;
let { stdout, stderr } = await reddioExec(`reddio p -l 1 comments/${id}`);
if (stderr) console.error("WARNING: reddio encountered an error: ", stderr);
this.currentId = id;
return stdout.substring(0,stdout.indexOf("__________________________________________________")) // post only, strip comments. I use this method so that the title is still pretty.
}
async viewComments(id) {
if(!id) id = this.currentId;
let { stdout, stderr } = await reddioExec(`reddio p -l 35 comments/${id}`);
if (stderr) console.error("WARNING: reddio encountered an error: ", stderr);
return stdout;
}
async submit(subreddit,title,content) {
let { stdout, stderr } = await reddioExec(`reddio submit -t "${content}" ${subreddit} "${title}"`);
if (stderr) console.error("WARNING: reddio encountered an error: ", stderr);
return stdout;
}
async postReply(msg,id) {
if(!id) id = this.currentId;
if(id.startsWith("t3_")) { // post reply
let { stdout, stderr } = await reddioExec(`reddio comment -t "${msg}" ${id}`);
if (stderr) console.error("WARNING: reddio encountered an error: ", stderr);
}
else if(id.startsWith("t1_")) {
// replying to comment
let { stdout, stderr } = await reddioExec(`echo "${msg}" | reddio comment ${id}`);
if (stderr) console.error("WARNING: reddio encountered an error: ", stderr);
}
return await this.viewComments(id);
}
async upvote(id) {
if(!id) id = this.currentId;
let { stdout, stderr } = await reddioExec(`reddio upvote ${id}`);
if (stderr) console.error("WARNING: reddio encountered an error: ", stderr);
return stdout;
}
async downvote(id) {
if(!id) id = this.currentId;
let { stdout, stderr } = await reddioExec(`reddio downvote ${id}`);
if (stderr) console.error("WARNING: reddio encountered an error: ", stderr);
return stdout;
}
}
module.exports = { Reddit };