-
-
Save anonymous/0165bee16b4ffa9be4d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$page = json_decode(file_get_contents('https://www.reddit.com/r/TheRedPill/comments/3f8t92/the_30_day_challenge_1_sleep.json?showmore=true')); | |
$users = array(); | |
$comments = $page[1]->data->children; | |
$max_update = 1; | |
foreach($comments as $comment) { | |
//echo $comment->data->body . "\n\n"; | |
if(preg_match('/will take part in this challenge/', $comment->data->body)) { | |
$user = (object)array('name' => $comment->data->author, 'updates' => array()); | |
if(isset($comment->data->replies)) { | |
foreach($comment->data->replies->data->children as $child) { | |
if(preg_match('/Day (\d+):.*(\d+)(\.\d+)? hours?/U', $child->data->body, $matches)) { | |
$user->updates[intval($matches[1])] = floatval($matches[2] . (isset($matches[3]) ? $matches[3] : '')); | |
$max_update = min(30, max($max_update, intval($matches[1]))); | |
} | |
} | |
$users[] = $user; | |
} | |
} | |
} | |
$json_data = array(); | |
$header_row = array('Day'); | |
for($i = 1; $i <= $max_update; $i++) { | |
$json_data[] = array('#' . $i); | |
} | |
foreach($users as $user) { | |
if(!empty($user->updates)) { | |
$header_row[] = $user->name; | |
for($i = 1; $i <= $max_update; $i++) { | |
$json_data[$i - 1][] = isset($user->updates[$i]) ? $user->updates[$i] : 0; | |
} | |
} | |
} | |
array_unshift($json_data, $header_row); | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>TRP Sleep Challenge</title> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css"> | |
<style> | |
body { | |
width: 100%; | |
max-width: 960px; | |
margin: 20px auto 0; | |
font-family: Arial, sans-serif; | |
font-size: 14px; | |
color: #333; | |
} | |
h1 { | |
font-size: 28px; | |
margin-bottom: 20px; | |
} | |
th { | |
background: #fdd; | |
border: 1px solid #d99; | |
padding: 3px 8px; | |
border-left: none; | |
} | |
th:first-child { | |
border-left: 1px solid #d99; | |
} | |
td { | |
background: #fff; | |
border: 1px solid #aaa; | |
padding: 3px 8px; | |
border-left: none; | |
border-top: none; | |
} | |
td:first-child { | |
border-left: 1px solid #aaa; | |
} | |
.is-empty { | |
display: none; | |
} | |
p.action { | |
margin-bottom: 10px; | |
} | |
.show-empty { | |
display: inline-block; | |
*display: inline; | |
*zoom: 1; | |
padding: 5px 5px; | |
background: #eee; | |
border: 1px solid #aaa; | |
border-radius: 3px; | |
-moz-border-radius: 3px; | |
-webkit-border-radius: 3px; | |
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3); | |
-moz-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3); | |
-webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3); | |
color: #333; | |
text-decoration: none; | |
cursor: pointer; | |
} | |
.show-empty:hover { | |
background: #e5e5e5; | |
} | |
.show-empty .fa { | |
display: inline-block; | |
*display: inline; | |
*zoom: 1; | |
margin-right: 2px; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>TRP Sleep Challenge Data Porn</h1> | |
<div id="chart"></div> | |
<p class="action">Toggle users with no updates: <a href="#" class="show-empty"><i class="fa fa-eye"></i> Toggle</a></p> | |
<table> | |
<thead> | |
<tr> | |
<th>User</th> | |
<?php for($i = 1; $i <= $max_update; $i++) : ?> | |
<th>Day #<?= $i; ?></th> | |
<?php endfor; ?> | |
</tr> | |
</thead> | |
<tbody> | |
<?php foreach($users as $user) : ?> | |
<tr<?php if(empty($user->updates)) echo ' class="is-empty"'; ?>> | |
<td><?php echo $user->name; ?></td> | |
<?php for($i = 1; $i <= $max_update; $i++) : ?> | |
<td><?php echo isset($user->updates[$i]) ? $user->updates[$i] : ''; ?></td> | |
<?php endfor; ?> | |
</tr> | |
<?php endforeach; ?> | |
</tbody> | |
</table> | |
<script type="text/javascript" | |
src="https://www.google.com/jsapi?autoload={ | |
'modules':[{ | |
'name':'visualization', | |
'version':'1', | |
'packages':['corechart'] | |
}] | |
}"></script> | |
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script> | |
<script> | |
google.setOnLoadCallback(function(){ | |
var data = google.visualization.arrayToDataTable(<?php echo json_encode($json_data); ?>); | |
var options = { | |
title: 'Sleep Challenge: Performance', | |
legend: { position: 'bottom' } | |
}; | |
var chart = new google.visualization.LineChart(document.getElementById('chart')); | |
chart.draw(data, options); | |
}); | |
jQuery(function($){ | |
$('.show-empty').click(function(e){ | |
$('.is-empty').toggle(); | |
return false; | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment