feat: add ability to scrape youtube video

This commit is contained in:
2023-08-02 15:56:33 +02:00
parent cebbb8af2b
commit ba853342bd
10 changed files with 193 additions and 36 deletions

86
lib/youtube.ts Normal file
View File

@@ -0,0 +1,86 @@
import { YOUTUBE_API_KEY } from "@lib/env.ts";
const BASE_URL = "https://youtube.googleapis.com/youtube/v3/";
export interface APIResponse {
kind: string;
etag: string;
items: Item[];
pageInfo: PageInfo;
}
export interface Item {
kind: string;
etag: string;
id: string;
snippet: Snippet;
contentDetails: ContentDetails;
statistics: Statistics;
}
export interface Snippet {
publishedAt: string;
channelId: string;
title: string;
description: string;
thumbnails: Thumbnails;
channelTitle: string;
tags: string[];
categoryId: string;
liveBroadcastContent: string;
localized: Localized;
}
export interface Thumbnails {
default: Resolution;
medium: Resolution;
high: Resolution;
standard: Resolution;
maxres: Resolution;
}
export interface Resolution {
url: string;
width: number;
height: number;
}
export interface Localized {
title: string;
description: string;
}
export interface ContentDetails {
duration: string;
dimension: string;
definition: string;
caption: string;
licensedContent: boolean;
contentRating: ContentRating;
projection: string;
}
export interface ContentRating {}
export interface Statistics {
viewCount: string;
likeCount: string;
favoriteCount: string;
commentCount: string;
}
export interface PageInfo {
totalResults: number;
resultsPerPage: number;
}
export async function getYoutubeVideoDetails(
id: string,
): Promise<Item> {
const response = await fetch(
`${BASE_URL}videos?part=snippet%2CcontentDetails%2Cstatistics&id=${id}&key=${YOUTUBE_API_KEY}`,
);
const json = await response.json();
return json?.items[0];
}