Created
November 14, 2017 22:10
-
-
Save Semx11/51207897accb5300104f7e5277206ac1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
use Predis\Client; | |
function getScore() { | |
$client = new Client(); | |
$data = json_decode($client->get('score'), true); | |
$time = time(); | |
if ($client->get('score_lock') || $time - intval($data['time']) < 3) { | |
return $data; | |
} | |
// Set a simple lock so it won't be requested multiple times | |
$client->set("score_lock", true); | |
$rawJson = file_get_contents("https://www.reddit.com/r/StarWarsBattlefront/comments/7cff0b/seriously_i_paid_80_to_have_vader_locked/dppum98.json"); | |
$comments = json_decode($rawJson, true); | |
$score = abs($comments[1]["data"]["children"][0]["data"]["score"]); | |
$data = array('score' => $score, 'time' => $time); | |
$client->set('score', json_encode($data)); | |
// Unlock | |
$client->set('score_lock', false); | |
return $data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment