xxxxxxxxxx
119
var videoData; // To store the API response
var apiKey = 'AIzaSyDb5lnof_d87ZJgh9XauyNIM82nBHpnau4'; // Replace with your valid API key
var searchApi = 'https://www.googleapis.com/youtube/v3/search?';
var videoApi = 'https://www.googleapis.com/youtube/v3/videos?';
var input; // For duration input
var targetSeconds; // Target duration in seconds
function setup() {
createCanvas(400, 300);
// Input for duration (in format MM:SS)
input = createInput();
input.position(10, 10);
input.attribute('placeholder', 'Enter duration (MM:SS)');
// Create a button to fetch video
var button = createButton('Search Funny Cats');
button.position(input.x + input.width + 10, 10);
button.mousePressed(fetchVideos);
// Instructions on canvas
textSize(12);
fill(255);
text("Search for 'Funny Cats' videos by length:", 10, 50);
}
// Convert duration MM:SS to seconds
function durationToSeconds(duration) {
let parts = duration.split(':').map(Number);
return parts.length === 2 ? parts[0] * 60 + parts[1] : 0;
}
// Fetch videos from the YouTube API
function fetchVideos() {
// Parse the desired duration from the input
let durationInput = input.value();
targetSeconds = durationToSeconds(durationInput);
if (isNaN(targetSeconds) || targetSeconds <= 0) {
console.error('Invalid duration format. Use MM:SS.');
return;
}
// Categorize the duration for API filtering
let videoDuration;
if (targetSeconds < 240) videoDuration = 'short';
else if (targetSeconds < 1200) videoDuration = 'medium';
else videoDuration = 'long';
// Construct the search API URL
let url = `${searchApi}key=${apiKey}&q=funny cats&type=video&part=snippet&maxResults=100&videoDuration=${videoDuration}`;
// Fetch data from the YouTube search API
loadJSON(url, processSearchResults);
}
// Process the search results to find a video with exact duration
function processSearchResults(data) {
if (!data.items || data.items.length === 0) {
console.log('No videos found.');
return;
}
// Retrieve detailed video information for exact duration matching
let videoIds = data.items.map((item) => item.id.videoId).join(',');
let url = `${videoApi}key=${apiKey}&id=${videoIds}&part=contentDetails,snippet`;
loadJSON(url, filterByDuration);
}
// Filter videos by exact duration and display the first match
function filterByDuration(data) {
if (!data.items || data.items.length === 0) {
console.log('No matching videos found.');
return;
}
for (let video of data.items) {
// Parse the ISO 8601 duration
let isoDuration = video.contentDetails.duration;
let videoDurationSeconds = isoDurationToSeconds(isoDuration);
if (videoDurationSeconds === targetSeconds) {
// Display video details
displayVideo(video.snippet.title, video.snippet.description, video.id);
return;
}
}
console.log('No videos matched the exact duration.');
}
// Convert ISO 8601 duration to seconds
function isoDurationToSeconds(iso) {
let pattern = /PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?/;
let match = pattern.exec(iso);
let hours = parseInt(match[1] || 0);
let minutes = parseInt(match[2] || 0);
let seconds = parseInt(match[3] || 0);
return hours * 3600 + minutes * 60 + seconds;
}
// Display the video details and embed it
function displayVideo(title, description, videoId) {
background(0);
textSize(16);
fill(255);
text('Title: ' + title, 10, 80);
textSize(12);
text('Description: ' + description, 10, 100, width - 20);
// Embed the video
let iframe = createElement('iframe');
iframe.attribute('src', `https://www.youtube.com/embed/${videoId}`);
iframe.attribute('width', '400');
iframe.attribute('height', '200');
iframe.position(10, 150);
noLoop(); // Prevent repeated iframe creation
}