From 92312865411c3286e5cf603663251031937db87f Mon Sep 17 00:00:00 2001 From: Gexi0619 Date: Mon, 31 Mar 2025 20:10:00 +0200 Subject: [PATCH] feat(route): add /unipd/ilbolive/news (#18720) * get date from article * fix(route): apply review suggestions for ilbolive news route * fix(route): remove YouTube iframe handling for /ilbolive/news to resolve CodeQL alert * fix(route) --- lib/routes/unipd/ilbolive/news.ts | 104 ++++++++++++++++++++++++++++++ lib/routes/unipd/namespace.ts | 7 ++ 2 files changed, 111 insertions(+) create mode 100644 lib/routes/unipd/ilbolive/news.ts create mode 100644 lib/routes/unipd/namespace.ts diff --git a/lib/routes/unipd/ilbolive/news.ts b/lib/routes/unipd/ilbolive/news.ts new file mode 100644 index 000000000..32fd5ed33 --- /dev/null +++ b/lib/routes/unipd/ilbolive/news.ts @@ -0,0 +1,104 @@ +import { Route } from '@/types'; +import got from '@/utils/got'; +import cache from '@/utils/cache'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; +import timezone from '@/utils/timezone'; + +export const route: Route = { + path: '/ilbolive/news', + name: 'Il Bo Live - News', + url: 'ilbolive.unipd.it/it/news', + maintainers: ['Gexi0619'], + example: '/unipd/ilbolive/news', + parameters: {}, + description: 'Il Bo Live - News', + categories: ['university'], + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportRadar: true, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['ilbolive.unipd.it/it/news'], + target: '/ilbolive/news', + }, + ], + handler, +}; + +async function handler() { + const baseUrl = 'https://ilbolive.unipd.it'; + const homeUrl = `${baseUrl}/it/news`; + + const response = await got(homeUrl); + const $ = load(response.data); + + const items = $('#list-nodes .col.-s-6') + .toArray() + .map((el) => { + const item = $(el); + const title = item.find('.title a').text().trim(); + const href = item.find('.title a').attr('href'); + const link = baseUrl + href; + const category = item.find('.category').text().trim(); + const image = item.find('.photo img').attr('src'); + const imageUrl = baseUrl + image; + + return { + title, + link, + category, + enclosure_url: imageUrl, + enclosure_type: 'image/jpeg', + }; + }); + + const finalItems = await Promise.all( + items.map((item) => + cache.tryGet(item.link, async () => { + const detailResponse = await got(item.link); + const $ = load(detailResponse.data); + + const article = $('article.post-generic'); + + // Picture + article.find('img').each((_, el) => { + const img = $(el); + const src = img.attr('src'); + if (src && src.startsWith('/')) { + img.attr('src', baseUrl + src); + } + img.attr('style', 'max-width: 100%; height: auto;'); + }); + + const datetime = article.find('time.date').attr('datetime'); + const pubDate = datetime ? timezone(parseDate(datetime), 0) : undefined; + + const author = article.find('.author a').text().trim(); + + // Delete header + article.find('.header').remove(); + + return { + ...item, + description: article.html() ?? '', + pubDate, + author, + }; + }) + ) + ); + + return { + title: 'Il Bo Live - News', + link: homeUrl, + item: finalItems, + language: 'it', + }; +} diff --git a/lib/routes/unipd/namespace.ts b/lib/routes/unipd/namespace.ts new file mode 100644 index 000000000..51251e3ef --- /dev/null +++ b/lib/routes/unipd/namespace.ts @@ -0,0 +1,7 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'Università di Padova', + url: 'unipd.it', + lang: 'it', +};