From c0638543b756ad78f188dc85d2a0caa86330fe84 Mon Sep 17 00:00:00 2001 From: Andvari <31068367+dzx-dzx@users.noreply.github.com> Date: Sat, 22 Jun 2024 21:51:43 +0800 Subject: [PATCH] fix(route/apnews): Remove duplicate (#15966) --- lib/routes/apnews/topics.ts | 4 ++-- lib/routes/apnews/utils.ts | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/routes/apnews/topics.ts b/lib/routes/apnews/topics.ts index 283c8e083..dc211d732 100644 --- a/lib/routes/apnews/topics.ts +++ b/lib/routes/apnews/topics.ts @@ -1,7 +1,7 @@ import { Route } from '@/types'; import got from '@/utils/got'; import { load } from 'cheerio'; -import { fetchArticle } from './utils'; +import { fetchArticle, removeDuplicateByKey } from './utils'; const HOME_PAGE = 'https://apnews.com'; export const route: Route = { @@ -50,7 +50,7 @@ async function handler(ctx) { title: $('title').text(), description: $("meta[property='og:description']").text(), link: url, - item: items, + item: removeDuplicateByKey(items, 'guid'), language: $('html').attr('lang'), }; } diff --git a/lib/routes/apnews/utils.ts b/lib/routes/apnews/utils.ts index a6c239223..c8b10545e 100644 --- a/lib/routes/apnews/utils.ts +++ b/lib/routes/apnews/utils.ts @@ -3,6 +3,10 @@ import ofetch from '@/utils/ofetch'; import { parseDate } from '@/utils/parse-date'; import { load } from 'cheerio'; +export function removeDuplicateByKey(items, key: string) { + return [...new Map(items.map((x) => [x[key], x])).values()]; +} + export function fetchArticle(item) { return cache.tryGet(item.link, async () => { const data = await ofetch(item.link);