This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
104 lines (84 loc) · 2.24 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
require("dotenv").config();
const Arena = require("are.na");
const arena = new Arena();
const rss = require("rss");
const express = require("express");
const path = require("path");
const app = express();
const timeago = require("timeago.js");
const URL = "inbox-zero";
const TITLE = "Incompleto – Inbox zero";
const FEED_URL = "https://inbox.incomple.to";
const SITE_URL = "https://www.are.na/incompleto/inbox-zero";
const AUTHOR = "Incompleto";
const PER_PAGE = 25;
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(express.static("public"));
const generateFeedFromChannel = channel => {
let feed = new rss({
title: TITLE, // channel.title
description: channel.metadata.description,
feed_url: FEED_URL,
site_url: SITE_URL,
author: AUTHOR
});
channel.contents.reverse().map(item => {
let url = "";
if (item && item.source && item.source.url) {
url = item.source.url;
}
if (item && item.image) {
enclosure = {
url: item.image.display.url
};
}
feed.item({
title: item.title,
description: item.description_html,
url,
enclosure,
author: item.author,
date: item.updated_at
});
});
return feed;
};
const getBlocksFromChannel = channel => {
let page = Math.ceil(channel.length / PER_PAGE);
return arena
.channel(URL, { page, PER_PAGE })
.get()
.then(generateFeedFromChannel)
.catch(error => {
console.log(error);
});
};
const getFeed = () => {
return arena
.channel(URL, { PER_PAGE })
.get()
.then(getBlocksFromChannel)
.catch(error => {
console.log(error);
});
};
app.get("/rss", function(request, response) {
let feed = getFeed();
feed.then(function(result) {
response.setHeader("Content-Type", "application/rss+xml");
response.send(result.xml({ indent: true }));
});
});
app.get("/", function(req, res) {
let feed = getFeed();
feed.then(function(result) {
result.items.forEach(function(item) {
item.timeago = timeago.format(item.date);
});
res.render("index", { feed: result });
});
});
const listener = app.listen(process.env.PORT, function() {
console.log("Your app is listening on port " + listener.address().port);
});