Skip to content

Commit

Permalink
Data Download & UI Upgrade
Browse files Browse the repository at this point in the history
- Implemented a CSV download of the video data
- Improved landing page controls UI
  • Loading branch information
athuler committed Dec 28, 2023
1 parent 90fe615 commit 90cbf55
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 123 deletions.
18 changes: 18 additions & 0 deletions download.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
require("video_data.php");
header('Content-Type: text/csv; charset=utf-16');

echo("Video ID, Views, Probability, Children, Child Paths, Parents, Paths\n");
foreach($GLOBALS["data"] as $videoID => $currentVideo) {
echo(
$videoID . "," .
$currentVideo->views . "," .
$currentVideo->probability() . "," .
implode(";",$currentVideo->children) . "," .
implode(";",$currentVideo->child_path) . "," .
implode(";",$currentVideo->parents) . "," .
implode(";",$currentVideo->paths) . "," .
"\n"
);
}
?>
148 changes: 25 additions & 123 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,104 +9,8 @@

###### Create Video Tree #####
require_once("video_data.php");
$first_vid = "PmWQmZXYd74";


###### Build Paths & Parents #####
foreach($GLOBALS["data"] as $video_id => $video) {
$num_children = count($video->children);

for($j = 0; $j < $num_children; $j ++) {


// Add Parent
$GLOBALS["data"][$video->children[$j]]->add_parent($video_id);

// Add New Path(s)
$new_paths = [];
foreach($video->paths as $current_path) {
array_push($new_paths,
$current_path . $video->child_path[$j]
);
}
$GLOBALS["data"][$video->children[$j]]->add_path(array_unique($new_paths));

}
}


###### Fetch Video Views ######
// Check last time videos were checked
$last_query_file = "last_query.txt";
$views_cache_file = "views_cache.txt";

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
$date_last_query = new DateTime;

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

$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"]);
}
}
// 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();

?>

<!DOCTYPE html>
Expand Down Expand Up @@ -136,47 +40,45 @@ function gtag(){dataLayer.push(arguments);}
<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")?></i></p>
<br/><br/>

<!-- Visual Controls -->
<h3>Controls:</h3>
<input type="button" class="btn btn-primary" value="Show/Hide Children & Parents" onclick="toggle_family()"/>
<input type="button" class="btn btn-primary" value="Show/Hide All Paths" onclick="toggle_paths()"/>

<!-- Statistics -->
<br/><br/><br/>
<h3>Some Statistics:</h3><p>
<b><?=count(array_keys($GLOBALS["data"]))?></b> videos<br/>
<b><?php
// Compute total video views
$total_views = 0;
foreach($GLOBALS["data"] as $vid){
$total_views += $vid->views;
}
echo(number_format($total_views));
?></b> total views

<p>
<b><?=count(array_keys($GLOBALS["data"]))?></b> videos<br/>
<b><?php
// Compute total video views
$total_views = 0;
foreach($GLOBALS["data"] as $vid){
$total_views += $vid->views;
}
echo(number_format($total_views));
?></b> total views
</p>

<!-- Add Tip on Using Arrows -->
<p><i>Tip: Use <i class="bi bi-caret-down" style="font-size: 35px; color: red;"></i> and <i class="bi bi-caret-down" style="font-size: 35px; color: green;"></i> to navigate more easily!</i></p>
<div class="row">
<p><i>Tip: Use <a href="#PmWQmZXYd74"><i class="bi bi-caret-down" style="font-size: 35px; color: red;"></i></a> and <a href="#PmWQmZXYd74"><i class="bi bi-caret-down" style="font-size: 35px; color: green;"></i></a> to navigate more easily!</i></p>
</div>

<!-- Visual Controls -->
<input type="button" class="btn btn-secondary" value="Show/Hide Children & Parents" onclick="toggle_family()"/>
<input type="button" class="btn btn-secondary" value="Show/Hide All Paths" onclick="toggle_paths()"/>

<br/><br/>

<!-- About -->
<h3>About this Project:</h3>
<br/><br/>
<p>Made by <a href="https://github.com/athuler" target="__blank">athuler</a> with much blood, sweats, and so many tears.</p>
<p>Bug? Something wrong? <a href="https://github.com/athuler/CgpGreyRockPaperScissors/issues" target="__blank">Report it!</a></p>

<div class="row justify-content-center">
<a href="https://github.com/athuler/CgpGreyRockPaperScissors" target="__blank" class="col-auto">Source</a>

<a href="https://github.com/sponsors/athuler" target="__blank" class="col-auto"><img src="https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86"/></a>
<div>
<a href="https://github.com/athuler/CgpGreyRockPaperScissors" target="__blank" class="col-auto">Source</a> |
<a href="download">Download Data</a> |
<a href="https://github.com/athuler/CgpGreyRockPaperScissors/issues" target="__blank">Report a Bug</a> |
<a href="https://github.com/sponsors/athuler" target="__blank" class="col-auto"><img src="https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86"/></a>
</div>
</div>

</div>

<br/>
<!-- Connectors Between Blocks -->
<div id="svgContainer" style="margin: 0px 0px;">
<svg id="svg1" width="0" height="0" >
<?php
Expand Down
95 changes: 95 additions & 0 deletions video_data.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
require("secrets.php");

class video {
public $paths = [];
Expand Down Expand Up @@ -311,4 +312,98 @@ function alignment() {
$GLOBALS["data"]["yCwdjfzxI4I"] = new video(
["0odtRIBvjes","RVLUX6BUEJI"]);

$first_vid = "PmWQmZXYd74";


###### Build Paths & Parents #####
foreach($GLOBALS["data"] as $video_id => $video) {
$num_children = count($video->children);

for($j = 0; $j < $num_children; $j ++) {


// Add Parent
$GLOBALS["data"][$video->children[$j]]->add_parent($video_id);

// Add New Path(s)
$new_paths = [];
foreach($video->paths as $current_path) {
array_push($new_paths,
$current_path . $video->child_path[$j]
);
}
$GLOBALS["data"][$video->children[$j]]->add_path(array_unique($new_paths));

}
}


###### Fetch Video Views ######
// Check last time videos were checked
$last_query_file = "last_query.txt";
$views_cache_file = "views_cache.txt";

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
$date_last_query = new DateTime;

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

$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"]);
}
}
// 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"]);
}


?>

1 comment on commit 90cbf55

@athuler
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed #6

Please sign in to comment.