diff options
Diffstat (limited to 'api/lyrics.php')
-rw-r--r-- | api/lyrics.php | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/api/lyrics.php b/api/lyrics.php new file mode 100644 index 0000000..a0126ee --- /dev/null +++ b/api/lyrics.php @@ -0,0 +1,57 @@ +<?php + +header("Content-Type: application/json"); +require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/session.php"; global $songs; +$token = json_decode(file_get_contents("/opt/spotify/token.json"), true); + +if (!isset($_GET["id"]) || !isset($songs[$_GET["id"]])) { + die(); +} + +$song = $songs[$_GET["id"]]; + +$id = json_decode(file_get_contents("https://api.spotify.com/v1/search?q=" . rawurlencode("track:" . $song["title"] . " artist:" . $song["artist"]) . "&type=track", false, stream_context_create([ + "http" => [ + "method" => "GET", + "header" => "Authorization: Bearer " . $token["access_token"] . "\r\n" + ] +])), true)["tracks"]["items"][0]["id"]; + +$data = json_decode(file_get_contents("http://localhost:8000/public/?trackid=" . $id), true); +if (isset($data) && str_ends_with($data["syncType"], "_SYNCED")) { + die(json_encode([ + "synced" => true, + "payload" => $data["lines"] + ], JSON_PRETTY_PRINT)); +} + +$genius = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/app.json"), true)["genius"]; + +$id = json_decode(file_get_contents("https://api.genius.com/search?q=" . rawurlencode($song["title"] . " " . $song["artist"]), false, stream_context_create([ + "http" => [ + "method" => "GET", + "header" => "Authorization: Bearer " . $genius . "\r\n" + ] +])), true)["response"]["hits"][0]["result"]["id"]; + +$data = []; +exec('bash -c "cd /opt/spotify/spotify-lyrics-api; python genius.py ' . $id . '"', $data); +$data = array_slice(array_map(function ($i) { + if (str_ends_with($i, "1Embed")) { + return substr($i, 0, -6); + } else { + return $i; + } +}, $data), 1); + +if (count($data) > 0) { + die(json_encode([ + "synced" => false, + "payload" => implode("\n", $data) + ], JSON_PRETTY_PRINT)); +} + +die(json_encode([ + "synced" => false, + "payload" => null +], JSON_PRETTY_PRINT));
\ No newline at end of file |