summaryrefslogtreecommitdiff
path: root/pages/api/plex.php
blob: 6d7ad8f91e177358bcd83ddf9f4968d05aee2dd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php

require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/random.inc";
$app = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/app.json"), true);

function formatTitle($metadata) {
    if ($metadata['grandparentTitle']) {
        return $metadata['grandparentTitle'];
    } else {
        $ret = $metadata['title'];
    if ($metadata['year']) {
        $ret .= " (" . $metadata['year'] . ")";
    }
    return $ret;
  }
}

function formatSubtitle($metadata) {
    $ret = '';

  if ($metadata['grandparentTitle']) {
      if ($metadata['type'] === 'track') {
          $ret = $metadata['parentTitle'];
      } else if ($metadata['index'] && $metadata['parentIndex']) {
          $ret = "S" . $metadata['parentIndex'] . " E" . $metadata['index'];
      } else if ($metadata['originallyAvailableAt']) {
          $ret = $metadata['originallyAvailableAt'];
      }

      if ($metadata['title']) {
          $ret .= ' - ' . $metadata['title'];
      }
  } else if ($metadata['type'] === 'movie') {
      $ret = $metadata['tagline'];
  }

  return $ret;
}

$payload = json_decode($_POST["payload"], true);

if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/assets/cache")) mkdir($_SERVER['DOCUMENT_ROOT'] . "/assets/cache");
$id = random(32);

if ($payload["Metadata"]["type"] === "track") {
    file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/assets/cache/" . $id . ".jpg", file_get_contents("https://plex.equestria.dev" . $payload["Metadata"]["thumb"] . "?X-Plex-Token=" . $app["plex"]));
} else {
    file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/assets/cache/" . $id . ".jpg", file_get_contents("https://plex.equestria.dev" . $payload["Metadata"]["grandparentThumb"] . "?X-Plex-Token=" . $app["plex"]));
}

file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/.files.json", json_encode($_FILES));

if ($payload["event"] === "playback.started" || $payload["event"] === "media.play") {
    $hookObject = json_encode([
        "username" => "Plex",
        "avatar_url" => "https://support.plex.tv/wp-content/themes/plex/assets/img/favicons/plex-192.png",
        "embeds" => [
            [
                "title" => formatTitle($payload["Metadata"]),
                "type" => "rich",
                "description" => formatSubtitle($payload["Metadata"]),
                "color" => hexdec( "2b2d31" ),
                "thumbnail" => [
                    "url" => "https://ponies.equestria.horse/assets/cache/" . $id . ".jpg"
                ],
                "footer" => [
                    "text" => $payload["Account"]["title"] . " · Playing from " . $payload["Player"]["title"],
                    "icon_url" => $payload["Account"]["thumb"]
                ]
            ]
        ]

    ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
}

if (isset($hookObject)) {
    $ch = curl_init();

    curl_setopt_array( $ch, [
        CURLOPT_URL => $app["webhook"]["plex"],
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $hookObject,
        CURLOPT_HTTPHEADER => [
            "Content-Type: application/json"
        ]
    ]);

    $response = curl_exec( $ch );
    curl_close( $ch );
}