summaryrefslogtreecommitdiff
path: root/api/lyrics.php
blob: a0126eea976125fe59de2ed1fe22574436956f64 (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
<?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));