Skip to content

Commit

Permalink
fix(route/bjnews): Update to new standard (#16249)
Browse files Browse the repository at this point in the history
* fix(route/bjnews): Update to new standard

* .

* .

* Add column

* Update lib/routes/bjnews/cat.ts

Co-authored-by: Tony <[email protected]>

* Update column.ts

---------
  • Loading branch information
dzx-dzx authored Jul 27, 2024
1 parent 1754ffa commit 4c3ab5c
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 47 deletions.
3 changes: 0 additions & 3 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ router.get('/konachan.net/post/popular_recent/:period', lazyloadRouteHandler('./
// EZTV
router.get('/eztv/torrents/:imdb_id', lazyloadRouteHandler('./routes/eztv/imdb'));

// 新京报
router.get('/bjnews/:cat', lazyloadRouteHandler('./routes/bjnews/news'));

// 米哈游
router.get('/mihoyo/bh3/:type', lazyloadRouteHandler('./routes/mihoyo/bh3'));
router.get('/mihoyo/bh2/:type', lazyloadRouteHandler('./routes/mihoyo/bh2'));
Expand Down
44 changes: 0 additions & 44 deletions lib/routes-deprecated/bjnews/news.js

This file was deleted.

43 changes: 43 additions & 0 deletions lib/routes/bjnews/cat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Route } from '@/types';
import { load } from 'cheerio';
import ofetch from '@/utils/ofetch';

import { fetchArticle } from './utils';

export const route: Route = {
path: '/cat/:cat',
categories: ['traditional-media'],
example: '/bjnews/cat/depth',
parameters: { cat: '分类, 可从URL中找到' },
features: {},
radar: [
{
source: ['www.bjnews.com.cn/:cat'],
},
],
name: '分类',
maintainers: ['dzx-dzx'],
handler,
url: 'www.bjnews.com.cn',
};

async function handler(ctx) {
const url = `https://www.bjnews.com.cn/${ctx.req.param('cat')}`;
const res = await ofetch(url);
const $ = load(res);
const list = $('#waterfall-container .pin_demo > a')
.toArray()
.slice(0, ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit')) : 15)
.map((a) => ({
title: $(a).text(),
link: $(a).attr('href'),
category: $(a).parent().find('.source').text().trim(),
}));

const out = await Promise.all(list.map((item) => fetchArticle(item)));
return {
title: $('title').text(),
link: url,
item: out,
};
}
43 changes: 43 additions & 0 deletions lib/routes/bjnews/column.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Route } from '@/types';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';

import { fetchArticle } from './utils';

export const route: Route = {
path: '/column/:column',
categories: ['traditional-media'],
example: '/bjnews/column/204',
parameters: { column: '栏目ID, 可从手机版网页URL中找到' },
features: {},
radar: [
{
source: ['m.bjnews.com.cn/column/:column.htm'],
},
],
name: '分类',
maintainers: ['dzx-dzx'],
handler,
url: 'www.bjnews.com.cn',
};

async function handler(ctx) {
const columnID = ctx.req.param('column');
const url = `https://api.bjnews.com.cn/api/v101/news/column_news.php?column_id=${columnID}`;
const res = await ofetch(url);
const list = res.data.slice(0, ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit')) : 15).map((e) => ({
title: e.row.title,
guid: e.uuid,
pubDate: timezone(parseDate(e.row.publish_time), +8),
updated: timezone(parseDate(e.row.update_time), +8),
link: `https://www.bjnews.com.cn/detail/${e.uuid}.html`,
}));

const out = await Promise.all(list.map((item) => fetchArticle(item)));
return {
title: `新京报 - 栏目 - ${res.data[0].row.column_info[0].column_name}`,
link: `https://m.bjnews.com.cn/column/${columnID}.html`,
item: out,
};
}
6 changes: 6 additions & 0 deletions lib/routes/bjnews/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: '新京报',
url: 'www.bjnews.com.cn',
};
19 changes: 19 additions & 0 deletions lib/routes/bjnews/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import cache from '@/utils/cache';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';

export function fetchArticle(item) {
return cache.tryGet(item.link, async () => {
const responses = await ofetch(item.link);
const $d = load(responses);
// $d('img').each((i, e) => $(e).attr('referrerpolicy', 'no-referrer'));

item.pubDate = timezone(parseDate($d('.left-info .timer').text()), +8);
item.author = $d('.left-info .reporter').text();
item.description = $d('#contentStr').html();

return item;
});
}

0 comments on commit 4c3ab5c

Please sign in to comment.