From f7b1b02b58522304f3364dfa40f8dd77ba10a9aa Mon Sep 17 00:00:00 2001 From: Jason Morris Date: Mon, 26 Aug 2024 20:34:09 -0400 Subject: [PATCH] Add video duration capture --- aggro-db.sql | 1 + app/Helpers/youtube_helper.php | 7 +++++++ app/Models/AggroModels.php | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/aggro-db.sql b/aggro-db.sql index 350484a..857f7e6 100644 --- a/aggro-db.sql +++ b/aggro-db.sql @@ -61,6 +61,7 @@ CREATE TABLE `aggro_videos` ( `video_width` int(6) NOT NULL DEFAULT '0', `video_height` int(6) NOT NULL DEFAULT '0', `video_aspect_ratio` float NOT NULL DEFAULT '0', + `video_duration` int(10) NOT NULL DEFAULT '0', `video_type` varchar(255) DEFAULT NULL, `video_source_id` varchar(255) DEFAULT NULL, `video_source_username` varchar(255) DEFAULT NULL, diff --git a/app/Helpers/youtube_helper.php b/app/Helpers/youtube_helper.php index 2a8b645..702a9ae 100644 --- a/app/Helpers/youtube_helper.php +++ b/app/Helpers/youtube_helper.php @@ -157,6 +157,13 @@ 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]; + } + } return $video; } } diff --git a/app/Models/AggroModels.php b/app/Models/AggroModels.php index 8d8c745..abffb9d 100644 --- a/app/Models/AggroModels.php +++ b/app/Models/AggroModels.php @@ -22,7 +22,7 @@ public function addVideo(array $video) $utilityModel = new UtilityModels(); helper('aggro'); - $sql = "INSERT INTO aggro_videos (video_id, aggro_date_added, aggro_date_updated, video_date_uploaded, flag_archive, flag_bad, video_plays, video_title, video_thumbnail_url, video_width, video_height, video_aspect_ratio, video_source_id, video_source_username, video_source_url, video_type) VALUES ('" . $video['video_id'] . "', '" . $video['aggro_date_added'] . "', '" . $video['aggro_date_updated'] . "', '" . $video['video_date_uploaded'] . "', " . $video['flag_archive'] . ', 0, ' . $video['video_plays'] . ", '" . $video['video_title'] . "', '" . $video['video_thumbnail_url'] . "', " . $video['video_width'] . ', ' . $video['video_height'] . ', ' . $video['video_aspect_ratio'] . ", '" . $video['video_source_id'] . "', '" . $video['video_source_username'] . "', '" . $video['video_source_url'] . "', '" . $video['video_type'] . "')"; + $sql = "INSERT INTO aggro_videos (video_id, aggro_date_added, aggro_date_updated, video_date_uploaded, flag_archive, flag_bad, video_plays, video_title, video_thumbnail_url, video_width, video_height, video_aspect_ratio, video_duration, video_source_id, video_source_username, video_source_url, video_type) VALUES ('" . $video['video_id'] . "', '" . $video['aggro_date_added'] . "', '" . $video['aggro_date_updated'] . "', '" . $video['video_date_uploaded'] . "', " . $video['flag_archive'] . ', 0, ' . $video['video_plays'] . ", '" . $video['video_title'] . "', '" . $video['video_thumbnail_url'] . "', " . $video['video_width'] . ', ' . $video['video_height'] . ', ' . $video['video_aspect_ratio'] . ", ". $video['video_duration'] ." , '" . $video['video_source_id'] . "', '" . $video['video_source_username'] . "', '" . $video['video_source_url'] . "', '" . $video['video_type'] . "')"; $this->db->query($sql);