Skip to content

Commit

Permalink
feat(route): 新增MC百科路由 (#16279)
Browse files Browse the repository at this point in the history
* feat(route): 新增MC百科路由

* fix
  • Loading branch information
hualiong authored Jul 30, 2024
1 parent 774839b commit 557f4ed
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
105 changes: 105 additions & 0 deletions lib/routes/mcmod/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { DataItem, Route } from '@/types';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import ofetch from '@/utils/ofetch';
import path from 'node:path';
import cache from '@/utils/cache';
import timezone from '@/utils/timezone';
import { art } from '@/utils/render';
import { getCurrentPath } from '@/utils/helpers';

const render = (mod) => art(path.join(getCurrentPath(import.meta.url), 'templates', 'mod.art'), { mod });

export const route: Route = {
path: '/:type',
categories: ['game'],
example: '/mcmod/new',
parameters: { type: '查询类型,详见下表' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
name: '最新MOD',
maintainers: ['hualiong'],
description: `\`:type\` 类型可选如下
| 随机显示MOD | 最新收录MOD | 最近编辑MOD |
| ------ | --- | ---- |
| random | new | edit |`,
handler: async (ctx) => {
const type = ctx.req.param('type');
const $get = ofetch.create({ baseURL: 'https://www.mcmod.cn' });
const response = await $get('/');

const $ = load(response);
const typeName = $(`div.left > ul > li[i='${type}']`).attr('title');
const list = $(`#indexNew_${type} > .block`)
.toArray()
.map((item): DataItem => {
const each = $(item);
const time = each.find('div .time');
return {
title: each.find('div > .name > a').text(),
image: each.find('img').attr('src')?.split('@')[0],
link: each.children('a').attr('href'),
pubDate: time.attr('title') && timezone(parseDate(time.attr('title')!.substring(6), 'YYYY-MM-DD HH:mm:ss'), +8),
};
});

const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link!, async () => {
const response = await $get(item.link!);
const $ = load(response);

item.author = $('.author li')
.toArray()
.map((item) => {
const each = $(item);
const name = each.find('.name a');
return {
name: name.text(),
url: 'https://www.mcmod.cn' + name.attr('href'),
avatar: each.find('.avatar img').attr('src')?.split('?')[0],
};
});

const html = $('.common-text[data-id="1"]').html()!;
const support = $('.mcver > ul > ul')
.toArray()
.map((e) => {
const ul = $(e);
const label = ul.children('li:first-child').text();
const versions = ul
.children('li:not(:first-child)')
.toArray()
.map((e) => $(e).text())
.join(',');
return { label, versions };
});

item.description =
render({
pic: 'https:' + item.image,
label: $('.class-info li.col-lg-4')
.toArray()
.map((e) => $(e).text()),
support,
}) + html.replaceAll(/\ssrc=".+?"/g, '').replaceAll('data-src', 'src');
return item;
})
)
);

return {
title: `${typeName} - MC百科`,
description: $('meta[name="description"]').attr('content'),
link: 'https://www.mcmod.cn',
item: items as DataItem[],
};
},
};
6 changes: 6 additions & 0 deletions lib/routes/mcmod/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: 'MC百科',
url: 'www.mcmod.cn',
};
13 changes: 13 additions & 0 deletions lib/routes/mcmod/templates/mod.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<img src="{{ mod.pic }}" referrerpolicy="no-referrer">
{{ each mod.label l }}
<p>{{ l }}</p>
{{ /each }}
{{ if mod.support.length > 0 }}
<p>支持的MC版本: </p>
<ul>
{{ each mod.support s }}
<li><b>{{ s.label }}</b>{{ s.versions }}</li>
{{ /each }}
</ul>
{{ /if }}
<hr style="height: 2px; background-color: #e7e7e7; border: 0 none;">

0 comments on commit 557f4ed

Please sign in to comment.