Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(route): nicovideo #16864

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/routes/nicovideo/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: 'Niconico',
url: 'www.nicovideo.jp',
categories: ['multimedia'],
};
7 changes: 7 additions & 0 deletions lib/routes/nicovideo/templates/video.art
Original file line number Diff line number Diff line change
@@ -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 changes: 54 additions & 0 deletions lib/routes/nicovideo/types.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 37 additions & 0 deletions lib/routes/nicovideo/utils.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 47 additions & 0 deletions lib/routes/nicovideo/video.ts
Original file line number Diff line number Diff line change
@@ -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,
};
Loading