Skip to content

Commit

Permalink
feat(route): fanbox (DIYgod#15418)
Browse files Browse the repository at this point in the history
* feat(route): fanbox

* fix

* Update lib/routes/fanbox/index.ts

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

* replace `got` with `ofetch`

* Update lib/routes/fanbox/utils.ts

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

---------
  • Loading branch information
KarasuShin authored May 1, 2024
1 parent 4aebe20 commit b3f0276
Show file tree
Hide file tree
Showing 9 changed files with 502 additions and 288 deletions.
3 changes: 0 additions & 3 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ router.get('/benedictevans', lazyloadRouteHandler('./routes/benedictevans/recent
// router.get('/jianshu/collection/:id', lazyloadRouteHandler('./routes/jianshu/collection'));
// router.get('/jianshu/user/:id', lazyloadRouteHandler('./routes/jianshu/user'));

// pixiv-fanbox
router.get('/fanbox/:user?', lazyloadRouteHandler('./routes/fanbox/main'));

// Disqus
router.get('/disqus/posts/:forum', lazyloadRouteHandler('./routes/disqus/posts'));

Expand Down
227 changes: 0 additions & 227 deletions lib/routes-deprecated/fanbox/conv.js

This file was deleted.

13 changes: 0 additions & 13 deletions lib/routes-deprecated/fanbox/header.js

This file was deleted.

45 changes: 0 additions & 45 deletions lib/routes-deprecated/fanbox/main.js

This file was deleted.

62 changes: 62 additions & 0 deletions lib/routes/fanbox/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import InvalidParameterError from '@/errors/types/invalid-parameter';
import type { Data, Route } from '@/types';
import { isValidHost } from '@/utils/valid-host';
import type { Context } from 'hono';
import { getHeaders, parseItem } from './utils';
import type { PostListResponse, UserInfoResponse } from './types';
import ofetch from '@/utils/ofetch';

export const route: Route = {
path: '/:creator',
categories: ['social-media'],
example: '/fanbox/official',
parameters: { creator: 'fanbox user name' },
maintainers: ['KarasuShin'],
name: 'Creator',
handler,
features: {
requireConfig: [
{
name: 'FANBOX_SESSION_ID',
description: 'Required for private posts. Can be found in browser DevTools -> Application -> Cookies -> https://www.fanbox.cc -> FANBOXSESSID',
optional: true,
},
],
},
};

async function handler(ctx: Context): Promise<Data> {
const creator = ctx.req.param('creator');
if (!isValidHost(creator)) {
throw new InvalidParameterError('Invalid user name');
}

let title = `Fanbox - ${creator}`;

let description: string | undefined;

let image: string | undefined;

try {
const userApi = `https://api.fanbox.cc/creator.get?creatorId=${creator}`;
const userInfoResponse = (await ofetch(userApi, {
headers: getHeaders(),
})) as UserInfoResponse;
title = `Fanbox - ${userInfoResponse.body.user.name}`;
description = userInfoResponse.body.description;
image = userInfoResponse.body.user.iconUrl;
} catch {
// ignore
}

const postListResponse = (await ofetch(`https://api.fanbox.cc/post.listCreator?creatorId=${creator}&limit=20`, { headers: getHeaders() })) as PostListResponse;
const items = await Promise.all(postListResponse.body.items.map((i) => parseItem(i)));

return {
title,
link: `https://${creator}.fanbox.cc`,
description,
image,
item: items,
};
}
6 changes: 6 additions & 0 deletions lib/routes/fanbox/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: 'fanbox',
url: 'https://www.fanbox.cc',
};
7 changes: 7 additions & 0 deletions lib/routes/fanbox/templates/fanbox-post.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<a href={{postUrl}}>
<h1>{{title}}</h1>
<span style="margin-right:1.6em">{{user.name}}</span>
<br />
<br />
<span>{{excerpt}}</span>
</a>
Loading

0 comments on commit b3f0276

Please sign in to comment.