-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(/scu/scupi): Add route for SCUPI (#17542)
* Add route for SCUPI * Update _utils.ts Add image for each article
- Loading branch information
1 parent
1f05d49
commit 947429a
Showing
3 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { Namespace } from '@/types'; | ||
|
||
export const namespace: Namespace = { | ||
name: '四川大学', | ||
url: 'www.scu.edu.cn', | ||
lang: 'zh-CN', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import got from '@/utils/got'; | ||
import { load } from 'cheerio'; | ||
import { parseDate } from '@/utils/parse-date'; | ||
import { config } from '@/config'; | ||
|
||
export async function getNotifList() { | ||
try { | ||
const response = await got.get('https://scupi.scu.edu.cn/activities/notice', { | ||
headers: { | ||
'User-Agent': config.ua, | ||
}, | ||
}); | ||
const html = response.body; | ||
const $ = load(html); | ||
|
||
const listElement = $('body > div.wrapper > main > section > div > div > div.news > div > ul'); | ||
return listElement | ||
.find('article') | ||
.toArray() | ||
.map((articleElement) => { | ||
const titleElement = $(articleElement).find('li > div > div.news-text > h4 > a'); | ||
const timeElement = $(articleElement).find('li > div > div.news-text > span'); | ||
const imageElement = $(articleElement).find('li > div > div.news-img > a > img'); | ||
|
||
const link = titleElement.attr('href'); | ||
const title = titleElement.attr('title'); | ||
const pubDate = timeElement.text().trim(); | ||
|
||
return { | ||
title, | ||
link, | ||
itunes_item_image: imageElement.attr('src'), | ||
pubDate: parseDate(pubDate, 'YYYY-MM-DD'), | ||
}; | ||
}); | ||
} catch { | ||
// console.error(error); | ||
} | ||
|
||
return []; | ||
} | ||
|
||
export async function getArticle(item) { | ||
try { | ||
const response = await got.get(item.link, { | ||
headers: { | ||
'User-Agent': config.ua, | ||
}, | ||
}); | ||
const html = response.body; | ||
const $ = load(html); | ||
const articleContentElement = $('body > div > main > section > div > div > div.post-content-contaier > div'); | ||
const content = articleContentElement.html(); | ||
const modifiedContent = content?.replace(/\n/g, '<br>'); | ||
|
||
item.description = modifiedContent; | ||
return item; | ||
} catch { | ||
// console.error(error); | ||
} | ||
|
||
return item; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Warning: The author still knows nothing about javascript! | ||
|
||
import { getNotifList, getArticle } from './_utils'; | ||
import { Route } from '@/types'; | ||
import cache from '@/utils/cache'; | ||
|
||
export const route: Route = { | ||
path: '/scupi', | ||
categories: ['university'], | ||
example: '/scu/scupi', | ||
parameters: {}, | ||
features: { | ||
requireConfig: false, | ||
requirePuppeteer: false, | ||
antiCrawler: false, | ||
supportBT: false, | ||
supportPodcast: false, | ||
supportScihub: false, | ||
}, | ||
name: '匹兹堡学院通知', | ||
maintainers: ['sitdownkevin'], | ||
url: 'scupi.scu.edu.cn/activities/notice', | ||
handler, | ||
description: ``, | ||
}; | ||
|
||
async function handler() { | ||
// feed the data to rss | ||
const items = await getNotifList(); | ||
const itemsWithContent = await Promise.all(items.map((item) => cache.tryGet(item.link, () => getArticle(item)))); | ||
|
||
return { | ||
title: '四川大学匹兹堡学院', | ||
description: '四川大学匹兹堡学院官网通知', | ||
language: 'zh-cn', | ||
image: 'https://scupi.scu.edu.cn/wp-content/themes/scupi/img/logo.png', | ||
link: 'https://scupi.scu.edu.cn/', | ||
item: itemsWithContent, | ||
}; | ||
} |