Skip to content

Commit

Permalink
Get duration from older videos
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Morris committed Aug 27, 2024
1 parent f7b1b02 commit b2d2a86
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/Config/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
$routes->add('aggro/vimeo', 'Aggro::getVimeo');
$routes->add('aggro/vimeo/(:segment)', 'Aggro::getVimeo/$1');
$routes->add('aggro/youtube', 'Aggro::getYoutube');
$routes->add('aggro/duration', 'Aggro::getYouTubeDuration');
$routes->add('aggro/youtube/(:segment)', 'Aggro::getYoutube/$1');
$routes->add('aggro/log-clean', 'Aggro::getLogClean');
$routes->add('aggro/log-error-clean', 'Aggro::getLogErrorClean');
Expand Down
20 changes: 20 additions & 0 deletions app/Controllers/Aggro.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,26 @@ public function getSweep()
return true;
}

/**
* Update duration value for videos.
*/
public function getYouTubeDuration()
{
helper('aggro');
helper('youtube');
$youtubeModel = new YoutubeModels();

if (! gate_check()) {
return false;
}

if ($youtubeModel->getDuration()) {
echo "\nDurations fetched.\n";
}

return true;
}

/**
* Vimeo video fetcher.
*
Expand Down
38 changes: 30 additions & 8 deletions app/Helpers/youtube_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,34 @@
* @file
* YouTube helper functions.
*/
if (! function_exists('youtube_get_feed')) {
if (! function_exists('youtube_get_duration')) {
/**
* Fetch YouTube video duration.
*
* @param string $videoID
* YouTube videoID.
*
* @return string
* Video duration.
*/
function youtube_get_duration($videoID)
{
helper('aggro');

$videoPage = 'https://www.youtube.com/watch?v=' . $videoID;
$resultPage = fetch_url($videoPage, 'text', 0);

if ($resultPage !== false && is_string($resultPage)) {
if (preg_match('/"lengthSeconds":"(\d+)"/', $resultPage, $matches)) {
return $matches[1];
}
}

return false;
}
}

if (! function_exists('youtube_get_feed')) {
/**
* Fetch YouTube channel feed.
*
Expand Down Expand Up @@ -157,13 +184,8 @@ function youtube_parse_meta(object $item)
}
$video['video_aspect_ratio'] = round($video['video_width'] / $video['video_height'], 3);

$videoPage = 'https://www.youtube.com/watch?v=' . $video['video_id'];
$resultPage = fetch_url($videoPage, 'text', 0);
if ($resultPage !== false && is_string($resultPage)) {
if (preg_match('/"lengthSeconds":"(\d+)"/', $resultPage, $matches)) {
$video['video_duration'] = $matches[1];
}
}
$video['video_duration'] = youtube_get_duration($video['video_id']);

return $video;
}
}
36 changes: 36 additions & 0 deletions app/Models/YoutubeModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,40 @@ public function parseChannel($feed)

return $addCount;
}

/**
* Get duration for YouTube videos.
*
* Write count of updated videos to log.
*
* @return bool
* Archive complete.
*
* @see sendLog()
*/
public function getDuration()
{
$utilityModel = new UtilityModels();
$now = date('Y-m-d H:i:s');
helper('youtube');

$sql = "SELECT * FROM aggro_videos WHERE video_date_uploaded <= DATE_SUB('" . $now . "',INTERVAL 31 DAY) AND flag_archive=0 AND flag_bad=0 AND video_duration=0 AND video_type='youtube' LIMIT 10";
$query = $this->db->query($sql);
$update = count($query->getResultArray());

if ($update > 0) {
$results = $query->getResult();
foreach ($results as $result) {
$videoDuration = youtube_get_duration($result->video_id);
$sql = "UPDATE aggro_videos SET video_duration = " . $videoDuration . " WHERE video_id = '" . $result->video_id . "'";
$query = $this->db->query($sql);
}
}

$message = $update . ' video durations fetched.';
$utilityModel->sendLog($message);

return true;
}

}

0 comments on commit b2d2a86

Please sign in to comment.