now-updater/utils.js

52 lines
1.5 KiB
JavaScript
Raw Normal View History

2025-12-18 05:35:31 +00:00
import { JSDOM } from "jsdom";
2025-12-18 01:57:14 +00:00
import sanitizeHtml from "sanitize-html";
export function markdownLinkRegex(url) {
return new RegExp(
`(\\[).*?(\\]\\(https:\\/\\/${url.replaceAll(".", "\\.")}\\))`,
"i",
);
}
export async function getJsonFeedItemTitle(feedUrl) {
const res = await fetch(feedUrl);
const data = await res.json();
const post = data.items[0];
const { title, image } = post;
2025-12-18 05:35:31 +00:00
return image ? `${title} ![${title}](${image})` : title;
2025-12-18 01:57:14 +00:00
}
export async function getJsonFeedItemContent(feedUrl) {
const res = await fetch(feedUrl);
const data = await res.json();
const post = data.items[0];
const { content_html, image } = post;
const title = sanitizeHtml(content_html, {
allowedTags: [],
allowedAttributes: {},
})?.trim();
2025-12-18 05:35:31 +00:00
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) {
const res = await fetch(`${malojaUrl}/apis/mlj_1/scrobbles?perpage=1`);
const data = await res.json();
const scrobble = data.list[0];
const {
title,
album: { artists },
} = scrobble.track;
return `${title}<br />by ${artists.join(", ")}`;
}
2025-12-18 05:35:31 +00:00
export async function getLetterboxdActivity(username) {
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})`;
}