Add a few more database tables & made the api pull from the database

when then article exists & not ddos line today, Also made a basic
newsView with the api & the scraping script now scraps more stuff! :D
This commit is contained in:
yuanhau 2025-05-18 11:59:58 +08:00
parent fe5e2d996e
commit b62a3cda3d
6 changed files with 84 additions and 40 deletions

View file

@ -0,0 +1,11 @@
<script setup lang="ts">
const { data, error, pending } = useFetch("/api/get/lt/kEJjxKw"); //demo URL
</script>
<template>
<div class="justify-center align-center text-center flex flex-col">
<h2 class="text-3xl text-bold">{{ data.title }}</h2>
<span class="text-lg text-bold"
>origin: {{ data.origin }} author: {{ data.author }}</span
>
</div>
</template>

View file

@ -38,13 +38,13 @@ CREATE TABLE IF NOT EXISTS chat_history (
const newsArticles = await sql`
create table if not exists news_articles (
uuid text primary key,
title text not null,
content text not null,
news_org text not null,
origin_link text not null,
author text,
related_uuid text not null
uuid text primary key,
title text not null,
content text not null,
news_org text not null,
origin_link text not null,
author text,
related_uuid text not null
)
`;
@ -59,6 +59,16 @@ create table if not exists hot_news (
)
`;
const articlesLt = await sql`
create table if not exists articles_lt (
uuid text primary key,
title text not null,
content text not null,
origin text not null,
author text,
)
`;
console.log("Creation Complete");
await sql.end();

View file

@ -1,28 +0,0 @@
import sql from "~/server/components/postgres";
export default defineEventHandler(async (event) => {
const slug = getRouterParam(event, "slug");
// Validate and sanitize the slug
if (!slug || typeof slug !== "string") {
throw createError({
statusCode: 400,
message: "Invalid slug parameter",
});
}
const cleanSlug = slug.replace(/[^a-zA-Z0-9-_]/g, "");
try {
const result = await sql`
select * from articles
where slug = ${cleanSlug}
`;
return result.rows[0] || null;
} catch (error) {
console.error("Database error:", error);
throw createError({
statusCode: 500,
message: "Internal server error",
});
}
});

View file

@ -1,6 +1,28 @@
import lineToday from "~/server/scrape/line_today";
import sql from "~/server/components/postgres";
import saveDataToSql from "~/server/scrape/save_scrape_data";
function cleanUpSlug(orgslug: string) {
let slug = dirtySlug.trim();
const validSlugRegex = /^[a-zA-Z0-9-]+$/;
if (!validSlugRegex.test(slug)) {
throw new Error("Invalid slug format");
}
return slug;
}
export default defineEventHandler(async (event) => {
const slug = getRouterParam(event, "slug");
const data = await lineToday(slug);
return data;
const cleanSlug = await cleanUpSlug(slug);
const result = await sql`
select * from articles_lt
where slug = ${cleanSlug}
`;
if (result) {
return result;
} else {
const data = await lineToday(slug);
saveDataToSql(data, slug);
return data;
}
});

View file

@ -21,16 +21,30 @@ async function lineToday(slug: string) {
// 加 await? no.
// AHHH I NEED TO CHANGE TO SOMETHING ELSE.
const html = cheerio.load(data);
const title = html("h1.entityTitle").text().replaceAll("\n", "");
const title = html("h1.entityTitle")
.text()
.replaceAll("\n", "")
.replace(" ", "");
const paragraph = html("article.news-content").text();
const newsOrgdir = html("h4.entityPublishInfo-publisher")
.text()
.replaceAll("\n", "")
.replaceAll(" ", "");
const author = html("span.entityPublishInfo-meta-info")
.text()
.replace(/更新於.*發布於.*•/g, "")
.replaceAll("\n", "")
.replaceAll(" ", "");
return {
title: title,
paragraph: paragraph,
origin: newsOrgdir,
author: author,
};
}
// Texting on console only!
//console.log(await lineToday("oqmazXP"));
//console.log(await lineToday("kEJjxKw"));
export default lineToday;

View file

@ -0,0 +1,15 @@
import postgres from "~/server/components/postgres";
import { v4 as uuidv4 } from "uuid";
async function saveDataToSql(
data: { title: string; paragraph: string; author: string; origin: string },
slug: string,
) {
const sql = postgres;
await sql`
INSERT INTO articles_lt (uuid, slug, title, content, author, origin)
VALUES (${uuidv4()}, ${slug}, ${data.title}, ${data.paragraph}, ${data.author}, ${data.origin})
ON CONFLICT (slug) DO NOTHING
`;
}
export default saveDataToSql;