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", + }; } });