mirror of
https://github.com/hpware/news-analyze.git
synced 2025-06-24 00:01:03 +08:00
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:
parent
fe5e2d996e
commit
b62a3cda3d
11
components/app/windows/newsView.vue
Normal file
11
components/app/windows/newsView.vue
Normal 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>
|
@ -38,13 +38,13 @@ CREATE TABLE IF NOT EXISTS chat_history (
|
|||||||
|
|
||||||
const newsArticles = await sql`
|
const newsArticles = await sql`
|
||||||
create table if not exists news_articles (
|
create table if not exists news_articles (
|
||||||
uuid text primary key,
|
uuid text primary key,
|
||||||
title text not null,
|
title text not null,
|
||||||
content text not null,
|
content text not null,
|
||||||
news_org text not null,
|
news_org text not null,
|
||||||
origin_link text not null,
|
origin_link text not null,
|
||||||
author text,
|
author text,
|
||||||
related_uuid text not null
|
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");
|
console.log("Creation Complete");
|
||||||
|
|
||||||
await sql.end();
|
await sql.end();
|
||||||
|
@ -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",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
@ -1,6 +1,28 @@
|
|||||||
import lineToday from "~/server/scrape/line_today";
|
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) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const slug = getRouterParam(event, "slug");
|
const slug = getRouterParam(event, "slug");
|
||||||
|
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);
|
const data = await lineToday(slug);
|
||||||
|
saveDataToSql(data, slug);
|
||||||
return data;
|
return data;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
@ -21,16 +21,30 @@ async function lineToday(slug: string) {
|
|||||||
// 加 await? no.
|
// 加 await? no.
|
||||||
// AHHH I NEED TO CHANGE TO SOMETHING ELSE.
|
// AHHH I NEED TO CHANGE TO SOMETHING ELSE.
|
||||||
const html = cheerio.load(data);
|
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 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 {
|
return {
|
||||||
title: title,
|
title: title,
|
||||||
paragraph: paragraph,
|
paragraph: paragraph,
|
||||||
|
origin: newsOrgdir,
|
||||||
|
author: author,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Texting on console only!
|
// Texting on console only!
|
||||||
//console.log(await lineToday("oqmazXP"));
|
//console.log(await lineToday("kEJjxKw"));
|
||||||
|
|
||||||
export default lineToday;
|
export default lineToday;
|
||||||
|
15
server/scrape/save_scrape_data.ts
Normal file
15
server/scrape/save_scrape_data.ts
Normal 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;
|
Loading…
x
Reference in New Issue
Block a user