graceful error handling, does not update services that did not return data
This commit is contained in:
parent
b9f0e17c46
commit
1d49626439
1 changed files with 25 additions and 21 deletions
44
index.js
44
index.js
|
|
@ -1,50 +1,54 @@
|
||||||
import "dotenv/config";
|
import 'dotenv/config'
|
||||||
|
|
||||||
import { items } from "./config.js";
|
import { items } from './config.js'
|
||||||
|
|
||||||
const OMGLOL_API = `https://api.omg.lol/address/${process.env.OMGLOL_USERNAME}/now`;
|
const OMGLOL_API = `https://api.omg.lol/address/${process.env.OMGLOL_USERNAME}/now`
|
||||||
|
|
||||||
export async function getNow() {
|
export async function getNow() {
|
||||||
const res = await fetch(OMGLOL_API);
|
const res = await fetch(OMGLOL_API)
|
||||||
const data = await res.json();
|
const data = await res.json()
|
||||||
return data.response.now.content;
|
return data.response.now.content
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function setNow(newNow) {
|
export async function setNow(newNow) {
|
||||||
const res = await fetch(OMGLOL_API, {
|
const res = await fetch(OMGLOL_API, {
|
||||||
method: "POST",
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
'Content-Type': 'application/json',
|
||||||
Authorization: `Bearer ${process.env.OMGLOL_KEY}`,
|
Authorization: `Bearer ${process.env.OMGLOL_KEY}`,
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
content: newNow,
|
content: newNow,
|
||||||
listed: 1,
|
listed: 1,
|
||||||
}),
|
}),
|
||||||
});
|
})
|
||||||
const data = await res.json();
|
const data = await res.json()
|
||||||
console.log(data.response.message);
|
console.log(`\n${data.response.message}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function now() {
|
export default async function now() {
|
||||||
const now = await getNow();
|
const now = await getNow()
|
||||||
let newNow = now;
|
let newNow = now
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
items.map(async ({ id, regex, getLatest }) => {
|
items.map(async ({ id, regex, getLatest }) => {
|
||||||
const latest = await getLatest();
|
try {
|
||||||
console.log(`${id}: ${latest}`);
|
const latest = await getLatest()
|
||||||
|
console.log(`${id}: ${latest}`)
|
||||||
if (latest) {
|
if (latest) {
|
||||||
newNow = newNow.replace(regex, `$1${latest}$2`);
|
newNow = newNow.replace(regex, `$1${latest}$2`)
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.warn(`⚠️ Failed to fetch ${id}`, e)
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
);
|
)
|
||||||
|
|
||||||
if (now === newNow) {
|
if (now === newNow) {
|
||||||
console.log("Now page has no changes");
|
console.log('\nNow page has no changes')
|
||||||
} else {
|
} else {
|
||||||
await setNow(newNow);
|
await setNow(newNow)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await now();
|
await now()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue