UNPKG

rsshub

Version:
105 lines (104 loc) 4.15 kB
import { t as config } from "./config-Bpyy15zS.mjs"; import { t as parseDate } from "./parse-date-CjhZE69i.mjs"; import { t as cache_default } from "./cache-CiDoUSLo.mjs"; import { t as got_default } from "./got-BDnf4rdT.mjs"; import { t as getAcwScV2ByArg1 } from "./utils-BiyO2GgI.mjs"; //#region lib/routes/techflowpost/utils.ts const rootUrl = "https://www.techflowpost.com"; const apiRootUrl = `${rootUrl}/api`; const uploadRootUrl = "https://upload.techflowpost.com"; const locale = "zh-CN"; let acwScV2Cookie = ""; function getHeaders(referer, cookie = acwScV2Cookie) { return { accept: "application/json, text/plain, */*", "accept-language": locale, referer, "user-agent": config.trueUA, ...cookie && { cookie } }; } function getAcwScV2Cookie(response) { const arg1 = response.match(/var arg1='(.*?)';/)?.[1]; if (!arg1) return ""; return `acw_sc__v2=${getAcwScV2ByArg1(arg1)}`; } function parseJsonBody(body) { try { return JSON.parse(body); } catch { return null; } } async function requestApi(endpoint, referer, searchParams) { const request = () => got_default.get(`${apiRootUrl}${endpoint}`, { searchParams, headers: getHeaders(referer) }); const { body } = await request(); const payload = parseJsonBody(body); if (payload !== null) return payload; const cookie = getAcwScV2Cookie(body); if (!cookie) throw new Error("TechFlow API returned an unexpected non-JSON response."); acwScV2Cookie = cookie; const retryPayload = parseJsonBody((await request()).body); if (retryPayload === null) throw new TypeError("TechFlow API still returned an anti-crawler challenge after retry."); return retryPayload; } function getPictureUrl(picture) { if (!picture) return; return new URL(picture, uploadRootUrl).href; } function getCategories(article) { const names = [article.category?.name, ...article.labels?.map((label) => label.label) ?? []]; return [...new Set(names.filter((name) => Boolean(name)))]; } function getArticleItem(article, content) { const link = `${rootUrl}/${locale}/article/${article.id}`; const categories = getCategories(article); const image = getPictureUrl(article.picture); return { title: article.title, author: article.author?.name, link, category: categories.length ? categories : void 0, pubDate: article.created_at ? parseDate(article.created_at) : void 0, updated: article.updated_at ? parseDate(article.updated_at) : void 0, description: content || article.abstract, image }; } function getNewsflashItem(newsflash, content) { return { title: newsflash.title, link: `${rootUrl}/${locale}/newsletter/${newsflash.id}`, pubDate: newsflash.created_at ? parseDate(newsflash.created_at) : void 0, updated: newsflash.updated_at ? parseDate(newsflash.updated_at) : void 0, description: content || newsflash.abstract }; } async function getArticleItems({ category, limit }) { const response = await requestApi("/client/articles", `${rootUrl}/${locale}/article`, { page: 1, page_size: limit, category_id: category || void 0 }); if (!Array.isArray(response.data)) throw new TypeError("TechFlow API returned an invalid article list."); return await Promise.all(response.data.map((article) => cache_default.tryGet(`techflowpost:article:${article.id}`, async () => { const itemLink = `${rootUrl}/${locale}/article/${article.id}`; return getArticleItem(article, (await requestApi(`/client/articles/${article.id}`, itemLink)).article?.content); }))); } async function getNewsflashItems(limit) { const response = await requestApi("/client/newsflashes", `${rootUrl}/${locale}/newsletter`, { page: 1, page_size: limit }); if (!Array.isArray(response.data)) throw new TypeError("TechFlow API returned an invalid newsflash list."); return await Promise.all(response.data.map((newsflash) => cache_default.tryGet(`techflowpost:newsflash:${newsflash.id}`, async () => { const itemLink = `${rootUrl}/${locale}/newsletter/${newsflash.id}`; return getNewsflashItem(newsflash, (await requestApi(`/client/newsflashes/${newsflash.id}`, itemLink)).content); }))); } //#endregion export { getNewsflashItems as n, rootUrl as r, getArticleItems as t };