Skip to content

Commit

Permalink
Adding basic subreddit-denylist functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ouifi committed Apr 20, 2021
1 parent 1ef2174 commit 8d2401e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 33 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Create a `.env` file with the following values from your own reddit bot account.
```
REDDIT_CLIENT_SECRET=<secret>
REDDIT_CLIENT_ID=<id>
REDDIT_USERNAME=<username>
NODE_ENV={ development | production }
LOG_LEVEL={ NONE | ERROR | WARN | LOG | [DEBUG] | VERBOSE }
```
Expand Down
7 changes: 0 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ if (process.env.NODE_ENV !== "production") {
});
}

Reddit.initialize(
// process.env.REDDIT_USERNAME,
// process.env.REDDIT_PASSWORD,
process.env.REDDIT_CLIENT_ID,
process.env.REDDIT_CLIENT_SECRET
);

Reddit.connect().then(() => {
Server.listen(process.env.PORT || 9999, "0.0.0.0", function (err, address) {
if (err) {
Expand Down
38 changes: 18 additions & 20 deletions src/lib/reddit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,15 @@ const url = require("url");
const { Logger } = require("../lib/log");

const creds = {
username: null,
password: null,
id: null,
secret: null
}
id: process.env.REDDIT_CLIENT_ID,
secret: process.env.REDDIT_CLIENT_SECRET
};

let auth = null;

let user_agent = `node.js:guessr.io:v1.0.0 (by u/${process.env.REDDIT_USERNAME})`;

class RedditClient {

static initialize(id, secret) {
creds.id = id;
creds.secret = secret;
}

static async connect() {
return axios.request({
method: "POST",
Expand All @@ -37,19 +29,24 @@ class RedditClient {
if (response.status === 200) {
auth = response.data;
return response.data;
} else {
throw new Error(response.message);
}
}
).catch(
(err) => {
Logger.error(err.message);
}
)
);
}

static async getRandomSubreddit() {
return axios.request({
method: "POST",
url: "https://www.reddit.com/r/random/top/.json?t=week",
headers: {
"User-Agent": user_agent
}
})
.then(
(response) => {
Expand All @@ -61,7 +58,7 @@ class RedditClient {
(err) => {
Logger.error(err.message);
}
)
);
}

static async getRandomSubredditWithAuth() {
Expand All @@ -88,10 +85,11 @@ class RedditClient {
);
if (clean_headers["x-ratelimit-used"]) {
Logger.warn("Reddit API is warning of usage for /u/" + process.env.REDDIT_USERNAME);
if (clean_headers["x-ratelimit-remaining"] < 20) {
Logger.error("< 20 requests remaining for /u/" + process.env.REDDIT_USERNAME);
}
}
}

if (clean_headers["x-ratelimit-remaining"] < 20) {
Logger.error("< 20 requests remaining for /u/" + process.env.REDDIT_USERNAME);
}
return response.data;
}
}
Expand All @@ -100,7 +98,7 @@ class RedditClient {
(err) => {
Logger.error(err);
}
)
);
}

static async requestWithAuth(config) {
Expand All @@ -111,7 +109,7 @@ class RedditClient {
"User-Agent": user_agent,
...config.headers
}
})
});
}
}

Expand All @@ -120,6 +118,6 @@ setInterval(
RedditClient.connect();
},
1000 * 60 * 5
)
);

module.exports = RedditClient;
29 changes: 23 additions & 6 deletions src/routes/api.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const fs = require("fs");
const path = require("path");

const { Logger } = require("../lib/log");
const Reddit = require("../lib/reddit");

Expand All @@ -8,21 +11,35 @@ const api = function (instance, opts, done) {
});
});

// instance.get("/connect", async () => {
// await Reddit.connect();
// return {};
// });
let denySubreddits = null;

try {
const data = fs.readFileSync(path.join(process.cwd(), "subreddit-denylist.txt"), { encoding: "utf-8"});
data.replace("\r", "");
denySubreddits = data.split("\n");
} catch (err) {
Logger.error(err.message);
denySubreddits = [];
}

instance.get("/newgame", async () => {
let response;
try {
response = await Reddit.getRandomSubredditWithAuth();
} catch (err) {
Logger.warn("Reddit API Call with Auth failed... falling back to no auth")

Logger.warn("Reddit API call with Auth failed... falling back to no auth");
response = await Reddit.getRandomSubreddit();
}

while (denySubreddits.indexOf(response.data.children[0].data.subreddit) > -1) {
try {
response = await Reddit.getRandomSubredditWithAuth();
} catch (err) {
Logger.warn("Reddit API call with Auth failed... falling back to no auth");
response = await Reddit.getRandomSubreddit();
}
}

return {
subreddit: response.data.children[0].data.subreddit,
posts: response.data.children.map((post) => ({
Expand Down
1 change: 1 addition & 0 deletions subreddit-denylist.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NoahGetTheBoat

0 comments on commit 8d2401e

Please sign in to comment.