-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
53 lines (47 loc) · 1.43 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
const express = require("express");
const request = require("request");
const cheerio = require("cheerio");
const env = require("./sample_env");
const port = process.env.PORT || env.PORT;
const bookmarksOptions = {
url: `https://medium.com/me/list/bookmarks?limit=${env.LIMIT}`,
headers: {
'Cookie': env.MEDIUM_COOKIE
}
};
const app = express();
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/views"));
app.use(express.static(__dirname + "/public"));
app.get("/bookmarks", async (req, res) => {
let list = await getBMs();
res.render("index", {list: list});
});
app.get("/api/bookmarks", async (req, res) => {
let list = await getBMs();
res.send(list);
});
function getBMs() {
return new Promise(function (resolve, reject) {
let result = [];
request(bookmarksOptions, function(err, res, html) {
if (!err && res.statusCode == 200) {
const $ = cheerio.load(html);
$(".streamItem .link.link--noUnderline.u-baseColor--link").each(function(i, elem) {
let bm = {};
if (i%2 == 0) {
bm["title"] = elem.children[0].children[0].data;
bm["desc"] = elem.children[1] ? elem.children[1].children[0].data : "";
bm["link"] = elem.attribs.href;
bm["time_mins"] = parseInt(elem.next.children[3].next.attribs.title);
result.push(bm);
}
});
resolve(result);
} else {
reject(err);
}
});
});
}
app.listen(port, () => console.log(`Server running on ${port}`));