-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
143 lines (129 loc) · 4.57 KB
/
handler.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
"use strict";
const Raindrops = require("./raindrops");
const wpcom = require("wpcom")(process.env.wpToken);
const trim = (str, length) => {
if (str.length > length) {
return str.substring(0, length - 4) + "...";
}
return str;
};
const removeHtml = (str) => str.replace(/<[^>]*>/g, "");
const getWeekNumber = () => {
const currentdate = new Date();
const oneJan = new Date(currentdate.getFullYear(), 0, 1);
const numberOfDays = Math.floor(
(currentdate - oneJan) / (24 * 60 * 60 * 1000)
);
const result = Math.ceil((currentdate.getDay() + 1 + numberOfDays) / 7);
return result;
};
const isVideo = ({ link }) => {
const videoDomains = ["youtube.com", "vimeo.com"];
if (videoDomains.some((domain) => link.indexOf(domain) !== -1)) {
return true;
}
};
module.exports.run = async (event, context) => {
const categories = [
{ slug: "coding", name: "Coding" },
{ slug: "tech", name: "Tech" },
{ slug: "handy", name: "Tips & Tricks" },
{ slug: "gaming", name: "Gaming" },
{ slug: "music", name: "Music" },
{ slug: "make", name: "Make" },
{ slug: "funny", name: "Funny" },
];
const time = new Date();
const raindropsApi = new Raindrops(process.env.raindropToken);
const { items } = await raindropsApi.call(
`/rest/v1/raindrops/${process.env.raindropCollection}?sort=-created`
);
const lastWeek = items.filter((item) => {
const time = new Date();
time.setDate(time.getDate() - 7);
return new Date(item.created).getTime() > time.getTime();
});
// get all tags from lastWeek items
const tags = lastWeek.reduce((acc, item) => {
const { tags } = item;
if (tags) {
tags.forEach((tag) => {
if (!acc.includes(tag)) {
acc.push(tag);
}
});
}
return acc;
}, []);
if (lastWeek.length) {
const groupedByCategory = [];
categories.forEach((category) => {
lastWeek.forEach((item) => {
if (item.tags.includes(category.slug)) {
if (
groupedByCategory.find(
(group) => group.category === category.name
)
) {
groupedByCategory
.find((group) => group.category === category.name)
.items.push(item);
} else {
groupedByCategory.push({
category: category.name,
items: [item],
});
}
}
});
});
const others = lastWeek.filter((item) => {
return !item.tags.some((tag) =>
categories.filter((c) => c.slug === tag)
);
});
if (others) {
groupedByCategory.push({
category: "Misc",
items: others,
});
}
const content = `${groupedByCategory
.map(
(category) =>
`<h2>${category.category}</h2>\n${category.items
.map(
(item) =>
`<p><a href="${
item.link
}" target="_blank" rel="noopener noreferrer">${
isVideo(item) ? "▶️ " : ""
}${item.title}</a>${
item.excerpt
? `<br />${trim(
removeHtml(item.excerpt),
140
)}`
: ""
}</p>\n\n`
)
.join("\n\n")}`
)
.join("")}`;
const weekNumber = getWeekNumber();
const post = {
title: `Weekly linkdump (week ${weekNumber})`,
content,
status: "draft",
categories: "English, Linkdump",
excerpt: "My weekly linkdump.",
tags: ["linkdump"],
};
if (process.env.PREVIEW) {
console.log(post);
} else {
await wpcom.site(process.env.wpSite).addPost(post);
}
}
console.log(`Your cron function "${context.functionName}" ran at ${time}`);
};