rsshub
Version:
Make RSS Great Again!
154 lines (152 loc) • 6.11 kB
JavaScript
import { n as init_esm_shims, t as __dirname } from "./esm-shims-CzJ_djXG.mjs";
import { t as config } from "./config-C37vj7VH.mjs";
import "./dist-BInvbO1W.mjs";
import "./logger-Czu8UMNd.mjs";
import "./ofetch-BIyrKU3Y.mjs";
import { t as parseDate } from "./parse-date-BrP7mxXf.mjs";
import { t as cache_default } from "./cache-Bo__VnGm.mjs";
import "./helpers-DxBp0Pty.mjs";
import { t as art } from "./render-BQo6B4tL.mjs";
import { t as got_default } from "./got-KxxWdaxq.mjs";
import { t as timezone } from "./timezone-D8cuwzTY.mjs";
import { FetchError } from "ofetch";
import path from "node:path";
import { load } from "cheerio";
//#region lib/routes/lorientlejour/index.ts
init_esm_shims();
const key = "3d5_f6A(S$G_FD=2S(Dr6%7BW_h37@rE";
const route = {
path: "/:category?",
categories: ["traditional-media"],
example: "/lorientlejour/977-lebanon",
parameters: { category: "Category from the last segment of the URL of the corresponding site, see below for more information, /977-Lebanon by default" },
features: {
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
requireConfig: [
{
name: "LORIENTLEJOUR_USERNAME",
optional: true,
description: `L'Orient-Le Jour/L'Orient Today Email or Username`
},
{
name: "LORIENTLEJOUR_PASSWORD",
optional: true,
description: `L'Orient-Le Jour/L'Orient Today Password`
},
{
name: "LORIENTLEJOUR_TOKEN",
optional: true,
description: `To obtain a token, log into L'Orient-Le Jour/L'Orient Today App and inspect the connection request to find the token parameter from the request URL`
}
]
},
name: "Category",
maintainers: ["quiniapiezoelectricity"],
handler,
description: ` ::: tip
For example, the path for the sites https://today.lorientlejour.com/section/977-lebanon and https://www.lorientlejour.com/rubrique/1-liban would be /lorientlejour/977-lebanon and /lorientlejour/1-liban respectively.
Multiple categories seperated by '|' is also supported, e.g. /lorientlejour/977-lebanon|1-liban.
:::`,
radar: [
{
source: ["www.lorientlejour.com/*/:category"],
target: "/:category"
},
{
source: ["www.lorientlejour.com"],
target: "/1-Liban"
},
{
source: ["today.lorientlejour.com/*/:category"],
target: "/:category"
},
{
source: ["today.lorientlejour.com"],
target: "/977-Lebanon"
}
]
};
async function viewCategory(category) {
const url = `https://www.lorientlejour.com/cmsapi/categories.php?key=${key}&action=view&categoryId=${category}`;
return (await cache_default.tryGet(url, async () => await got_default({
method: "get",
url
}), config.cache.routeExpire, false)).data.data[0];
}
async function handler(ctx) {
const categoryId = (ctx.req.param("category") ?? "977-Lebanon").split("|").map((item) => item.match(/^(\d+)/i)[0] ?? item);
const limit = ctx.req.query("limit") ?? 25;
let token;
const cacheIn = await cache_default.get("lorientlejour:token");
if (cacheIn) token = cacheIn;
else if (config.lorientlejour.token) {
token = config.lorientlejour.token;
cache_default.set("lorientlejour:token", token);
} else if (config.lorientlejour.username && config.lorientlejour.password) {
token = (await got_default(`https://www.lorientlejour.com/cmsapi/visitors.php?key=${key}&action=login&loginName=${config.lorientlejour.username}&password=${config.lorientlejour.password}`)).data.data.token;
cache_default.set("lorientlejour:token", token);
}
if (token) try {
await got_default(`https://www.lorientlejour.com/cmsapi/visitors.php?key=${key}&action=login_token&token=${token}`);
} catch (error) {
if (error instanceof FetchError && error.statusCode === 403) await cache_default.set("lorientlejour:token", "");
throw error;
}
let title = `L'Orient Le Jour/L'Orient Today`;
let description = "";
let link = "https://www.lorientlejour.com";
let language = "";
if (categoryId.length === 1) {
const categoryInfo = await viewCategory(categoryId[0]);
if (categoryInfo.typeId.locale) language = categoryInfo.typeId.locale;
else language = categoryInfo.typeId.name === "English" ? "en-US" : "fr-FR";
title = language === "en-US" ? `L'Orient Today - ${categoryInfo.name}` : `L'Orient Le Jour - ${categoryInfo.name}`;
description = categoryInfo.description;
link = categoryInfo.url;
}
const subcategories = await Promise.all(categoryId.map(async (id) => {
return (await viewCategory(id)).children.map((child) => child.id);
}));
const categoriesParam = [...new Set([...categoryId, ...subcategories.flat()])];
let url = `https://www.lorientlejour.com/cmsapi/content.php?text=clean&key=${key}&action=search&category=${encodeURIComponent(JSON.stringify(categoriesParam))}&limit=${limit}&text=false&page=1`;
if (token) url = url + `&token=${token}`;
const items = (await got_default(url)).data.data.map((item) => {
item.link = item.url;
item.author = item.authors.map((author) => author.name).join(", ");
item.pubDate = timezone(parseDate(item.firstPublished), 3);
item.updated = timezone(parseDate(item.lastUpdate), 3);
item.category = item.categories.map((itemCategory) => itemCategory.name);
const contents = item.contents;
const $ = load(contents);
const article = $("html");
article.find(".inline-embeded-article").remove();
article.find(".relatedArticles").remove();
if (item.inline_attachments) article.find(".inlineImage").each(function() {
const inlineImageSrc = $(this).attr("src");
const inlineAttachment = item.inline_attachments.find((inlineAttachment$1) => inlineAttachment$1.url === inlineImageSrc);
if (inlineAttachment && inlineAttachment.description) {
$(this).wrap("<figure></figure>");
$(this).after(`<figcaption>${inlineAttachment.description}</figcaption>`);
}
});
item.description = art(path.join(__dirname, "templates/description-b70dec08.art"), {
summary: item.summary,
attachments: item.attachments,
article: article.html()
});
return item;
});
return {
title,
description,
language,
link,
item: items
};
}
//#endregion
export { route };