Delete old components from a month ago? & Update draggable window to be using the native svgs by lucide icons & updated the news page to activate the tab changing animation when changing tabs & added caching into the [slug].ts file in publishers/lt & added a basic endpoint for searching for sources.

This commit is contained in:
yuanhau 2025-06-04 22:30:02 +08:00
parent 231a7ce251
commit 8032c3faae
12 changed files with 124 additions and 117 deletions

View file

@ -1,14 +0,0 @@
export default defineEventHandler(async (event) => {
const slug = getRouterParam(event, "slug");
const body = await readBody(event);
return {
body: body,
title: "News Org 1",
slug: "taisounds",
website: "https://yuanhau.com",
description: "wah wah wah wah wah wah I dont fucking care",
facebook: "https://www.facebook.csdkc",
logoUrl:
"https://cdn.discordapp.com/avatars/918723093646684180/4eecc27ac05ee8a701fa167808610c7a.jpg",
};
});

View file

@ -1,27 +0,0 @@
import sql from "~/server/components/postgres";
export default defineEventHandler(async (event) => {
const body = await readBody(event);
const query = getQuery(event);
/*const sources = await sql`SELECT * FROM sources`;
return sources;*/
// Fake data
return {
status: "ok",
data: [
{
id: 1,
title: "Source 1",
logo: "#",
url: "https://source1.com",
description: "Description for Source 1",
},
{
id: 2,
title: "Source 2",
logo: "#",
url: "https://source2.com",
description: "Description for Source 2",
},
],
};
});

View file

@ -0,0 +1,44 @@
import sql from "~/server/components/postgres";
const createUsers = await sql`
create table if not exists users (
uuid text primary key,
created_at timestamptz default current_timestamp,
username text not null unique,
avatarurl text,
firstname text,
passwordhash text not null,
email text
);
`;
const usersList = await sql`
create table if not exists usertokens (
token text not null primary key,
created_at timestamptz default current_timestamp,
username text not null,
email text,
avatarurl text,
firstname text
)
`;
const createUserAiChatHistory = await sql`
CREATE TABLE IF NOT EXISTS chat_history (
id SERIAL PRIMARY KEY,
uuid VARCHAR(255) NOT NULL,
role VARCHAR(50) NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)`;
const createSources = await sql``;
export default defineEventHandler(async (event) => {
return {
createUsers: createUsers,
usersList: usersList,
createUserAiChatHistory: createUserAiChatHistory,
createSources: createSources,
};
});

View file

@ -1,10 +0,0 @@
import s3 from "~/server/components/s3";
export default defineEventHandler(async (event) => {
const slug = getRouterParam(event, "slug");
return sendRedirect(
event,
`${process.env.S3_ENDPOINT}/${process.env.S3_BUCKETNAME}/${slug}`,
302,
);
});

View file

@ -1,8 +1,43 @@
// TODO Add caching
import sql from "~/server/components/postgres";
import * as cheerio from "cheerio";
// Caching
interface CacheItems {
slug: string;
title: string;
description: string;
articles: any[];
timestamp: number;
}
const CACHE_DURATION = 1000 * 60 * 30;
const cache: Record<string, CacheItems> = {};
function cleanupCache() {
const now = Date.now();
Object.keys(cache).forEach((key) => {
if (now - cache[key].timestamp > CACHE_DURATION) {
delete cache[key];
}
});
}
setInterval(cleanupCache, CACHE_DURATION);
export default defineEventHandler(async (event) => {
const slug = getRouterParam(event, "slug");
if (!slug) {
return {
error: "NO_SLUG_PROVIDED",
};
}
if (cache[slug] && Date.now() - cache[slug].timestamp < CACHE_DURATION) {
return {
...cache[slug],
cached: true,
};
}
const buildUrl = "https://today.line.me/tw/v3/publisher/" + slug;
try {
const req = await fetch(buildUrl, {
@ -25,37 +60,42 @@ export default defineEventHandler(async (event) => {
.text()
.replace(/.css-.*\}/, "");
const description = html("p.description").text();
const logoClue = html("div.editor").contents();
const logo =
logoClue.find("img").attr("srcset") ||
html("div.editor div figure img").attr("src") ||
"";
const bgImage = html("figure.keyVisual img").attr("srcset") || "";
const articles = [];
const regexArticleLinks = /[a-zA-Z0-9]{7}/g;
const otherArticles = <any[]>[];
html("a.ltcp-link").each((i, element) => {
const articleLink = html(element).attr("href");
const articleTitle = html(element).find("h3.header").text();
//const image = html(element).find("figure").attr("src");
console.log(html(element).find("img"));
console.log("----------");
const date = html(element)
.find("div._articleCard div.css-wqleh6 span")
.text();
if (articleLink && articleTitle) {
const articleSlug = articleLink.matchAll(regexArticleLinks);
const articleSlug = articleLink
.replaceAll("article", "")
.match(regexArticleLinks);
otherArticles.push({
index: i,
title: articleTitle,
link: articleSlug,
link: articleSlug[0],
date: date,
//image: image || "/geterrorassets/noImageLogo.svg",
});
}
});
cache[slug] = {
slug: slug,
title: newsOrgName,
description: description,
articles: otherArticles,
timestamp: Date.now(),
};
return {
title: newsOrgName,
description: description,
logo: logo,
articles: otherArticles,
logoClue: String(logoClue),
cached: false,
};
} catch (e) {
console.log(e);

View file

@ -0,0 +1,17 @@
import sql from "~/server/components/postgres";
export default defineEventHandler(async (event) => {
try {
const fetchDataInSQL = await sql`
SELECT * FROM lt_news_org;
`;
return {
data: fetchDataInSQL,
};
} catch (e) {
console.log(e);
return {
error: "SERVER_SIDE_ERR",
elogs: e.message,
};
}
});

View file

@ -1,3 +0,0 @@
export default defineEventHandler(async (event) => {
return {};
});