feat(route): nicovideo (#16864)

This commit is contained in:
Tony
2024-09-23 00:57:06 +08:00
committed by GitHub
parent b379e7d1aa
commit da72591b7a
5 changed files with 152 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
import type { Namespace } from '@/types';
export const namespace: Namespace = {
name: 'Niconico',
url: 'www.nicovideo.jp',
categories: ['multimedia'],
};
+7
View File
@@ -0,0 +1,7 @@
{{ if embed }}
<iframe src="https://embed.nicovideo.jp/watch/{{ video.id }}" style="top: 0; left: 0; width: 100%; height: 100%; position: absolute; border: 0;" allowfullscreen></iframe>
{{ else }}
<img src="{{ video.thumbnail.nHdUrl || video.thumbnail.largeUrl || video.thumbnail.middleUrl }}">
{{ /if }}
<br>
{{@ video.shortDescription }}
+54
View File
@@ -0,0 +1,54 @@
export interface UserInfo {
id: number;
nickname: string;
icon: string;
smallIcon: string;
}
interface Count {
view: number;
comment: number;
mylist: number;
like: number;
}
interface Thumbnail {
url: string;
middleUrl: string | null;
largeUrl: string | null;
listingUrl: string;
nHdUrl: string;
}
interface Owner {
ownerType: string;
type: string;
visibility: string;
id: string;
name: string;
iconUrl: string;
}
export interface Essential {
type: string;
id: string;
title: string;
registeredAt: string;
count: Count;
thumbnail: Thumbnail;
duration: number;
shortDescription: string;
latestCommentSummary: string;
isChannelVideo: boolean;
isPaymentRequired: boolean;
playbackPosition: null;
owner: Owner;
requireSensitiveMasking: boolean;
videoLive: null;
isMuted: boolean;
}
export interface VideoItem {
series: null;
essential: Essential;
}
+37
View File
@@ -0,0 +1,37 @@
import { Essential, UserInfo, VideoItem } from './types';
import ofetch from '@/utils/ofetch';
import cache from '@/utils/cache';
import { config } from '@/config';
import path from 'node:path';
import { art } from '@/utils/render';
import { getCurrentPath } from '@/utils/helpers';
const __dirname = getCurrentPath(import.meta.url);
export const getUserInfoById = (id: string) => cache.tryGet(`nicovideo:user:${id}`, () => ofetch<UserInfo>(`https://embed.nicovideo.jp/users/${id}`)) as Promise<UserInfo>;
export const getUserVideosById = (id: string) =>
cache.tryGet(
`nicovideo:user:${id}:videos`,
async () => {
const { data } = await ofetch(`https://nvapi.nicovideo.jp/v3/users/${id}/videos`, {
headers: {
'X-Frontend-Id': '6',
},
query: {
sortKey: 'registeredAt',
sortOrder: 'desc',
sensitiveContents: 'mask',
pageSize: 100,
page: 1,
},
});
return data.items;
},
config.cache.routeExpire,
false
) as Promise<VideoItem[]>;
export const renderVideo = (video: Essential, embed: boolean) => art(path.join(__dirname, 'templates', 'video.art'), { video, embed });
+47
View File
@@ -0,0 +1,47 @@
import type { Context } from 'hono';
import { DataItem, Route } from '@/types';
import { UserInfo, VideoItem } from './types';
import { parseDate } from '@/utils/parse-date';
import { getUserInfoById, getUserVideosById, renderVideo } from './utils';
const handler = async (ctx: Context) => {
const { id } = ctx.req.param();
const embed = !ctx.req.param('embed');
const userInfo: UserInfo = await getUserInfoById(id);
const videos: VideoItem[] = await getUserVideosById(id);
const items = videos.map(({ essential: video }) => ({
title: video.title,
link: `https://www.nicovideo.jp/watch/${video.id}`,
pubDate: parseDate(video.registeredAt),
author: [{ name: video.owner.name, avatar: video.owner.iconUrl, url: `https://www.nicovideo.jp/user/${video.owner.id}` }],
description: renderVideo(video, embed),
image: video.thumbnail.nHdUrl || video.thumbnail.largeUrl || video.thumbnail.middleUrl,
upvotes: video.count.like,
comments: video.count.comment,
})) as DataItem[];
return {
title: `${userInfo.nickname} - ニコニコ`,
link: `https://www.nicovideo.jp/user/${id}/video`,
image: userInfo.icon,
item: items,
};
};
export const route: Route = {
name: 'User Videos',
path: '/user/:id/video/:embed?',
parameters: { id: 'User ID', embed: 'Default to embed the video, set to any value to disable embedding' },
example: '/nicovideo/user/16690815/video',
maintainers: ['TonyRL'],
radar: [
{
source: ['www.nicovideo.jp/user/:id', 'www.nicovideo.jp/user/:id/video'],
target: '/user/:id/video',
},
],
handler,
};