From 0ed7ca8f80d26cd753ba4f146ff7c77d80e4e1fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B3=E5=85=83=E7=9A=93?= Date: Mon, 26 May 2025 11:39:19 +0800 Subject: [PATCH] New Tabs, this includes the FULL caching feat, w/ a kill switch in the source code, if I want to test something when I don't want the data to be cached. & BLOCK unneeded info like expireMode is NOT OFF or the pageType is NOT GENERAL. --- server/api/tabs.ts | 64 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/server/api/tabs.ts b/server/api/tabs.ts index cbeaeef..a91d60b 100644 --- a/server/api/tabs.ts +++ b/server/api/tabs.ts @@ -1,34 +1,64 @@ +const cacheEnabled = true; let cachedData: { data: string[]; timestamp: number } | null = null; -const CACHE_DURATION = 1000 * 60 * 60; // 1 Hour +const CACHE_DURAION = 1000 * 60 * 60; // 1 Hour export default defineEventHandler(async (event) => { - if (cachedData && Date.now() - cachedData.timestamp < CACHE_DURATION) { + if ( + cachedData && + Date.now() - cachedData.timestamp < CACHE_DURATION && + cacheEnabled + ) { return { data: cachedData.data, - cached: true + cached: true, }; } - try { - const req = await fetch("https://today.line.me/_next/data/v1/tw/v3/tab/domestic.json?tabs=domestic", { - headers: { - "Accept-Encoding": "gzip, deflate, br", - Accept: "application/json", - "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", + try { + const req = await fetch( + "https://today.line.me/_next/data/v1/tw/v3/tab/domestic.json?tabs=domestic", + { + headers: { + "Accept-Encoding": "gzip, deflate, br", + Accept: "application/json", + "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", + }, }, - }); + ); const res = await req.json(); - const req2 = res.pageProps.fallback.getTabsData.modules; + const req2 = res.pageProps.fallback.getTabsData.modules[0].tabs; + const newArray = []; + var first = true; + for (const key in req2) { + const item = req2[key]; + if (item.expireMode === "OFF" && item.link.pageType === "GENERAL") { + newArray.push({ + text: item.link.page.name, + url: item.link.page.urlPath, + default: first, + }); + } + if (first === true) { + first = false; + } + } cachedData = { - data: req2, + data: newArray, timestamp: Date.now(), }; return { - data: req2, - cached: false + data: newArray, + cached: false, }; } catch (e) { - return { - "error": "INTERNAL_SERVER_ERROR" + console.log(e); + if (cachedData && cacheEnabled) { + return { + data: cachedData.data, + cached: true, + }; } + return { + error: "INTERNAL_SERVER_ERROR", + }; } });