mirror of
https://github.com/hpware/news-analyze.git
synced 2025-06-23 13:04:23 +00:00
Update some stuff & remove unneed code like python stuff, it is in the
web app.
This commit is contained in:
parent
bc9a63f6ab
commit
62fa31ae4a
24 changed files with 104 additions and 2580 deletions
|
@ -1,10 +1,72 @@
|
|||
// 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));
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const data = await getUUID(String(query.query));
|
||||
const validUUIDs = filterUUIDs(data || []);
|
||||
const slug = getRouterParam(event, "slug");
|
||||
const urlBuild = "/api/home/uuid_lt/" + slug;
|
||||
const urlBuild = "/api/home/uuid_lt/action?query=" + String(slug.trim());
|
||||
const articleArray = [];
|
||||
const req = await fetch(urlBuild);
|
||||
const res = await req.text();
|
||||
const { data: res } = await useFetch(urlBuild);
|
||||
return res;
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
|
|
|
@ -6,7 +6,8 @@ interface CacheItem {
|
|||
const cache: Record<string, CacheItem> = {};
|
||||
const CACHE_DURATION = 1000 * 60 * 60; // 1 Hour
|
||||
|
||||
async function getLineTodayData(type: string) {
|
||||
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;
|
||||
|
@ -23,7 +24,7 @@ async function getLineTodayData(type: string) {
|
|||
},
|
||||
});
|
||||
const res = await req.json();
|
||||
const req2 = res.pageProps.fallback["getPageData,domestic"].modules;
|
||||
const req2 = res.pageProps.fallback[`getPageData,${type}`].modules;
|
||||
const req3 = [];
|
||||
req2.forEach((key) => {
|
||||
const listings = key.listings;
|
||||
|
@ -67,8 +68,16 @@ export default defineEventHandler(async (event) => {
|
|||
}
|
||||
const data = await getLineTodayData(String(query.query));
|
||||
const validUUIDs = filterUUIDs(data || []);
|
||||
const noDup = [];
|
||||
validUUIDs.forEach((key) => {
|
||||
if (noDup.includes(key)) {
|
||||
return;
|
||||
} else {
|
||||
noDup.push(key);
|
||||
}
|
||||
});
|
||||
return {
|
||||
data: validUUIDs,
|
||||
data: noDup,
|
||||
cached: !!cache[String(query.query)],
|
||||
};
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue