62 lines
1.4 KiB
JavaScript
62 lines
1.4 KiB
JavaScript
import 'dotenv/config'
|
|
|
|
import { items } from './config.js'
|
|
|
|
const OMGLOL_API = `https://api.omg.lol/address/${process.env.OMGLOL_USERNAME}/now`
|
|
|
|
export async function getNow() {
|
|
const res = await fetch(OMGLOL_API)
|
|
const data = await res.json()
|
|
return data.response.now.content
|
|
}
|
|
|
|
export async function setNow(newNow) {
|
|
const res = await fetch(OMGLOL_API, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
Authorization: `Bearer ${process.env.OMGLOL_KEY}`,
|
|
},
|
|
body: JSON.stringify({
|
|
content: newNow,
|
|
listed: 1,
|
|
}),
|
|
})
|
|
const data = await res.json()
|
|
console.log(`\n${data.response.message}`)
|
|
}
|
|
|
|
export default async function now() {
|
|
const now = await getNow()
|
|
let newNow = now
|
|
|
|
await Promise.all(
|
|
items.map(async ({ id, regex, getLatest }) => {
|
|
try {
|
|
const latest = await getLatest()
|
|
console.log(`${id}: ${latest.text}`)
|
|
console.log(`${id} URL: ${latest.url}`)
|
|
if (latest && latest.text && latest.url) {
|
|
newNow = newNow.replace(regex, (match, openTag, closeTag) => {
|
|
// Replace href in opening tag
|
|
const updatedOpenTag = openTag.replace(
|
|
/href=["'][^"']*["']/,
|
|
`href="${latest.url}"`,
|
|
)
|
|
return `${updatedOpenTag}${latest.text}${closeTag}`
|
|
})
|
|
}
|
|
} catch (e) {
|
|
console.warn(`⚠️ Failed to fetch ${id}`, e)
|
|
}
|
|
}),
|
|
)
|
|
|
|
if (now === newNow) {
|
|
console.log('\nNow page has no changes')
|
|
} else {
|
|
await setNow(newNow)
|
|
}
|
|
}
|
|
|
|
await now()
|