<?php

// Load Google APIs Client Library for PHP using Composer
require 'vendor/autoload.php';

// Your Google API key obtained from the Cloud Console
$apiKey = 'YOUR_API_KEY';

// Create a Google_Client object
$client = new Google_Client();
$client->setApplicationName('YouTube Triggers');
$client->setDeveloperKey($apiKey);

// Create a YouTube service instance
$youtube = new Google_Service_YouTube($client);

// Function to check for new video uploads
function checkForNewVideoUploads() {
    global $youtube;

    // Set the YouTube channel ID for which you want to check new video uploads
    $channelId = 'YOUR_CHANNEL_ID';

    // Call the YouTube API to get the channel's uploads playlist ID
    $channelResponse = $youtube->channels->listChannels('contentDetails', array('id' => $channelId));
    $uploadsPlaylistId = $channelResponse['items'][0]['contentDetails']['relatedPlaylists']['uploads'];

    // Call the YouTube API to get the latest video in the uploads playlist
    $playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet', array('playlistId' => $uploadsPlaylistId, 'maxResults' => 1));
    $latestVideoId = $playlistItemsResponse['items'][0]['snippet']['resourceId']['videoId'];

    // Store the latest video ID in a file or database
    // You can use a database to keep track of previously detected videos
    // For simplicity, we'll store it in a text file here
    $filePath = 'latest_video.txt';
    if (file_exists($filePath)) {
        $storedVideoId = file_get_contents($filePath);

        // Compare the latest video ID with the stored video ID
        if ($latestVideoId != $storedVideoId) {
            // New video upload detected! Handle the trigger event here.
            echo "New video upload detected! Video ID: " . $latestVideoId;

            // Update the stored video ID with the latest one
            file_put_contents($filePath, $latestVideoId);
        } else {
            echo "No new video uploads.";
        }
    } else {
        file_put_contents($filePath, $latestVideoId);
        echo "Initial setup done. No previous video to compare.";
    }
}

// Call the function to check for new video uploads
checkForNewVideoUploads();


Post a Comment

0 Comments