rsshub
Version:
Make RSS Great Again!
171 lines (169 loc) • 4.94 kB
JavaScript
import { n as init_esm_shims, t as __dirname } from "./esm-shims-CzJ_djXG.mjs";
import { t as ofetch_default } from "./ofetch-BIyrKU3Y.mjs";
import { t as parseDate } from "./parse-date-BrP7mxXf.mjs";
import { t as cache_default } from "./cache-Bo__VnGm.mjs";
import { t as art } from "./render-BQo6B4tL.mjs";
import path from "node:path";
import { load } from "cheerio";
//#region lib/routes/tmtpost/util.ts
init_esm_shims();
const baseUrl = "https://www.tmtpost.com";
const apiBaseUrl = "https://api.tmtpost.com";
const postApiUrl = new URL("v1/posts/", apiBaseUrl).href;
const headers = { "app-version": "web1.0" };
const processItems = async (limit, query, apiUrl, targetUrl) => {
const response = await ofetch_default(apiUrl, {
query: {
limit,
...query
},
headers
});
const $ = load(await ofetch_default(targetUrl));
const language = $("html").attr("lang") ?? "zh-CN";
let items = [];
items = response.data.slice(0, limit).map((item) => {
const title$1 = item.title;
const description = art(path.join(__dirname, "templates/description-ca64252b.art"), { intro: item.summary });
const pubDate = item.time_published;
const linkUrl = item.share_link;
const guid = item.guid;
const image = item.thumb_image?.original?.url;
const updated = item.updated ?? pubDate;
return {
title: title$1,
description,
pubDate: pubDate ? parseDate(pubDate, "X") : void 0,
link: linkUrl,
guid,
id: guid,
content: {
html: description,
text: item.content ?? description
},
image,
banner: image,
updated: updated ? parseDate(updated, "X") : void 0,
language
};
});
items = (await Promise.all(items.map((item) => {
if (!item.link) return item;
return cache_default.tryGet(item.link, async () => {
const detailApiUrl = new URL(item.guid, postApiUrl).href;
const data = (await ofetch_default(detailApiUrl, {
query: { fields: [
"authors",
"tags",
"featured_image",
"categories",
"stock_list",
"big_plate",
"concept_plate",
"plate",
"plate_list",
"share_link"
].join(";") },
headers
})).data;
if (!data) return item;
const title$1 = data.title;
const description = art(path.join(__dirname, "templates/description-ca64252b.art"), {
intro: data.summary,
description: data.main
});
const pubDate = data.time_published;
const linkUrl = data.share_link;
const categories = [...new Set([
...data.categories ?? [],
...data.stock_list ?? [],
...data.big_plate ?? [],
...data.concept_plate ?? [],
...data.plate ?? [],
...data.plate_list ?? [],
...data.tags ?? []
].map((c) => c.title ?? c.name ?? c.tag).filter(Boolean))];
const authors = data.authors?.map((author$1) => ({
name: author$1.username,
url: new URL(`user/${author$1.guid}`, baseUrl).href,
avatar: author$1.avatar
}));
const guid = `tmtpost-${data.post_guid}`;
const image = data.images?.[0]?.url;
const updated = data.time_updated;
let processedItem = {
title: title$1,
description,
pubDate: pubDate ? parseDate(pubDate, "X") : void 0,
link: linkUrl ?? item.link,
category: categories,
author: authors,
guid,
id: guid,
content: {
html: description,
text: description
},
image,
banner: image,
updated: updated ? parseDate(updated, "X") : void 0,
language
};
const enclosureUrl = data.audio;
if (enclosureUrl) {
const enclosureType = `audio/${enclosureUrl.split(/\./).pop()}`;
const itunesDuration = data.duration;
processedItem = {
...processedItem,
enclosure_url: enclosureUrl,
enclosure_type: enclosureType,
enclosure_title: title$1,
enclosure_length: void 0,
itunes_duration: itunesDuration,
itunes_item_image: image
};
}
const medias = {};
if (data.full_size_images ?? data.images) {
const images = data.full_size_images ?? data.images;
for (const media of images) {
const url = media.url ?? media;
if (!url) continue;
const medium = "image";
const key = `${medium}${Object.values(medias).filter((m) => m.medium === medium).length + 1}`;
medias[key] = {
url,
medium,
title: title$1,
thumbnail: media.thumbnail ?? url
};
}
}
processedItem = {
...processedItem,
media: medias
};
return {
...item,
...processedItem
};
});
}))).filter((_) => true);
const title = $("title").text();
const author = title.split(/-/).pop();
return {
title,
description: $("meta[property=\"og:description\"]").attr("content"),
link: targetUrl,
item: items,
allowEmpty: true,
image: $("meta[property=\"og:image\"]").attr("content"),
author: title.split(/-/).pop(),
language,
itunes_author: author,
itunes_category: "Technology",
id: targetUrl
};
};
//#endregion
export { baseUrl as n, processItems as r, apiBaseUrl as t };