|
| 1 | +import { type Data, type DataItem, type Route, ViewType } from '@/types'; |
| 2 | + |
| 3 | +import cache from '@/utils/cache'; |
| 4 | +import ofetch from '@/utils/ofetch'; |
| 5 | +import { parseDate } from '@/utils/parse-date'; |
| 6 | +import timezone from '@/utils/timezone'; |
| 7 | + |
| 8 | +import { type CheerioAPI, type Cheerio, load } from 'cheerio'; |
| 9 | +import type { Element } from 'domhandler'; |
| 10 | +import { type Context } from 'hono'; |
| 11 | + |
| 12 | +export const handler = async (ctx: Context): Promise<Data> => { |
| 13 | + const { category = 'association-news' } = ctx.req.param(); |
| 14 | + const limit: number = Number.parseInt(ctx.req.query('limit') ?? '10', 10); |
| 15 | + |
| 16 | + const baseUrl: string = 'http://www.ccagm.org.cn'; |
| 17 | + const targetUrl: string = new URL(category, baseUrl).href; |
| 18 | + |
| 19 | + const response = await ofetch(targetUrl); |
| 20 | + const $: CheerioAPI = load(response); |
| 21 | + const language = $('html').attr('lang') ?? 'zh'; |
| 22 | + |
| 23 | + let items: DataItem[] = []; |
| 24 | + |
| 25 | + items = $('ul.news_list li a') |
| 26 | + .slice(0, limit) |
| 27 | + .toArray() |
| 28 | + .map((el): Element => { |
| 29 | + const $el: Cheerio<Element> = $(el); |
| 30 | + |
| 31 | + const title: string = $el.attr('title') ?? $el.find('span.fl').text(); |
| 32 | + const pubDateStr: string | undefined = $el.find('span.fr').text(); |
| 33 | + const linkUrl: string | undefined = $el.attr('href'); |
| 34 | + const upDatedStr: string | undefined = pubDateStr; |
| 35 | + |
| 36 | + const processedItem: DataItem = { |
| 37 | + title, |
| 38 | + pubDate: pubDateStr ? timezone(parseDate(pubDateStr), +8) : undefined, |
| 39 | + link: linkUrl, |
| 40 | + updated: upDatedStr ? timezone(parseDate(upDatedStr), +8) : undefined, |
| 41 | + language, |
| 42 | + }; |
| 43 | + |
| 44 | + return processedItem; |
| 45 | + }); |
| 46 | + |
| 47 | + items = await Promise.all( |
| 48 | + items.map((item) => { |
| 49 | + if (!item.link) { |
| 50 | + return item; |
| 51 | + } |
| 52 | + |
| 53 | + return cache.tryGet(item.link, async (): Promise<DataItem> => { |
| 54 | + const detailResponse = await ofetch(item.link); |
| 55 | + const $$: CheerioAPI = load(detailResponse); |
| 56 | + |
| 57 | + const title: string = $$('h2.center').text(); |
| 58 | + const description: string | undefined = $$('div.newsview').html() ?? undefined; |
| 59 | + const pubDateStr: string | undefined = $$('p.title_s').text().trim().split(/:/).pop(); |
| 60 | + const upDatedStr: string | undefined = pubDateStr; |
| 61 | + |
| 62 | + const processedItem: DataItem = { |
| 63 | + title, |
| 64 | + description, |
| 65 | + pubDate: pubDateStr ? timezone(parseDate(pubDateStr), +8) : item.pubDate, |
| 66 | + content: { |
| 67 | + html: description, |
| 68 | + text: description, |
| 69 | + }, |
| 70 | + updated: upDatedStr ? timezone(parseDate(upDatedStr), +8) : item.updated, |
| 71 | + language, |
| 72 | + }; |
| 73 | + |
| 74 | + return { |
| 75 | + ...item, |
| 76 | + ...processedItem, |
| 77 | + }; |
| 78 | + }); |
| 79 | + }) |
| 80 | + ); |
| 81 | + |
| 82 | + return { |
| 83 | + title: `${$('title').text()}${$('span.titlespan').text() ? ` - ${$('span.titlespan').text()}` : ''}`, |
| 84 | + description: $('meta[name="description"]').attr('content'), |
| 85 | + link: targetUrl, |
| 86 | + item: items, |
| 87 | + allowEmpty: true, |
| 88 | + image: $('a.logo img').attr('src'), |
| 89 | + author: $('meta[name="keywords"]').attr('content'), |
| 90 | + language, |
| 91 | + id: targetUrl, |
| 92 | + }; |
| 93 | +}; |
| 94 | + |
| 95 | +export const route: Route = { |
| 96 | + path: '/:category{.+}?', |
| 97 | + name: '栏目', |
| 98 | + url: 'www.ccagm.org.cn', |
| 99 | + maintainers: ['nczitzk'], |
| 100 | + handler, |
| 101 | + example: '/ccagm/association-news', |
| 102 | + parameters: { |
| 103 | + category: { |
| 104 | + description: '分类,默认为 `association-news`,即协会动态,可在对应分类页 URL 中找到', |
| 105 | + options: [ |
| 106 | + { |
| 107 | + label: '协会动态', |
| 108 | + value: 'association-news', |
| 109 | + }, |
| 110 | + { |
| 111 | + label: '会议活动', |
| 112 | + value: 'xh-activity/activities-huiyi', |
| 113 | + }, |
| 114 | + { |
| 115 | + label: '调研与报告', |
| 116 | + value: 'xh-activity/bg-yj', |
| 117 | + }, |
| 118 | + { |
| 119 | + label: '协会党建', |
| 120 | + value: 'xie-hui-dang-jian', |
| 121 | + }, |
| 122 | + { |
| 123 | + label: '行业新闻', |
| 124 | + value: 'members-info', |
| 125 | + }, |
| 126 | + { |
| 127 | + label: '行业研究', |
| 128 | + value: 'bg-yj', |
| 129 | + }, |
| 130 | + { |
| 131 | + label: '行业标准', |
| 132 | + value: 'industry-policy/industry-standard', |
| 133 | + }, |
| 134 | + { |
| 135 | + label: '法律法规', |
| 136 | + value: 'industry-policy/policies-regulations', |
| 137 | + }, |
| 138 | + { |
| 139 | + label: '资料下载', |
| 140 | + value: 'download', |
| 141 | + }, |
| 142 | + { |
| 143 | + label: '工作总结与计划', |
| 144 | + value: 'about-association/gong-zuo-zong-jie-yu-ji-hua', |
| 145 | + }, |
| 146 | + ], |
| 147 | + }, |
| 148 | + }, |
| 149 | + description: `:::tip |
| 150 | +订阅 [协会动态](http://www.ccagm.org.cn/association-news),其源网址为 \`http://www.ccagm.org.cn/association-news\`,请参考该 URL 指定部分构成参数,此时路由为 [\`/ccagm/association-news\`](https://rsshub.app/ccagm/association-news)。 |
| 151 | +::: |
| 152 | +
|
| 153 | +<details> |
| 154 | + <summary>更多分类</summary> |
| 155 | +
|
| 156 | + | 栏目 | ID | |
| 157 | + | -------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- | |
| 158 | + | [协会动态](http://www.ccagm.org.cn/association-news.html) | [association-news](https://rsshub.app/ccagm/association-news) | |
| 159 | + | [会议活动](http://www.ccagm.org.cn/xh-activity/activities-huiyi.html) | [xh-activity/activities-huiyi](https://rsshub.app/ccagm/xh-activity/activities-huiyi) | |
| 160 | + | [调研与报告](http://www.ccagm.org.cn/xh-activity/bg-yj.html) | [xh-activity/bg-yj](https://rsshub.app/ccagm/xh-activity/bg-yj) | |
| 161 | + | [协会党建](http://www.ccagm.org.cn/xie-hui-dang-jian.html) | [xie-hui-dang-jian](https://rsshub.app/ccagm/xie-hui-dang-jian) | |
| 162 | + | [行业新闻](http://www.ccagm.org.cn/members-info.html) | [members-info](https://rsshub.app/ccagm/members-info) | |
| 163 | + | [行业研究](http://www.ccagm.org.cn/bg-yj.html) | [bg-yj](https://rsshub.app/ccagm/bg-yj) | |
| 164 | + | [行业标准](http://www.ccagm.org.cn/industry-policy/industry-standard.html) | [industry-policy/industry-standard](https://rsshub.app/ccagm/industry-policy/industry-standard) | |
| 165 | + | [法律法规](http://www.ccagm.org.cn/industry-policy/policies-regulations.html) | [industry-policy/policies-regulations](https://rsshub.app/ccagm/industry-policy/policies-regulations) | |
| 166 | + | [资料下载](http://www.ccagm.org.cn/download.html) | [download](https://rsshub.app/ccagm/download) | |
| 167 | + | [工作总结与计划](http://www.ccagm.org.cn/about-association/gong-zuo-zong-jie-yu-ji-hua.html) | [about-association/gong-zuo-zong-jie-yu-ji-hua](https://rsshub.app/ccagm/about-association/gong-zuo-zong-jie-yu-ji-hua) | |
| 168 | +
|
| 169 | +</details> |
| 170 | +`, |
| 171 | + categories: ['new-media'], |
| 172 | + features: { |
| 173 | + requireConfig: false, |
| 174 | + requirePuppeteer: false, |
| 175 | + antiCrawler: false, |
| 176 | + supportRadar: true, |
| 177 | + supportBT: false, |
| 178 | + supportPodcast: false, |
| 179 | + supportScihub: false, |
| 180 | + }, |
| 181 | + radar: [ |
| 182 | + { |
| 183 | + source: ['www.ccagm.org.cn/category?'], |
| 184 | + target: '/:category', |
| 185 | + }, |
| 186 | + { |
| 187 | + title: '协会动态', |
| 188 | + source: ['www.ccagm.org.cn/association-news.html'], |
| 189 | + target: '/association-news', |
| 190 | + }, |
| 191 | + { |
| 192 | + title: '会议活动', |
| 193 | + source: ['www.ccagm.org.cn/xh-activity/activities-huiyi.html'], |
| 194 | + target: '/xh-activity/activities-huiyi', |
| 195 | + }, |
| 196 | + { |
| 197 | + title: '调研与报告', |
| 198 | + source: ['www.ccagm.org.cn/xh-activity/bg-yj.html'], |
| 199 | + target: '/xh-activity/bg-yj', |
| 200 | + }, |
| 201 | + { |
| 202 | + title: '协会党建', |
| 203 | + source: ['www.ccagm.org.cn/xie-hui-dang-jian.html'], |
| 204 | + target: '/xie-hui-dang-jian', |
| 205 | + }, |
| 206 | + { |
| 207 | + title: '行业新闻', |
| 208 | + source: ['www.ccagm.org.cn/members-info.html'], |
| 209 | + target: '/members-info', |
| 210 | + }, |
| 211 | + { |
| 212 | + title: '行业研究', |
| 213 | + source: ['www.ccagm.org.cn/bg-yj.html'], |
| 214 | + target: '/bg-yj', |
| 215 | + }, |
| 216 | + { |
| 217 | + title: '行业标准', |
| 218 | + source: ['www.ccagm.org.cn/industry-policy/industry-standard.html'], |
| 219 | + target: '/industry-policy/industry-standard', |
| 220 | + }, |
| 221 | + { |
| 222 | + title: '法律法规', |
| 223 | + source: ['www.ccagm.org.cn/industry-policy/policies-regulations.html'], |
| 224 | + target: '/industry-policy/policies-regulations', |
| 225 | + }, |
| 226 | + { |
| 227 | + title: '资料下载', |
| 228 | + source: ['www.ccagm.org.cn/download.html'], |
| 229 | + target: '/download', |
| 230 | + }, |
| 231 | + { |
| 232 | + title: '工作总结与计划', |
| 233 | + source: ['www.ccagm.org.cn/about-association/gong-zuo-zong-jie-yu-ji-hua.html'], |
| 234 | + target: '/about-association/gong-zuo-zong-jie-yu-ji-hua', |
| 235 | + }, |
| 236 | + ], |
| 237 | + view: ViewType.Articles, |
| 238 | +}; |
0 commit comments