From da72591b7af665c045c3f87fd8c06c0d7928fdeb Mon Sep 17 00:00:00 2001 From: Tony Date: Mon, 23 Sep 2024 00:57:06 +0800 Subject: [PATCH] feat(route): nicovideo (#16864) --- lib/routes/nicovideo/namespace.ts | 7 +++ lib/routes/nicovideo/templates/video.art | 7 +++ lib/routes/nicovideo/types.ts | 54 ++++++++++++++++++++++++ lib/routes/nicovideo/utils.ts | 37 ++++++++++++++++ lib/routes/nicovideo/video.ts | 47 +++++++++++++++++++++ 5 files changed, 152 insertions(+) create mode 100644 lib/routes/nicovideo/namespace.ts create mode 100644 lib/routes/nicovideo/templates/video.art create mode 100644 lib/routes/nicovideo/types.ts create mode 100644 lib/routes/nicovideo/utils.ts create mode 100644 lib/routes/nicovideo/video.ts diff --git a/lib/routes/nicovideo/namespace.ts b/lib/routes/nicovideo/namespace.ts new file mode 100644 index 000000000..98326adcf --- /dev/null +++ b/lib/routes/nicovideo/namespace.ts @@ -0,0 +1,7 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'Niconico', + url: 'www.nicovideo.jp', + categories: ['multimedia'], +}; diff --git a/lib/routes/nicovideo/templates/video.art b/lib/routes/nicovideo/templates/video.art new file mode 100644 index 000000000..457b2e399 --- /dev/null +++ b/lib/routes/nicovideo/templates/video.art @@ -0,0 +1,7 @@ +{{ if embed }} + +{{ else }} + +{{ /if }} +
+{{@ video.shortDescription }} diff --git a/lib/routes/nicovideo/types.ts b/lib/routes/nicovideo/types.ts new file mode 100644 index 000000000..f8690c625 --- /dev/null +++ b/lib/routes/nicovideo/types.ts @@ -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; +} diff --git a/lib/routes/nicovideo/utils.ts b/lib/routes/nicovideo/utils.ts new file mode 100644 index 000000000..10b92a697 --- /dev/null +++ b/lib/routes/nicovideo/utils.ts @@ -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(`https://embed.nicovideo.jp/users/${id}`)) as Promise; + +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; + +export const renderVideo = (video: Essential, embed: boolean) => art(path.join(__dirname, 'templates', 'video.art'), { video, embed }); diff --git a/lib/routes/nicovideo/video.ts b/lib/routes/nicovideo/video.ts new file mode 100644 index 000000000..d09959386 --- /dev/null +++ b/lib/routes/nicovideo/video.ts @@ -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, +};