-
Notifications
You must be signed in to change notification settings - Fork 7.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(route): add route for university: CNU JWC #17709
Open
Aicnal
wants to merge
10
commits into
DIYgod:master
Choose a base branch
from
Aicnal:feature/cnu-route
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+86
−0
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4f3f713
New feature: Add new route of CNU
Aicnal 22ea2c7
Update: Remove img
Aicnal bad1499
Update: Using to.Array() before .map()
Aicnal c898451
Update: Fix
Aicnal f9fab65
Update: Fix of first PR
Aicnal b23c731
Update: namespace 's link
Aicnal 9ece9f8
Update: Simply select element
Aicnal 90e1116
Update: Fix
Aicnal ab3b2ee
Update: Fix
Aicnal e928d7b
Update: Fix
Aicnal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,79 @@ | ||
import { Route } from '@/types'; | ||
import { parseDate } from '@/utils/parse-date'; | ||
import got from '@/utils/got'; | ||
import { load } from 'cheerio'; | ||
import cache from '@/utils/cache'; | ||
|
||
const BASE_URL = 'https://jwc.cnu.edu.cn/tzgg/index.htm'; | ||
|
||
export const route: Route = { | ||
path: '/jwc', | ||
categories: ['university'], | ||
example: '/cnu/jwc', | ||
radar: [ | ||
{ | ||
source: [new URL(BASE_URL).host], | ||
}, | ||
], | ||
name: '教务处', // Name of the route | ||
maintainers: ['Aicnal'], | ||
handler, | ||
url: new URL(BASE_URL).host + new URL(BASE_URL).pathname, // host + pathname | ||
}; | ||
|
||
async function handler() { | ||
const response = await got({ method: 'get', url: BASE_URL }); | ||
const $ = load(response.data); | ||
|
||
const list = $('li') | ||
.toArray() // Convert to an array first | ||
.map((e) => { | ||
const element = $(e); | ||
const rawTitle = element.find('a').text().trim(); | ||
const dateRegex = /^(\d{1,2})\s+(\d{4})-(\d{1,2})/; | ||
const match = rawTitle.match(dateRegex); | ||
|
||
if (!match) { | ||
return null; | ||
} | ||
|
||
const [, day, year, month] = match; | ||
const pubDate = parseDate(`${year}-${month}-${day}`, 'YYYY-MM-DD'); | ||
const title = rawTitle | ||
.replace(dateRegex, '') | ||
.trim() | ||
.replaceAll(/(公众|教师|学生)/g, '') | ||
.trim(); | ||
const href = element.find('a').attr('href') ?? ''; | ||
const link = href.startsWith('http') ? href : new URL(href, BASE_URL).href; | ||
|
||
return { title, link, pubDate }; | ||
}) | ||
.filter(Boolean); | ||
|
||
const items = await Promise.all( | ||
list.map((item) => | ||
cache.tryGet(item.link, async () => { | ||
// Cache the detail page | ||
const detailResponse = await got({ method: 'get', url: item.link }); | ||
const content = load(detailResponse.data); | ||
const paragraphs = content('.article02') // with `.article02` | ||
.toArray() | ||
.map((el) => content(el).html()?.trim()) | ||
.join('<br/><br/>'); | ||
|
||
return { | ||
...item, | ||
description: paragraphs || '暂无内容', | ||
}; | ||
}) | ||
) | ||
); | ||
|
||
return { | ||
title: '首都师范大学教务信息', | ||
link: BASE_URL, | ||
description: '首都师范大学教务处的最新通知公告', | ||
item: items, | ||
}; | ||
} |
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: 'cnu.edu.cn', | ||
lang: 'zh-CN', | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
li
include way too many unrelated elements