-
Notifications
You must be signed in to change notification settings - Fork 1
/
meme.js
76 lines (66 loc) · 1.69 KB
/
meme.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
const axios = require("axios");
async function freshMeme() {
let subReddit = await getSubReddit();
let meme = await getMeme(subReddit);
console.log(meme);
async function getSubReddit() {
let memeSubs = [
"blackpeopletwitter",
"metal_me_irl",
"bee_irl",
"coaxedintoasnafu",
"195",
"shittyadviceanimals",
"wholesomememes",
"dankmemes",
"memes",
"Dogfort",
"vertical",
"AdviceAnimals",
"fffffffuuuuuuuuuuuu",
"treecomics",
];
let randomSub = randomNumber(memeSubs.length);
let subReddit = memeSubs[randomSub];
return subReddit;
}
async function getMeme(subReddit) {
try {
const options = {
method: "GET",
url: `https://www.reddit.com/r/${subReddit}/hot.json?limit=100`,
};
const response = await axios.request(options);
let randomMeme = randomNumber(100);
let post = response.data.data.children[randomMeme].data;
let link = post.url;
let checkUrl = linkChecker(link);
let memeLink = checkUrl ? "image" : "not an image";
let nsfw = nsfwChecker(post);
let memeNsfw = nsfw ? "nsfw" : "sfw";
let memeTitle = post.title;
let memeImg = post.url;
return {
title: memeTitle,
img: memeImg,
nsfw: memeNsfw,
link: memeLink,
};
} catch (error) {
console.error(error);
}
}
function linkChecker(link) {
return link.match(/\.(jpeg|jpg|gif|png)$/) !== null;
}
function nsfwChecker(post) {
return post.over_18;
}
function randomNumber(num) {
return Math.floor(Math.random() * num);
}
return meme;
}
module.exports = {
freshMeme: freshMeme,
};