A publisher api. & modded the news.vue file to use the publisherId instead of the name. & cleaned the code using prettier.

This commit is contained in:
yuanhau 2025-06-02 00:47:23 +08:00
parent 1dd324e0ca
commit 5392974261
6 changed files with 99 additions and 36 deletions

View file

@ -1,9 +1,48 @@
import * as cheerio from "cheerio";
export default defineEventHandler(async (event) => {
const slug = getRouterParam(event, "slug");
const buildUrl = "https://today.line.me/tw/v3/publisher/" + slug;
try {
const req = await fetch(buildUrl)
} catch (e) {}
})
const slug = getRouterParam(event, "slug");
const buildUrl = "https://today.line.me/tw/v3/publisher/" + slug;
try {
const req = await fetch(buildUrl, {
headers: {
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
Accept: "*",
"Accept-Language": "zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7",
"Accept-Encoding": "gzip, deflate, br",
Connection: "keep-alive",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "same-origin",
"Cache-Control": "max-age=0",
},
});
const data = await req.text();
const html = cheerio.load(data);
const newsOrgName = html("div.profileHead")
.text()
.replace(/.css-.*\}/, "");
const description = html("p.description").text();
const logo =
html("div.editor div figure img").attr("srcset") ||
html("div.editor div figure img").attr("src") ||
"";
const articles = [];
const otherArticles = html("section.moduleContainer div");
for (const item in otherArticles) {
}
return {
name: newsOrgName,
description: description,
logo: logo,
articles: []
};
} catch (e) {
console.log(e);
return {
error: "SERVER_SIDE_ERROR",
};
}
});

View file

@ -89,11 +89,12 @@ async function lineToday(slug: string) {
if (publishMatch) {
publishedAt = findTime(publishMatch[1].trim());
}
const findPublisherUrl = html("a.entityPublishInfo-avatarLink").attr("href") || "";
const publisherIdMatch = findPublisherUrl.match(/[0-9]{6}/);
const publisherId = publisherIdMatch ? publisherIdMatch[0] : "";
console.log(publisherId);
const findPublisherUrl =
html("a.entityPublishInfo-avatarLink").attr("href") || "";
const publisherIdMatch = findPublisherUrl.match(/[0-9]{6}/);
const publisherId = publisherIdMatch ? publisherIdMatch[0] : "";
console.log(publisherId);
return {
title: title,
@ -103,8 +104,8 @@ console.log(publisherId);
images: images,
updateat: updatedAt,
publishedat: publishedAt,
publisherId: publisherId
};
publisherId: publisherId,
};
}
export default lineToday;