rsshub
Version:
Make RSS Great Again!
121 lines (119 loc) • 5.25 kB
JavaScript
import { t as config } from "./config-C37vj7VH.mjs";
import { t as parseDate } from "./parse-date-BrP7mxXf.mjs";
import { t as cache_default } from "./cache-Bo__VnGm.mjs";
import { t as got_default } from "./got-KxxWdaxq.mjs";
import { t as invalid_parameter_default } from "./invalid-parameter-rr4AgGpp.mjs";
import { FetchError } from "ofetch";
import { load } from "cheerio";
//#region lib/routes/theinitium/utils.ts
const TOKEN = "Basic YW5vbnltb3VzOkdpQ2VMRWp4bnFCY1ZwbnA2Y0xzVXZKaWV2dlJRY0FYTHY=";
const processFeed = async (model, ctx) => {
const type = ctx.req.param("type") ?? "latest";
const language = ctx.req.param("language") ?? "zh-hans";
let listUrl;
let listLink;
switch (model) {
case "author":
listUrl = `https://api.theinitium.com/api/v2/author/?language=${language}&slug=${type}`;
listLink = `https://theinitium.com/author/${type}/`;
break;
case "follow":
listUrl = `https://api.theinitium.com/api/v2/user/follows/${type}/?language=${language}`;
listLink = `https://theinitium.com/follow/`;
break;
case "channel":
listUrl = `https://api.theinitium.com/api/v2/channel/articles/?language=${language}&slug=${type}`;
listLink = `https://theinitium.com/channel/${type}/`;
break;
case "tags":
listUrl = `https://api.theinitium.com/api/v2/tag/articles/?language=${language}&slug=${type}`;
listLink = `https://theinitium.com/tags/${type}/`;
break;
default: throw new invalid_parameter_default("wrong model");
}
const key = {
email: config.initium.username,
password: config.initium.password
};
const body = JSON.stringify(key);
let token;
const cacheIn = await cache_default.get("initium:token");
if (cacheIn) token = cacheIn;
else if (config.initium.bearertoken) {
token = config.initium.bearertoken;
cache_default.set("initium:token", config.initium.bearertoken);
} else if (key.email === void 0) token = TOKEN;
else {
token = "token " + (await got_default.post(`https://api.theinitium.com/api/v2/auth/login/?language=${language}`, {
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Connection: "keep-alive",
Authorization: TOKEN
},
body
})).data.token;
cache_default.set("initium:token", token);
}
const headers = {
Accept: "*/*",
Connection: "keep-alive",
Authorization: token
};
let response;
try {
response = await got_default(listUrl, { headers });
} catch (error) {
if (error instanceof FetchError && error.statusCode === 401) await cache_default.set("initium:token", "");
throw error;
}
const name = response.data.name || response.data[model] && response.data[model].name || "追踪";
const articles = response.data.results ?? response.data.digests;
const image = response.data[model] && (response.data[model].cover || response.data[model].avatar);
const getFullText = (slug) => cache_default.tryGet(`theinitium:${slug}:${language}`, async () => {
let content = "";
const { data } = await got_default(`https://api.theinitium.com/api/v2/article/detail/?language=${language}&slug=${slug}`, { headers });
if (data.lead.length) content += "<p>「" + data.lead + "」</p>";
if (data.byline.length) content += "<p>" + data.byline + "</p>";
if (data.content) content += data.content.replace("<figure class=\"advertisement\"/><br/>", "").replaceAll(/(?:<br>){2}-{11}<br>.*$/g, "");
else if (data.type === "html") content += "内容为空,请稍后再来";
else if (data.type === "web") {
const webcontent = load((await got_default(data.web.url)).body).html();
content += webcontent;
}
if (data.paywall_enabled) {
const google_bot_ua = "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.92 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
const accept_language = language + ";q=0.9";
const pay_content = load((await got_default(`https://theinitium.com/article/${slug}/`, { headers: {
"user-agent": google_bot_ua,
"accept-language": accept_language
} })).body)("div.paywall").html();
if (pay_content) content += pay_content.replace("<meta itemprop=\"isAccessibleForFree\" content=\"false\">", "");
}
return content;
});
const items = await Promise.all(articles.filter((a) => a.article).slice(0, token === TOKEN ? 25 : articles.length).map(async (item) => {
item.article.date = parseDate(item.article.date);
item.article.updated = parseDate(item.article.updated);
const description = await getFullText(item.article.slug);
return {
title: item.article.headline,
author: item.article.authors.length > 0 ? item.article.authors.map((x) => x.name).toString() : item.article.byline,
category: item.article.channels.filter((x) => !x.homepage).map((x) => x.name),
description,
link: new URL(item.article.url, "https://theinitium.com").href,
pubDate: item.article.date,
updated: item.article.updated,
guid: description.endsWith("内容为空,请稍后再来") ? item.article.uuid + "-I-am-empty" : item.article.uuid
};
}));
return {
title: `端传媒 - ${name}`,
link: listLink,
icon: "https://theinitium.com/misc/about/logo192.png",
item: items,
image
};
};
//#endregion
export { processFeed as t };