]>
Commit | Line | Data |
---|---|---|
c425298e JR |
1 | <?php |
2 | function err($s) { | |
3 | error_log($s); | |
4 | } | |
5 | if (empty($_GET['token'])) { | |
6 | err("Must specify ?token with your ListenBrainz token."); | |
7 | exit; | |
8 | } | |
9 | if (empty($_GET['user'])) { | |
10 | err("Must specify ?user with your Plex username."); | |
11 | exit; | |
12 | } | |
13 | ||
14 | $plex = json_decode($_POST['payload'], TRUE); | |
15 | if (!in_array($plex['event'], explode(' ', 'media.scrobble media.play media.resume'))) { | |
16 | exit; | |
17 | } | |
18 | if (mb_strtolower($plex['Account']['title']) != mb_strtolower($_GET['user'])) { | |
19 | exit; | |
20 | } | |
21 | ||
22 | $DIR = dirname(readlink(__FILE__)); | |
23 | $listenbrainz = array( | |
24 | 'listen_type' => (($plex['event'] == 'media.scrobble') ? 'single' : 'playing_now'), | |
25 | 'payload' => array(array( | |
26 | 'track_metadata' => array( | |
27 | 'artist_name' => $plex['Metadata']['originalTitle'] ?? $plex['Metadata']['grandparentTitle'], | |
28 | 'track_name' => $plex['Metadata']['title'], | |
29 | 'release_name' => $plex['Metadata']['parentTitle'], | |
30 | 'additional_info' => array( | |
31 | 'media_player' => 'Plex', | |
32 | 'submission_client' => 'https://jfr.im/git/plex-to-listenbrainz.git/', | |
33 | 'submission_client_version' => `git --git-dir $DIR describe --long --tags HEAD`, | |
34 | 'tracknumber' => $plex['Metadata']['index'], | |
35 | ), | |
36 | ), | |
37 | )), | |
38 | ); | |
39 | if ($plex['event'] == 'media.scrobble') { | |
40 | $listenbrainz['payload'][0]['listened_at'] = time(); | |
41 | $listenbrainz['listen_type'] = 'single'; | |
42 | } else { | |
43 | $listenbrainz['listen_type'] = 'playing_now'; | |
44 | } | |
45 | ||
46 | if (!empty($plex['Metadata']['attribution'])) { | |
47 | $listenbrainz['payload'][0]['track_metadata']['additional_info']['music_service'] = implode('.', array_reverse(explode('.', $plex['Metadata']['attribution']))); | |
48 | } | |
49 | ||
50 | $lb_payload = json_encode($listenbrainz); | |
51 | ||
52 | $ch = curl_init('https://api.listenbrainz.org/1/submit-listens'); | |
53 | curl_setopt_array($ch, array( | |
54 | CURLOPT_HTTPHEADER => array( | |
55 | 'Content-Type: application/json', | |
56 | 'Authorization: Token ' . $_GET['token'], | |
57 | ), | |
58 | CURLOPT_RETURNTRANSFER => TRUE, | |
59 | CURLOPT_POSTFIELDS => json_encode($listenbrainz), | |
60 | )); | |
61 | $resp = curl_exec($ch); | |
62 | if ($resp === FALSE) { | |
63 | err("Failed to reach ListenBrainz: curl " . curl_errno($ch) . ": " . curl_error($ch)); | |
64 | exit; | |
65 | } | |
66 | $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
67 | if ($status != 200) { | |
68 | err("Error response from ListenBrainz: HTTP $status: $resp"); | |
69 | exit; | |
70 | } | |
71 | ||
72 | file_put_contents('/home/jrunyon/plexlog', var_export($status, true) . ';' . var_export($resp, true), FILE_APPEND); |