]> jfr.im git - plex-to-listenbrainz.git/commitdiff
init
authorJohn Runyon <redacted>
Wed, 5 Jan 2022 20:48:38 +0000 (14:48 -0600)
committerJohn Runyon <redacted>
Wed, 5 Jan 2022 20:48:38 +0000 (14:48 -0600)
plex-to-listenbrainz.php [new file with mode: 0644]

diff --git a/plex-to-listenbrainz.php b/plex-to-listenbrainz.php
new file mode 100644 (file)
index 0000000..f0be39c
--- /dev/null
@@ -0,0 +1,72 @@
+<?php
+function err($s) {
+       error_log($s);
+}
+if (empty($_GET['token'])) {
+       err("Must specify ?token with your ListenBrainz token.");
+       exit;
+}
+if (empty($_GET['user'])) {
+       err("Must specify ?user with your Plex username.");
+       exit;
+}
+
+$plex = json_decode($_POST['payload'], TRUE);
+if (!in_array($plex['event'], explode(' ', 'media.scrobble media.play media.resume'))) {
+       exit;
+}
+if (mb_strtolower($plex['Account']['title']) != mb_strtolower($_GET['user'])) {
+       exit;
+}
+
+$DIR = dirname(readlink(__FILE__));
+$listenbrainz = array(
+       'listen_type' => (($plex['event'] == 'media.scrobble') ? 'single' : 'playing_now'),
+       'payload' => array(array(
+               'track_metadata' => array(
+                       'artist_name' => $plex['Metadata']['originalTitle'] ?? $plex['Metadata']['grandparentTitle'],
+                       'track_name' => $plex['Metadata']['title'],
+                       'release_name' => $plex['Metadata']['parentTitle'],
+                       'additional_info' => array(
+                               'media_player' => 'Plex',
+                               'submission_client' => 'https://jfr.im/git/plex-to-listenbrainz.git/',
+                               'submission_client_version' => `git --git-dir $DIR describe --long --tags HEAD`,
+                               'tracknumber' => $plex['Metadata']['index'],
+                       ),
+               ),
+       )),
+);
+if ($plex['event'] == 'media.scrobble') {
+       $listenbrainz['payload'][0]['listened_at'] = time();
+       $listenbrainz['listen_type'] = 'single';
+} else {
+       $listenbrainz['listen_type'] = 'playing_now';
+}
+
+if (!empty($plex['Metadata']['attribution'])) {
+       $listenbrainz['payload'][0]['track_metadata']['additional_info']['music_service'] = implode('.', array_reverse(explode('.', $plex['Metadata']['attribution'])));
+}
+
+$lb_payload = json_encode($listenbrainz);
+
+$ch = curl_init('https://api.listenbrainz.org/1/submit-listens');
+curl_setopt_array($ch, array(
+       CURLOPT_HTTPHEADER => array(
+               'Content-Type: application/json',
+               'Authorization: Token ' . $_GET['token'],
+       ),
+       CURLOPT_RETURNTRANSFER => TRUE,
+       CURLOPT_POSTFIELDS => json_encode($listenbrainz),
+));
+$resp = curl_exec($ch);
+if ($resp === FALSE) {
+       err("Failed to reach ListenBrainz: curl " . curl_errno($ch) . ": " . curl_error($ch));
+       exit;
+}
+$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+if ($status != 200) {
+       err("Error response from ListenBrainz: HTTP $status: $resp");
+       exit;
+}
+
+file_put_contents('/home/jrunyon/plexlog', var_export($status, true) . ';' . var_export($resp, true), FILE_APPEND);