Skip to content

Commit

Permalink
Merge branch 'DIYgod:master' into feature/qbitai
Browse files Browse the repository at this point in the history
  • Loading branch information
Geraldxm authored Dec 11, 2024
2 parents 897a2b7 + e764739 commit 1d45d9c
Show file tree
Hide file tree
Showing 13 changed files with 595 additions and 374 deletions.
9 changes: 9 additions & 0 deletions lib/routes/ali213/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: '游侠网',
url: 'ali213.net',
categories: ['game'],
description: '',
lang: 'zh-CN',
};
269 changes: 269 additions & 0 deletions lib/routes/ali213/news.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
import path from 'node:path';

import { type CheerioAPI, type Cheerio, type Element, load } from 'cheerio';
import { type Context } from 'hono';

import { type DataItem, type Route, type Data, ViewType } from '@/types';

import { art } from '@/utils/render';
import cache from '@/utils/cache';
import { getCurrentPath } from '@/utils/helpers';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';

const __dirname = getCurrentPath(import.meta.url);

export const handler = async (ctx: Context): Promise<Data> => {
const { category = 'new' } = ctx.req.param();
const limit: number = Number.parseInt(ctx.req.query('limit') ?? '30', 10);

const rootUrl: string = 'https://www.ali213.net';
const targetUrl: string = new URL(`news/${category.endsWith('/') ? category : `${category}/`}`, rootUrl).href;

const response = await ofetch(targetUrl);
const $: CheerioAPI = load(response);
const language: string = $('html').prop('lang') ?? 'zh-CN';

let items: DataItem[] = $('div.n_lone')
.slice(0, limit)
.toArray()
.map((item): DataItem => {
const $item: Cheerio<Element> = $(item);

const aEl: Cheerio<Element> = $item.find('h2.lone_t a');

const title: string = aEl.prop('title') || aEl.text();
const link: string | undefined = aEl.prop('href');

const imageEl: Cheerio<Element> = $item.find('img');
const imageSrc: string | undefined = imageEl?.prop('src');
const imageAlt: string | undefined = imageEl?.prop('alt');

const intro: string = $item.find('div.lone_f_r_t').text();

const description: string = art(path.join(__dirname, 'templates/description.art'), {
images: imageEl
? [
{
src: imageSrc,
alt: imageAlt,
},
]
: undefined,
intro,
});

const author: DataItem['author'] = $item.find('div.lone_f_r_f span').last().text().split(//).pop();

return {
title,
description,
pubDate: parseDate($item.find('div.lone_f_r_f span').first().text()),
link,
author,
content: {
html: description,
text: $item.find('div.lone_f_r_t').text(),
},
image: imageSrc,
banner: imageSrc,
language,
};
});

items = (
await Promise.all(
items.map((item) => {
if (!item.link && typeof item.link !== 'string') {
return item;
}

return cache.tryGet(item.link, async (): Promise<DataItem> => {
try {
const detailResponse = await ofetch(item.link);
const $$: CheerioAPI = load(detailResponse);

const title: string = $$('h1.newstit').text();
const image: string | undefined = $$('div#Content img').first().prop('src');

const mediaContent: Cheerio<Element> = $$('div#Content p span img');
const media: Record<string, Record<string, string>> = {};

if (mediaContent.length) {
mediaContent.each((_, el) => {
const $$el: Cheerio<Element> = $$(el);

const pEl: Cheerio<Element> = $$el.closest('p');

const mediaUrl: string | undefined = $$el.prop('src');
const mediaType: string | undefined = mediaUrl?.split(/\./).pop();

if (mediaType && mediaUrl) {
media[mediaType] = { url: mediaUrl };

pEl.replaceWith(
art(path.join(__dirname, 'templates/description.art'), {
images: [
{
src: mediaUrl,
},
],
})
);
}
});
}

const description: string = art(path.join(__dirname, 'templates/description.art'), {
description: $$('div#Content').html() ?? '',
});

const extraLinks = $$('div.extend_read ul li a')
.toArray()
.map((el) => {
const $$el: Cheerio<Element> = $$(el);

return {
url: $$el.prop('href'),
type: 'related',
content_html: $$el.prop('title') || $$el.text(),
};
})
.filter((_): _ is { url: string; type: string; content_html: string } => true);

return {
title,
description,
pubDate: timezone(parseDate($$('div.newstag_l').text().split(/\s/)[0]), +8),
author: item.author,
content: {
html: description,
text: $$('div#Content').html() ?? '',
},
image,
banner: image,
language,
media: Object.keys(media).length > 0 ? media : undefined,
_extra: {
links: extraLinks.length > 0 ? extraLinks : undefined,
},
};
} catch {
return item;
}
});
})
)
).filter((_): _ is DataItem => true);

const author = '游侠网';
const title = $('div.news-list-title').text();
const feedImage = new URL('news/images/ali213_app_big.png', rootUrl).href;

return {
title: `${author} - ${title}`,
description: title,
link: targetUrl,
item: items,
allowEmpty: true,
image: feedImage,
author,
language,
id: targetUrl,
};
};

export const route: Route = {
path: '/news/:category?',
name: '资讯',
url: 'www.ali213.net',
maintainers: ['nczitzk'],
handler,
example: '/ali213/news/new',
parameters: {
category: '分类,默认为 `new`,即最新资讯,可在对应分类页 URL 中找到',
},
description: `:::tip
若订阅 [游戏资讯](https://www.ali213.net/news/game/),网址为 \`https://www.ali213.net/news/game/\`,请截取 \`https://www.ali213.net/news/\` 到末尾 \`/\` 的部分 \`game\` 作为 \`category\` 参数填入,此时目标路由为 [\`/ali213/news/game\`](https://rsshub.app/ali213/news/game)。
:::
| 分类名称 | 分类 ID |
| -------- | ------- |
| 最新资讯 | new |
| 评测 | pingce |
| 游戏 | game |
| 动漫 | comic |
| 影视 | movie |
| 科技 | tech |
| 电竞 | esports |
| 娱乐 | amuse |
| 手游 | mobile |
`,
categories: ['game'],
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportRadar: true,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['www.ali213.net/news/:category'],
target: (params) => {
const category = params.category;

return `/news/${category ? `/${category}` : ''}`;
},
},
{
title: '最新资讯',
source: ['www.ali213.net/news/new'],
target: '/news/new',
},
{
title: '评测',
source: ['www.ali213.net/news/pingce'],
target: '/news/pingce',
},
{
title: '游戏',
source: ['www.ali213.net/news/game'],
target: '/news/game',
},
{
title: '动漫',
source: ['www.ali213.net/news/comic'],
target: '/news/comic',
},
{
title: '影视',
source: ['www.ali213.net/news/movie'],
target: '/news/movie',
},
{
title: '科技',
source: ['www.ali213.net/news/tech'],
target: '/news/tech',
},
{
title: '电竞',
source: ['www.ali213.net/news/esports'],
target: '/news/esports',
},
{
title: '娱乐',
source: ['www.ali213.net/news/amuse'],
target: '/news/amuse',
},
{
title: '手游',
source: ['www.ali213.net/news/mobile'],
target: '/news/mobile',
},
],
view: ViewType.Articles,
};
21 changes: 21 additions & 0 deletions lib/routes/ali213/templates/description.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{ if images }}
{{ each images image }}
{{ if image?.src }}
<figure>
<img
{{ if image.alt }}
alt="{{ image.alt }}"
{{ /if }}
src="{{ image.src }}">
</figure>
{{ /if }}
{{ /each }}
{{ /if }}

