36947-vm/youtube.php
Flatlogic Bot 4be8924eba Educ8
2025-12-14 21:05:45 +00:00

41 lines
1.2 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
$channel_id = 'UCv4OxMyz0n-KQfATK3Tbexg';
$rss_url = 'https://www.youtube.com/feeds/videos.xml?channel_id=' . $channel_id;
$xml_content = @file_get_contents($rss_url);
if ($xml_content === false) {
die('Failed to fetch YouTube RSS feed. The channel ID might be incorrect or the channel may not have any videos.');
}
$xml = new SimpleXMLElement($xml_content);
if (!isset($xml->entry)) {
die('No video entries found in the RSS feed.');
}
$pdo = db();
$stmt = $pdo->prepare("INSERT IGNORE INTO youtube_videos (youtube_id, title, thumbnail_url, published_at) VALUES (:youtube_id, :title, :thumbnail_url, :published_at)");
foreach ($xml->entry as $entry) {
$media = $entry->children('media', true);
$yt = $entry->children('yt', true);
$video_id = (string)$yt->videoId;
$title = (string)$media->group->title;
$thumbnail_url = (string)$media->group->thumbnail->attributes()->url;
$published_at = new DateTime((string)$entry->published);
$stmt->execute([
':youtube_id' => $video_id,
':title' => $title,
':thumbnail_url' => $thumbnail_url,
':published_at' => $published_at->format('Y-m-d H:i:s')
]);
}
echo "YouTube videos have been successfully imported.";