-
Notifications
You must be signed in to change notification settings - Fork 6
/
CommitStrip.ts
117 lines (104 loc) · 2.78 KB
/
CommitStrip.ts
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
import { MessageEmbed } from 'discord.js';
import got from 'got';
import { decode } from 'html-entities';
import { Cron, findTextChannelByName } from '../framework';
export default new Cron({
enabled: true,
name: 'CommitStrip',
description:
'Vérifie toutes les 30 minutes si un nouveau CommitStrip est sorti et le poste dans #gif',
schedule: '*/30 * * * *',
async handle(context) {
const latestCommitStrip = await getRecentCommitStrip(context.date);
if (!latestCommitStrip) {
return;
}
context.logger.info(`Found a new CommitStrip (${latestCommitStrip.id})`);
const channel = findTextChannelByName(context.client, 'gif');
if (!channel) {
throw new Error('found no #gif channel');
}
await channel.send(
new MessageEmbed()
.setTitle(latestCommitStrip.title)
.setURL(latestCommitStrip.link)
.setImage(latestCommitStrip.imageUrl),
);
},
});
/**
* Subset of the fields returned by the WordPress Posts API.
*/
interface WordPressPost {
id: number;
date: string;
link: string;
title: {
rendered: string;
};
content: {
rendered: string;
};
}
/**
* Strip information extracted from the WordPress post.
*/
interface CommitStrip {
/**
* Post ID.
*/
id: number;
/**
* Post date.
*/
date: Date;
/**
* Direct link to the post.
*/
link: string;
/**
* Post title.
*/
title: string;
/**
* URL of the strip image.
*/
imageUrl: string;
}
/**
* Fetches the most recent post from the WordPress API. If there is one and it
* was posted between the previous and current cron execution, returns it.
* Otherwise, or if the post does not contain any image URL, returns null.
* @param now - Current date. Comes from cron schedule.
*/
async function getRecentCommitStrip(now: Date): Promise<CommitStrip | null> {
const { body: posts } = await got<WordPressPost[]>(
'https://www.commitstrip.com/fr/wp-json/wp/v2/posts?per_page=1',
{ responseType: 'json' },
);
if (posts.length === 0) {
return null;
}
const [strip] = posts;
const stripDate = new Date(strip.date);
const stripTime = stripDate.getTime();
const nowTime = now.getTime();
const thirtyMinutes = 1000 * 60 * 30;
if (nowTime - stripTime > thirtyMinutes) {
// Ignore if the strip was not posted in the last 30 minutes
return null;
}
const stripImageUrlReg = /src="([^"]+)"/;
const urlMatch = stripImageUrlReg.exec(strip.content.rendered);
if (!urlMatch) {
return null;
}
return {
id: strip.id,
date: stripDate,
link: strip.link,
// Sometimes, the title can contain HTML Entities, e.g. "Pendant ce temps, sur Mars #16 – Vivement 2028"
title: decode(strip.title.rendered, { level: 'html5', scope: 'strict' }),
imageUrl: urlMatch[1],
};
}