Skip to content

Commit

Permalink
Implemented API Caching
Browse files Browse the repository at this point in the history
Fix #3
  • Loading branch information
athuler committed Oct 27, 2023
1 parent bd2b6c3 commit bd1c11b
Showing 1 changed file with 63 additions and 25 deletions.
88 changes: 63 additions & 25 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,73 @@


###### Fetch Video Views ######
// Process videos in batches of 50
$all_ids = array_chunk(
array_keys($GLOBALS["data"]),
50);
// Check last time videos were checked
$last_query_file = "last_query.txt";
$views_cache_file = "views_cache.txt";

$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
foreach($all_ids as $id_list) {
if(!file_exists($last_query_file)) {
// Time of Last Query Doesn't Exist
file_put_contents($last_query_file, serialize(new DateTime()));
}

if(!file_exists($views_cache_file)) {
// Views Cache Doesn't Exist
file_put_contents($views_cache_file, serialize([]));
}


$date_last_query = unserialize(file_get_contents($last_query_file));

//Check time since last query
$time_difference_minutes = $date_last_query->diff(new DateTime)->i;

if($time_difference_minutes >= 1) {
// If it's been more than 1 minute, cache video data

// Process videos in batches of 50
$all_ids = array_chunk(
array_keys($GLOBALS["data"]),
50);

curl_setopt_array($curl, [
CURLOPT_URL => "https://youtube.googleapis.com/youtube/v3/videos?part=statistics&id=" . implode(",", $id_list) . "&key=" . $GLOBALS["API_KEY"],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [],
]);
$response = json_decode(curl_exec($curl), true);
if (array_key_exists("items", $response)){
foreach($response["items"] as $vid) {
$GLOBALS["data"][$vid["id"]]->set_views($vid["statistics"]["viewCount"]);
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$all_responses = [];
foreach($all_ids as $id_list) {

curl_setopt_array($curl, [
CURLOPT_URL => "https://youtube.googleapis.com/youtube/v3/videos?part=statistics&id=" . implode(",", $id_list) . "&key=" . $GLOBALS["API_KEY"],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [],
]);
$response = json_decode(curl_exec($curl), true);
if (array_key_exists("items", $response)){
$all_responses = array_merge($all_responses,$response["items"]);
}
} else {
echo("API ERROR");
echo("Num of Videos:" . sizeof($id_list));
var_dump($response);
}
// Cache Video Data
file_put_contents($views_cache_file, serialize($all_responses));
// Update Time file
file_put_contents($last_query_file, serialize(new DateTime()));
} else {
// Get Data from file
$all_responses = unserialize(file_get_contents($views_cache_file));
}

//var_dump($all_responses);
//exit();

// Save video views to tree
foreach($all_responses as $vid) {
$GLOBALS["data"][$vid["id"]]->set_views($vid["statistics"]["viewCount"]);
}



//var_dump($GLOBALS["data"]);
//exit();

Expand Down Expand Up @@ -95,6 +132,7 @@ function gtag(){dataLayer.push(arguments);}
<!-- Title -->
<h1 id="title">CGP Grey: Rock Paper Scissors</h1>
<h2>A Visual and Statistical Overview</h2>
<p><i>Last Updated: <?=$date_last_query->format("y/m/d - H:i")?> UTC</i></p>
<br/><br/>

<!-- Visual Controls -->
Expand Down

0 comments on commit bd1c11b

Please sign in to comment.