mirror of
https://github.com/hpware/news-analyze.git
synced 2025-06-24 05:24:23 +00:00
Add caching into the code to make it faster.
This commit is contained in:
parent
2d8628d0da
commit
bc9a63f6ab
3 changed files with 101 additions and 15 deletions
|
@ -1,5 +1,17 @@
|
|||
// Check /about/scraping_line_today_home.md for more info or https://news.yuanhau.com/datainfo/linetodayjsondata.json
|
||||
interface CacheItem {
|
||||
data: string[];
|
||||
timestamp: number;
|
||||
}
|
||||
const cache: Record<string, CacheItem> = {};
|
||||
const CACHE_DURATION = 1000 * 60 * 60; // 1 Hour
|
||||
|
||||
async function getLineTodayData(type: string) {
|
||||
if (cache[type] && Date.now() - cache[type].timestamp < CACHE_DURATION) {
|
||||
console.log("Serving from cache for type:", type);
|
||||
return cache[type].data;
|
||||
}
|
||||
|
||||
try {
|
||||
const buildUrl = `https://today.line.me/_next/data/v1/tw/v3/tab/${type}.json?tabs=${type}`;
|
||||
const req = await fetch(buildUrl, {
|
||||
|
@ -21,17 +33,42 @@ async function getLineTodayData(type: string) {
|
|||
req3.push(listing.id);
|
||||
}
|
||||
});
|
||||
} else if (listings && listings.id) {
|
||||
req3.push(listings.id);
|
||||
}
|
||||
});
|
||||
|
||||
cache[type] = {
|
||||
data: req3,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
return req3;
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
if (cache[type]) {
|
||||
console.log("Serving expired cache due to error");
|
||||
return cache[type].data;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function filterUUIDs(ids: string[]): string[] {
|
||||
const uuidPattern =
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
||||
return ids.filter((id) => uuidPattern.test(id));
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const slug = getRouterParam(event, "slug");
|
||||
return await getLineTodayData(slug);
|
||||
const query = getQuery(event);
|
||||
if (!query.query) {
|
||||
return {
|
||||
error: "NOT_A_QUERY",
|
||||
};
|
||||
}
|
||||
const data = await getLineTodayData(String(query.query));
|
||||
const validUUIDs = filterUUIDs(data || []);
|
||||
return {
|
||||
data: validUUIDs,
|
||||
cached: !!cache[String(query.query)],
|
||||
};
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue