feat(route): add Uptime Robot RSS (#11663)

Signed-off-by: Rongrong <i@rong.moe>

Signed-off-by: Rongrong <i@rong.moe>
This commit is contained in:
Rongrong
2023-01-21 03:35:31 +08:00
committed by GitHub
parent c2da0659ae
commit 7e6fbdb37f
7 changed files with 192 additions and 1 deletions
+14 -1
View File
@@ -2,7 +2,7 @@
pageClass: routes
---
# Forecast
# Forecast and alerts
## Outage.Report
@@ -13,3 +13,16 @@ pageClass: routes
Please skip the local service area code for `name`, for example `https://outage.report/us/verizon-wireless` to `verizon-wireless`.
</RouteEn>
## Uptime Robot
### RSS
<RouteEn author="Rongronggg9" example="/uptimerobot/rss/u358785-e4323652448755805d668f1a66506f2f" path="/uptimerobot/rss/:id/:routeParams?" :paramsDesc="['the last part of your RSS URL (e.g. `u358785-e4323652448755805d668f1a66506f2f` for `https://rss.uptimerobot.com/u358785-e4323652448755805d668f1a66506f2f`)', 'extra parameters, see the table below']">
<!-- example stolen from https://atlas.eff.org//domains/uptimerobot.com.html -->
| Key | Description | Accepts | Defaults to |
|--------|--------------------------------------------------------------------------|----------------|-------------|
| showID | Show monitor ID (disabling it will also disable link for each RSS entry) | 0/1/true/false | true |
</RouteEn>
+13
View File
@@ -14,6 +14,19 @@ pageClass: routes
</Route>
## Uptime Robot
### RSS
<Route author="Rongronggg9" example="/uptimerobot/rss/u358785-e4323652448755805d668f1a66506f2f" path="/uptimerobot/rss/:id/:routeParams?" :paramsDesc="['RSS URL 的最后一部分 (e.g. 对于 `https://rss.uptimerobot.com/u358785-e4323652448755805d668f1a66506f2f`,为 `u358785-e4323652448755805d668f1a66506f2f`)', '额外参数,请参阅下面的表格']">
<!-- example stolen from https://atlas.eff.org//domains/uptimerobot.com.html -->
| 键 | 含义 | 接受的值 | 默认值 |
| ------ | ----------------------------------------- | -------------- | ---- |
| showID | 是否包含 monitor ID (关闭此项同时也会使得各个 RSS 条目不附链接) | 0/1/true/false | true |
</Route>
## 地震速报
### 中国地震局
+3
View File
@@ -0,0 +1,3 @@
module.exports = {
'/rss/:id/:routeParams?': ['Rongronggg9'],
};
+13
View File
@@ -0,0 +1,13 @@
module.exports = {
'uptimerobot.com': {
_name: 'Uptime Robot',
rss: [
{
title: 'RSS',
docs: 'https://docs.rsshub.app/forecast.html#uptime-robot',
source: ['/:id'],
target: '/uptimerobot/rss/:id',
},
],
},
};
+3
View File
@@ -0,0 +1,3 @@
module.exports = (router) => {
router.get('/rss/:id/:routeParams?', require('./rss'));
};
+126
View File
@@ -0,0 +1,126 @@
const Parser = require('rss-parser');
const { art } = require('@/utils/render');
const path = require('path');
const dayjs = require('dayjs');
const { fallback, queryToBoolean } = require('@/utils/readable-social');
const titleRegex = /(.+)\s+is\s+([A-Z]+)\s+\((.+)\)/;
const formatTime = (s) => {
const duration = dayjs.duration(s - 0, 'seconds');
const days = duration.days();
const hours = duration.hours();
const minutes = duration.minutes();
const seconds = duration.seconds();
if (days > 0) {
return `${days}d ${hours}h ${minutes}m ${seconds}s`;
} else if (hours > 0) {
return `${hours}h ${minutes}m ${seconds}s`;
} else if (minutes > 0) {
return `${minutes}m ${seconds}s`;
} else {
return `${seconds}s`;
}
};
class Monitor {
constructor(name, uptime = 0, downtime = 0) {
this.name = name;
this.uptime = uptime;
this.downtime = downtime;
}
uptimeRatio() {
return this.uptime / (this.uptime + this.downtime);
}
downtimeRatio() {
return this.downtime / (this.uptime + this.downtime);
}
up(time) {
this.uptime += time - 0;
}
down(time) {
this.downtime += time - 0;
}
}
const rootURL = 'https://rss.uptimerobot.com';
module.exports = async (ctx) => {
const { id } = ctx.params;
const routeParams = Object.fromEntries(new URLSearchParams(ctx.params.routeParams));
const showID = fallback(undefined, queryToBoolean(routeParams.showID), true);
const rssUrl = `${rootURL}/${id}`;
const rss = await new Parser({
customFields: {
item: ['details:duration'],
},
}).parseURL(rssUrl);
const monitors = {};
const items = rss.items.reverse().map((item) => {
const titleMatch = item.title.match(titleRegex);
if (!titleMatch) {
throw new Error('Unexpected title, please open an issue.');
}
const [monitorName, status, id] = titleMatch.slice(1);
if (id !== item.link) {
throw new Error('Monitor ID mismatch, please open an issue.');
}
// id could be a URL, a domain, an IP address, or a hex string. fix it
let link;
try {
link = !id.startsWith('http') && id.includes('.') ? new URL(`http://${id}`).href : new URL(id).href;
} catch (e) {
// ignore
}
const duration = item['details:duration'];
const monitor = (monitors[monitorName] = monitors[monitorName] || new Monitor(monitorName));
if (status === 'UP') {
monitor.up(duration);
} else if (status === 'DOWN') {
monitor.down(duration);
} else {
throw new Error('Unexpected status, please open an issue.');
}
const desc = art(path.join(__dirname, 'templates/rss.art'), {
status,
link,
id: showID ? id : null,
duration: formatTime(duration),
uptime: formatTime(monitor.uptime),
downtime: formatTime(monitor.downtime),
uptime_ratio: Number(monitor.uptimeRatio()).toLocaleString(undefined, {
style: 'percent',
minimumFractionDigits: 2,
}),
details: item.content,
});
return {
...item,
title: `[${status}] ${monitorName}`,
description: desc,
link: showID ? link : null,
};
});
ctx.state.data = {
title: 'Uptime Robot - RSS (enhanced)',
description: rss.description,
link: rssUrl,
item: items,
image: 'https://uptimerobot.com/favicon.ico',
};
};
+20
View File
@@ -0,0 +1,20 @@
Already {{ status }} for {{ duration }}
<br><br>
{{ if id }}
Monitor ID:
{{ if link }}
<a href="{{ link }}" target="_blank">{{ id }}</a>
{{ else }}
{{ id }}
{{ /if }}
<br><br>
{{ /if }}
Uptime: {{ uptime }}
<br>
Downtime: {{ downtime }}
<br>
Availability: {{ uptime_ratio }}
{{ if details && details.trim() !== 'Alert Details:' }}
<br><br>
{{ details }}
{{ /if }}