Fork
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import process from 'node:process';
import { YouTube } from 'https://deno.land/x/youtube@v0.3.0/mod.ts';
export async function fetchVideoDetails(req) {
const url = new URL(req.url);
let yturl = url.searchParams.get("yturl") || "";
let mode = url.searchParams.get("mode") || "";
let responseContent;
let publishedDate, currentDate, timeDiff, daysDiff; // Declare variables outside the switch
yturl = decodeURIComponent(yturl);
const videoIdMatch = yturl.match(/(?:https?:\/\/)?(?:www\.)?youtube\.com\/(?:watch\?v=|live\/)([a-zA-Z0-9_-]+)|youtu\.be\/([a-zA-Z0-9_-]+)/);
let videoId = videoIdMatch ? videoIdMatch[1] || videoIdMatch[2] : null;
if (videoId && videoId.includes('&')) {
videoId = videoId.split('&')[0];
}
if (!videoId) {
videoId = `0iaU9VZXKUQ`;
}
if (!mode) {
mode = `transcript`;
}
const apiKey = process.env.ytapiKey;
let apiUrl = `https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,statistics&id=${videoId}&key=${apiKey}`;
try {
let response = await fetch(apiUrl);
let data = await response.json();
if (!data.items || data.items.length === 0) {
throw new Error('No video details found');
}
const item = data.items[0];
let responseContent;
switch (mode) {
case 'thumbnail':
responseContent = `![](${item.snippet.thumbnails.high.url})`;
break;
case 'channel':
const channelLink = `https://www.youtube.com/channel/${item.snippet.channelId}`;
responseContent = `[${item.snippet.channelTitle}](${channelLink})`;
break;
case 'publishDate':
responseContent = item.snippet.publishedAt;
break;
case 'viewCount':
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.