{{ if intro }}
<blockquote>{{ intro }}</blockquote>
{{ /if }}

{{ if description }}
{{@ description }}
{{ /if }}
7 changes: 3 additions & 4 deletions lib/routes/duckdb/news.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ async function handler() {
item = $(item);
return {
title: item.find('h3').text().trim(),
link: `https://duckdb.org${item.find('a').eq(2).attr('href')}`,
link: `https://duckdb.org${item.find('a.blocklink').attr('href')}`,
pubDate: timezone(parseDate(item.find('.date').text(), 'YYYY-MM-DD'), 0),
author: item.find('.author').text().trim(),
};
});

Expand All @@ -46,9 +47,7 @@ async function handler() {
cache.tryGet(item.link, async () => {
const response = await got(item.link);
const $ = load(response.body);
item.author = $('.author').text();
item.description = $('.singleentry').html();

item.description = $('.contentwidth').find('h1, .infoline').remove().end().html();
// 上面每个列表项的每个属性都在此重用,
// 并增加了一个新属性“description”
return item;
Expand Down
47 changes: 37 additions & 10 deletions lib/routes/github/repos.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Route } from '@/types';
import got from '@/utils/got';
import { config } from '@/config';
import queryString from 'query-string';

export const route: Route = {
path: '/repos/:user',
path: '/repos/:user/:type?/:sort?',
categories: ['programming'],
example: '/github/repos/DIYgod',
parameters: { user: 'GitHub username' },
parameters: {
user: 'GitHub username',
type: 'Type of repository, can be `all`, `owner`, `member`, `public`, `private`, `forks`, `sources`',
sort: 'Sort by `created`, `updated`, `pushed`, `full_name`',
},
features: {
requireConfig: false,
requirePuppeteer: false,
Expand All @@ -28,20 +31,44 @@ export const route: Route = {

async function handler(ctx) {
const user = ctx.req.param('user');

const headers = {};
const type = ctx.req.param('type') || 'all';
const sort = ctx.req.param('sort') || 'created';
let headers = {};
if (config.github && config.github.access_token) {
headers.Authorization = `token ${config.github.access_token}`;
headers = {
...headers,
Authorization: `token ${config.github.access_token}`,
};
}
const response = await got({
method: 'get',
url: `https://api.github.com/users/${user}/repos`,
searchParams: queryString.stringify({
sort: 'created',
}),
searchParams: {
type,
sort,
},
headers,
});
const data = response.data;
const data = response.data.filter((item) => {
switch (type) {
case 'all':
return true;
case 'owner':
return item.owner.login === user;
case 'member':
return item.owner.login !== user;
case 'public':
return item.private === false;
case 'private':
return item.private === true;
case 'forks':
return item.fork === true;
case 'sources':
return item.fork === false;
default:
return true;
}
});
return {
allowEmpty: true,
title: `${user}'s GitHub repositories`,
Expand Down
Loading

0 comments on commit 1d45d9c

Please sign in to comment.