Why the fuck is nuxt so laggy? I can't even do live changes.

This commit is contained in:
yuanhau 2025-05-22 17:21:32 +08:00
parent 109a39f6e9
commit 62833af4ae
4 changed files with 211 additions and 296 deletions

View file

@ -1,99 +0,0 @@
// 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(orgtype: string) {
const type = orgtype.toLowerCase();
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, {
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[`getPageData,${type}`].modules;
const req3 = [];
req2.forEach((key) => {
const listings = key.listings;
if (Array.isArray(listings)) {
listings.forEach((listing) => {
if (listing && listing.id) {
req3.push(listing.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 query = getQuery(event);
if (!query.query) {
return {
error: "NOT_A_QUERY",
};
}
const data = await getLineTodayData(String(query.query));
const validUUIDs = filterUUIDs(data || []);
const noDup = [];
validUUIDs.forEach((key) => {
if (noDup.includes(key)) {
return;
} else {
noDup.push({
type: "nuuid",
content: key,
});
}
});
const nonUUIDbutValidLinks = data.filter((id) =>
/.*:[a-zA-Z0-9]{24}/g.test(id),
);
nonUUIDbutValidLinks.forEach((key) => {
if (noDup.includes(key)) {
return;
} else {
noDup.push({
type: "vUUID",
content: key,
});
}
});
return {
data: noDup,
cached: !!cache[String(query.query)],
};
});

View file

@ -1,108 +0,0 @@
// 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 getUUID(orgtype: string) {
const type = orgtype.toLowerCase();
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, {
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[`getPageData,${type}`].modules;
const req3 = [];
req2.forEach((key) => {
const listings = key.listings;
if (Array.isArray(listings)) {
listings.forEach((listing) => {
if (listing && listing.id) {
req3.push(listing.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));
}
function filter2(ids: string[]): string[] {
const pattern = /.*:[a-zA-Z0-9]{24}/g;
return ids.filter((id) => pattern.test(id));
}
async function tryToPullDataUUID(uuid: string) {
const buildUrl =
"https://today.line.me/api/v6/listings/" +
uuid +
"?country=tw&offset=0&length=27";
const req = await fetch(buildUrl);
const res = await req.json();
return res;
}
export default defineEventHandler(async (event) => {
try {
const query = getQuery(event);
if (!query.query) {
return {
error: "NOT_A_QUERY",
};
}
const data = await getUUID(String(query.query));
const validUUIDs = filterUUIDs(data || []);
const diffFormat = [];
data.forEach((key) => {
if (validUUIDs.includes(key)) {
return;
} else {
diffFormat.push(key);
}
});
const returnData = [];
validUUIDs.forEach(async (key) => {
const data = await tryToPullDataUUID(key);
returnData.push(data);
});
const fillll = filter2(diffFormat);
return { valid: validUUIDs, maybe: fillll, returnData: returnData };
} catch (e) {
console.log(e);
return {
error: "SERVER_SIDE_ERROR",
};
}
});

View file

@ -1,3 +1,115 @@
export default defineEventHandler((event) => {
return sendRedirect(event, "/datainfo/linetodayjsondata.json", 302);
// 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 getUUID(orgtype: string) {
const type = orgtype.toLowerCase();
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, {
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[`getPageData,${type}`].modules;
const req3 = [];
req2.forEach((key) => {
const listings = key.listings;
if (Array.isArray(listings)) {
listings.forEach((listing) => {
if (listing && listing.id) {
req3.push(listing.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));
}
function filter2(ids: string[]): string[] {
const pattern = /.*:[a-zA-Z0-9]{24}/g;
return ids.filter((id) => pattern.test(id));
}
async function tryToPullDataUUID(uuid: string) {
const buildUrl =
"https://today.line.me/api/v6/listings/" +
uuid +
"?country=tw&offset=0&length=27";
const req = await fetch(buildUrl);
const res = await req.json();
return res;
}
export default defineEventHandler(async (event) => {
try {
const query = getQuery(event);
if (!query.query) {
return {
error: "NOT_A_QUERY",
};
}
const data = await getUUID(String(query.query));
const validUUIDs = filterUUIDs(data || []);
const uniqueUUIDs = [...new Set(validUUIDs)];
const returnData: any[] = await Promise.all(
uniqueUUIDs.map(async (key) => {
const data = await tryToPullDataUUID(key);
return data;
}),
);
const diffFormat = [];
data.forEach((key) => {
if (validUUIDs.includes(key)) {
return;
} else {
diffFormat.push(key);
}
});
const fillll = filter2(diffFormat);
return {
valid: uniqueUUIDs,
maybe: fillll,
returnData: returnData,
};
} catch (e) {
console.log(e);
return {
error: "SERVER_SIDE_ERROR",
};
}
});