now-updater/utils.js

63 lines
2 KiB
JavaScript
Raw Normal View History

2025-12-18 05:53:32 +00:00
import 'dotenv/config'
import { JSDOM } from 'jsdom'
import sanitizeHtml from 'sanitize-html'
2025-12-18 01:57:14 +00:00
export function markdownLinkRegex(url) {
return new RegExp(
2025-12-18 05:53:32 +00:00
`(\\[).*?(\\]\\(https:\\/\\/${url.replaceAll('.', '\\.')}\\))`,
'i',
)
2025-12-18 01:57:14 +00:00
}
export async function getJsonFeedItemTitle(feedUrl) {
2025-12-18 05:53:32 +00:00
const res = await fetch(feedUrl)
const data = await res.json()
const post = data.items[0]
const { title, image } = post
return image ? `${title} ![${title}](${image})` : title
2025-12-18 01:57:14 +00:00
}
export async function getJsonFeedItemContent(feedUrl) {
2025-12-18 05:53:32 +00:00
const res = await fetch(feedUrl)
const data = await res.json()
const post = data.items[0]
const { content_html, image } = post
2025-12-18 01:57:14 +00:00
const title = sanitizeHtml(content_html, {
allowedTags: [],
allowedAttributes: {},
2025-12-18 05:53:32 +00:00
})?.trim()
return image ? `${title} ![${title}](${image})` : title
2025-12-18 01:57:14 +00:00
}
2025-12-18 02:39:27 +00:00
export async function getMalojaScrobble(malojaUrl) {
2025-12-18 05:53:32 +00:00
const res = await fetch(`${malojaUrl}/apis/mlj_1/scrobbles?perpage=1`)
const data = await res.json()
const scrobble = data.list[0]
2025-12-18 02:39:27 +00:00
const {
title,
album: { artists },
2025-12-18 05:53:32 +00:00
} = scrobble.track
return `${title}<br />by ${artists.join(', ')}`
2025-12-18 02:39:27 +00:00
}
2025-12-18 05:35:31 +00:00
export async function getLetterboxdActivity(username) {
2025-12-18 05:53:32 +00:00
const res = await fetch(`https://letterboxd.com/${username}/rss/`)
const data = await res.text()
const dom = new JSDOM(data)
const item = dom.window.document.querySelector('item')
const title = item.querySelector('title').textContent
const image = item.querySelector('description > img').src
return `${title} ![${title}](${image})`
}
2025-12-18 05:35:31 +00:00
2025-12-18 05:53:32 +00:00
export async function getTraktEpisode(username, id) {
const res = await fetch(
`https://trakt.tv/users/${username}/history/episodes/added/asc.atom?slurm=${process.env.TRAKT_SLURM}`,
)
const data = await res.text()
const dom = new JSDOM(data)
const entry = dom.window.document.querySelector('entry')
const title = entry.querySelector('title').textContent
return `${title} ![${title}](https://widgets.trakt.tv/users/${id}/watched/thumb@2x.jpg?type=episode&image_only=1)`
2025-12-18 05:35:31 +00:00
}