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

4
lib/cache/image.ts vendored
View File

@ -69,8 +69,8 @@ export async function setImage(
const pointerId = await hash(cacheKey);
await cache.set(pointerId, clone);
cache.expire(pointerId, 60 * 10);
cache.expire(cacheKey, 60 * 10);
cache.expire(pointerId, 60 * 60 * 24);
cache.expire(cacheKey, 60 * 60 * 24);
await cache.set(
cacheKey,

View File

@ -3,3 +3,4 @@ export const REDIS_HOST = Deno.env.get("REDIS_HOST");
export const REDIS_PASS = Deno.env.get("REDIS_PASS");
export const TMDB_API_KEY = Deno.env.get("TMDB_API_KEY");
export const OPENAI_API_KEY = Deno.env.get("OPENAI_API_KEY");
export const YOUTUBE_API_KEY = Deno.env.get("YOUTUBE_API_KEY");

View File

@ -55,8 +55,12 @@ export async function extractAuthorName(content: string) {
const author = chatCompletion.choices[0].message.content;
if (author !== "not found") return author;
return "";
if (
author?.toLowerCase().includes("not") &&
author?.toLowerCase().includes("found")
) return "";
return author;
}
export async function createTags(content: string) {

View File

@ -30,3 +30,26 @@ export function extractHashTags(inputString: string) {
return hashtags;
}
export const isYoutubeLink = (link: string) => {
try {
const url = new URL(link);
return ["youtu.be", "youtube.com","www.youtube.com" ].includes(url.hostname);
} catch (err) {
console.log(err);
return false;
}
};
export function extractYoutubeId(link: string) {
const url = new URL(link);
if (url.searchParams.has("v")) {
const id = url.searchParams.get("v");
if (id?.length && id.length > 4) {
return id;
}
}
return url.pathname.replace(/^\//, "");
}

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];
}