Created
December 4, 2024 23:02
-
-
Save xrobau/82d70763d9295696bac9a5327b364b48 to your computer and use it in GitHub Desktop.
Simple Plex History reporter
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
#!/usr/bin/env php | |
<?php | |
// Setup: (Run this in a root shell, either log in as root or use 'sudo -i') | |
// You should have the following packages installed | |
// apt-get -y install composer php-xml php-sqlite3 | |
// Install Guzzle | |
// composer require guzzlehttp/guzzle | |
// | |
// Set the token - there's many ways to get it, including | |
// the 'official' way: | |
// https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/ | |
$token = 'PUT YOUR TOKEN HERE'; // It looks like '9wZM7erqoYTfaCsi7jyQ'; | |
// If this is NOT where your db file is, edit this. Note you don't | |
// need to run this on the plex server, you can just copy the | |
// sqlite file to any random unix machine. | |
$dbfile = '/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db'; | |
// Now you can run this script 'php plexstats.php' and you'll get some output | |
// of who has not been using it! | |
use GuzzleHttp\Client; | |
require 'vendor/autoload.php'; | |
$url = 'https://clients.plex.tv/api/users'; | |
$debug = false; | |
$pdo = new \PDO("sqlite://$dbfile", '', '', [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]); | |
$pdo->exec('PRAGMA journal_mode=WAL'); | |
$sql = "select | |
accounts.id, | |
accounts.name, | |
datetime(max(statistics_media.at), 'unixepoch') lastplay, | |
count(statistics_media.id) plays | |
from accounts left join statistics_media on statistics_media.account_id=accounts.id | |
where accounts.id=?"; | |
$query = $pdo->prepare($sql); | |
$client = new Client([ 'base_uri' => $url ]); | |
$req = $client->get('', ['query' => [ "X-Plex-Token" => $token ], "debug" => $debug ]); | |
$b = (string) $req->getBody(); | |
$xml = simplexml_load_string($b); | |
$users = []; | |
$byplaycount = []; | |
// Attributes we care about from plex.tv's xml response | |
$atts = [ "id", "title", "username", "email", "home", "restricted" ]; | |
foreach ($xml->User as $u) { | |
if (!$u->Server) { | |
// No server access, don't care | |
continue; | |
} | |
$retarr = []; | |
foreach ($atts as $a) { | |
$retarr[$a] = current($u->attributes()->$a ?? ["err"]); | |
} | |
$id = (string) $retarr['id']; | |
if (!$query->execute([$id])) { | |
throw new \Exception("Something failed asking for $id"); | |
} | |
$result = $query->fetch(\PDO::FETCH_ASSOC); | |
$retarr['result'] = $result; | |
$users[$id] = $retarr; | |
$plays = $result['plays']; | |
$byplaycount[$plays][$id] = $retarr; | |
} | |
krsort($byplaycount); | |
foreach ($byplaycount as $c => $ids) { | |
if ($c < 5 ) { | |
print "There are ".count($ids)." with $c playcount\n"; | |
foreach ($ids as $id => $row) { | |
$lastplay = $row['result']['lastplay'] ?? 'never!'; | |
print " $id lastplay is $lastplay. Data:\n ".json_encode($row)."\n"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment