Skip to content

Commit

Permalink
Add caching to PlexPyAPI module
Browse files Browse the repository at this point in the history
  • Loading branch information
hjone72 authored and hjone72 committed Oct 17, 2016
1 parent 3bc9ce8 commit 36ffc84
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions inc/modules/plexpyAPI.module.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
<?php
function plexpyAPI($command){
//Get Plex Library stats from PlexPy
function plexpyAPI($command, $cached = false){
//Get API cmd from PlexPy
if ($cached != false) {
if (array_key_exists('plexpy', $_SESSION)) {
if (array_key_exists($cached, $_SESSION['plexpy'])) {
return $_SESSION['plexpy'][$cached];
}
}
}

//Load module config
$module_config = parse_ini_file("config.module.ini.php"); //Config file that has configurations for site.

//Get the users friendly name from PlexPy
$pyapi = $module_config['pp_api'];
$pyurl = $module_config['pp_url'];
$return = json_decode(file_get_contents($pyurl . "/plexpy/api/v2?apikey=". $pyapi . "&cmd=" . $command));
return $return;

$host = "https://localhost:8181/plexpy/api/v2?apikey=". $pyapi . "&cmd=" . $command;
$process = curl_init($host);
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($process);
$curlError = curl_error($process);
curl_close($process);
$json = json_decode($data, true);

if ($cached != false){
$_SESSION['plexpy'][$cached] = $json;
}

return $json;
}
?>
?>

0 comments on commit 36ffc84

Please sign in to comment.