-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
198 lines (173 loc) · 5.84 KB
/
server.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const axios = require("axios");
const express = require("express");
const cheerio = require("cheerio");
const jsonfile = require("jsonfile");
const cors = require("cors");
const uniqid = require("uniqid");
const app = express();
app.use(cors());
app.use(express.json()); // parse content so we can access it in requests
let recievedUserInput = "";
const baseUrl = "https://old.reddit.com/r/";
const subredditData = async (sub) => {
console.log("Getting data from website...");
const user_url = baseUrl + sub + "/";
const user_rising_url = baseUrl + sub + "/rising/";
const user_new_url = baseUrl + sub + "/new/";
const user_controversial_url = baseUrl + sub + "/controversial/";
try {
let response = await axios.get(user_url);
let $ = cheerio.load(response.data);
const subreddits = [];
const user_subreddit = {
name: "",
url: "",
about: "",
members: "",
online_members: "",
img: "",
age: "",
posts: [],
new_posts: [],
rising_posts: [],
controversial_posts: [],
discord: "",
};
// url
user_subreddit.url = user_url;
// name
const sub_name = $("h1.redditname").text();
user_subreddit.name = sub_name;
// about
const sub_about = $("div.md p:first-child").text();
user_subreddit.about = sub_about;
// header image
const sub_img = $("img#header-img").attr("src");
user_subreddit.img = "https:" + sub_img;
// members
const sub_members = $("span.subscribers > span.number").text();
user_subreddit.members = sub_members;
// online members
const sub_online_members = $("p.users-online > span.number").text();
user_subreddit.online_members = sub_online_members;
// age
const sub_age = $("span.age > time").text();
user_subreddit.age = sub_age;
// discord
const sub_discord = $("div.md a[href^='https://discord']");
user_subreddit.discord = sub_discord.attr("href");
// hot posts
$("div.thing").each((i, val) => {
const post = {};
post.id = uniqid();
post.number = $(val).find("span.rank").text();
post.title = $(val).find("div.top-matter > p.title > a.title").text();
post.upvotes = $(val).find("div.score.unvoted").text();
if (post.upvotes === "•") {
post.upvotes = "No upvotes yet";
}
post.comments = $(val)
.find("ul.flat-list.buttons a[data-event-action='comments']")
.text();
const parseComment = parseInt(post.comments);
if (isNaN(parseComment)) {
post.comments = "No comments yet";
}
post.timestamp = $(val).find("p.tagline > time").text();
user_subreddit.posts.push(post);
});
// new posts
response = await axios.get(user_new_url);
$ = cheerio.load(response.data);
$("div.thing").each((i, val) => {
const post = {};
post.id = uniqid();
post.number = $(val).find("span.rank").text();
post.title = $(val).find("div.top-matter > p.title > a.title").text();
post.upvotes = $(val).find("div.score.unvoted").text();
if (post.upvotes === "•") {
post.upvotes = "No upvotes yet";
}
post.comments = $(val)
.find("ul.flat-list.buttons a[data-event-action='comments']")
.text();
const parseComment = parseInt(post.comments);
if (isNaN(parseComment)) {
post.comments = "No comments yet";
}
post.timestamp = $(val).find("p.tagline > time").text();
user_subreddit.new_posts.push(post);
});
// rising posts
response = await axios.get(user_rising_url);
$ = cheerio.load(response.data);
$("div.thing").each((i, val) => {
const post = {};
post.id = uniqid();
post.number = $(val).find("span.rank").text();
post.title = $(val).find("div.top-matter > p.title > a.title").text();
post.upvotes = $(val).find("div.score.unvoted").text();
if (post.upvotes === "•") {
post.upvotes = "No upvotes yet";
}
post.comments = $(val)
.find("ul.flat-list.buttons a[data-event-action='comments']")
.text();
const parseComment = parseInt(post.comments);
if (isNaN(parseComment)) {
post.comments = "No comments yet";
}
post.timestamp = $(val).find("p.tagline > time").text();
user_subreddit.rising_posts.push(post);
});
// controversial
response = await axios.get(user_controversial_url);
$ = cheerio.load(response.data);
$("div.thing").each((i, val) => {
const post = {};
post.id = uniqid();
post.number = $(val).find("span.rank").text();
post.title = $(val).find("div.top-matter > p.title > a.title").text();
post.upvotes = $(val).find("div.score.unvoted").text();
if (post.upvotes === "•") {
post.upvotes = "No upvotes yet";
}
post.comments = $(val)
.find("ul.flat-list.buttons a[data-event-action='comments']")
.text();
const parseComment = parseInt(post.comments);
if (isNaN(parseComment)) {
post.comments = "No comments yet";
}
post.timestamp = $(val).find("p.tagline > time").text();
user_subreddit.controversial_posts.push(post);
});
console.log("backend data", user_subreddit);
subreddits.push(user_subreddit);
return subreddits;
} catch (err) {
console.log(err.message);
}
};
app.get("/", (req, res) => {
console.log("home");
});
// retrieve user input from frontend
app.post("/post", (req, res) => {
const body = req.body;
recievedUserInput = body.userInput;
});
// get subreddit data using recieved user input
app.get("/api", (req, res) => {
const response = res;
subredditData(recievedUserInput)
.then((res) => {
response.json(res);
})
.catch((err) => {
console.log(err.message);
});
});
app.listen(process.env.PORT || 5000, () => {
console.log(`server listening on port: ${process.env.PORT} or 5000`);
});