UNPKG

rsshub

Version:
99 lines (97 loc) 3.96 kB
import { t as ofetch_default } from "./ofetch-DKl_Ma9g.mjs"; import { t as parseDate } from "./parse-date-r5WDWHno.mjs"; import { t as cache_default } from "./cache-SV6W5EPy.mjs"; import { load } from "cheerio"; //#region lib/routes/dailypush/utils.ts const BASE_URL = "https://www.dailypush.dev"; /** * Extract author from article element */ function extractAuthor(article) { const sourceSpan = article.find("span.text-xs.font-medium.uppercase").first(); if (sourceSpan.length > 0) return sourceSpan.text().trim(); const authorMatch = article.find(".flex.items-center.gap-3").first().text().match(/^([^•]+?)(?:\s*•)/); if (authorMatch && !/\d{4}/.test(authorMatch[1])) return authorMatch[1].trim(); } /** * Extract categories/tags from article element */ function extractCategories(article, $) { return article.find("a[href^=\"/\"]").toArray().map((tagEl) => { const tagElement = $(tagEl); const tagHref = tagElement.attr("href"); const tagText = tagElement.text().trim(); if (tagHref && tagText && !tagHref.includes("article/") && !tagHref.includes("Summary") && tagText.length < 50 && !/^(Summary|stats|About|Tags|Toggle|Trending|Latest|Previous|Next)$/i.test(tagText)) return tagText; return null; }).filter((tagText) => tagText !== null); } /** * Extract publication date from article element */ function extractPubDate(article) { const authorAndDate = article.find(".flex.items-center.justify-between.gap-4.flex-wrap").first().find(".flex.items-center.gap-3.text-xs").first(); if (authorAndDate.length === 0) return; const spans = authorAndDate.find("span"); let dateText; if (spans.length === 3) dateText = spans.eq(2).text().trim(); else if (spans.length === 1) dateText = spans.eq(0).text().trim(); if (!dateText) return; try { return parseDate(dateText); } catch { const match = dateText.match(/((?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+\d+,\s+\d{4})/i); if (match && match[1]) try { return parseDate(match[1]); } catch {} } } /** * Parse a single article element into an ArticleItem */ function parseArticle(article, $, baseUrl) { const titleLink = article.find("h2 a[href^=\"http\"]").first(); if (titleLink.length === 0) return null; const title = titleLink.text().trim(); const link = titleLink.attr("href"); if (!title || !link || link.includes("dailypush.dev")) return null; const author = extractAuthor(article); const description = article.find("p.text-sm.text-muted-foreground").first().text().trim() || void 0; const categories = extractCategories(article, $); const summaryLink = article.find(".flex.items-center.justify-between.gap-4.flex-wrap").first().find("a[href*=\"/article/\"]").first().attr("href"); const dailyPushUrl = summaryLink ? `${baseUrl}${summaryLink}` : void 0; const pubDate = extractPubDate(article); return { title, link, author: author || void 0, pubDate, category: categories.length > 0 ? categories : void 0, description, articleUrl: link, dailyPushUrl }; } /** * Parse all articles from the page */ function parseArticles($, baseUrl) { return $("article").toArray().map((articleEl) => { return parseArticle($(articleEl), $, baseUrl); }).filter((parsed) => parsed !== null); } /** * Enhance items with full summaries from dailypush article pages */ async function enhanceItemsWithSummaries(items) { const itemsWithUrl = items.filter((item) => item.dailyPushUrl !== void 0); const itemsWithoutUrl = items.filter((item) => item.dailyPushUrl === void 0); return [...await Promise.all(itemsWithUrl.map((item) => cache_default.tryGet(item.dailyPushUrl, async () => { try { const summary = load(await ofetch_default(item.dailyPushUrl))("p.font-ibm-plex-sans.leading-relaxed").first(); if (summary.length > 0 && summary.text().trim()) item.description = summary.text().trim(); } catch {} return item; }))), ...itemsWithoutUrl]; } //#endregion export { enhanceItemsWithSummaries as n, parseArticles as r, BASE_URL as t };