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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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':
responseContent = item.statistics.viewCount.toString();
break;
case 'viewsPerDay':
publishedDate = new Date(item.snippet.publishedAt);
currentDate = new Date();
timeDiff = currentDate.getTime() - publishedDate.getTime();
daysDiff = Math.floor(timeDiff / (1000 * 3600 * 24));
const viewsPerDay = daysDiff > 0 ? (item.statistics.viewCount / daysDiff).toFixed(2) : 'N/A';
responseContent = viewsPerDay.toString();
break;
case 'videoAge':
publishedDate = new Date(item.snippet.publishedAt);
currentDate = new Date();
timeDiff = currentDate.getTime() - publishedDate.getTime();
daysDiff = Math.floor(timeDiff / (1000 * 3600 * 24));
responseContent = daysDiff.toString();
break;
case 'videoTitle':
responseContent = item.snippet.title;
break;
case 'engagementMetrics':
const likeCount = item.statistics.likeCount || 'N/A';
const dislikeCount = item.statistics.dislikeCount || 'N/A';
const commentCount = item.statistics.commentCount || 'N/A';
responseContent = `Likes: ${likeCount}, Dislikes: ${dislikeCount}, Comments: ${commentCount}`;
break;
case 'averageViewDuration':
const duration = item.contentDetails.duration;
const durationInSeconds = parseISO8601Duration(duration);
const averageViewDuration = (durationInSeconds / item.statistics.viewCount).toFixed(2);
responseContent = `Average View Duration: ${averageViewDuration} seconds`;
break;
case 'videoCategory':
const categoryId = item.snippet.categoryId;
apiUrl = `https://www.googleapis.com/youtube/v3/videoCategories?part=snippet&id=${categoryId}&key=${apiKey}`;
response = await fetch(apiUrl);
data = await response.json();
const category = data.items && data.items.length > 0 ? data.items[0].snippet.title : 'N/A';
responseContent = `Category: ${category}`;
break;
case 'tags':
const tags = item.snippet.tags || [];
responseContent = `Tags: ${tags.join(', ')}`;
break;
case 'transcript':
async function fetchcaptions(videoId) {
const WATCH_URL = `https://www.youtube.com/watch?v=${videoId}`;
try {
const response = await fetch(WATCH_URL);
👆 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.