Update README because I forgot how to develop this app.
[Marketplaylister.git] / searchify.php
1 <?php
2
3 const BASE_URL = 'https://api.spotify.com/v1/';
4 // Currently updated manually whenever I get one from the server
5 const SPOT_TOKEN = 'BQBU1Qs3ROpkN9CwlQNpZS00khdSU61zuejyKbjS4KiIszK8aiLaTd9TfPiSH0OsmtWStOVL7ym-QYEBWyLX3qlFIN5peit0n6_B-LLtz4C8KSh3Dxj5O3jf4HSWf3fFISC4cLbznfSV3QnpQ4vdnCTehz4vT8V54XDiG2hX275Uw_gDHzKjqFWQo249-rY42rBv7pf555wQ2PSBymuZMcDlIDEeAbGiyRI';
6
7 $pdo = new PDO("sqlite:mktplc.sqlite3");
8
9 $stmt = $pdo->prepare("SELECT * FROM SONGS WHERE uri IS NULL");
10 $upstmt = $pdo->prepare("UPDATE songs SET (uri) = :uri WHERE id = :id");
11 $upstmt->bindParam(':uri', $uri);
12 $upstmt->bindParam(':id', $id);
13
14 if ($stmt->execute()) {
15 while ($row = $stmt->fetch()) {
16
17 $track_opts = [
18 'http' => [
19 'method' => 'GET',
20 'header' => 'Authorization: Bearer ' . SPOT_TOKEN . "\r\n"
21 ]
22 ];
23
24 $track_context = stream_context_create($track_opts);
25
26 $track_search_url = BASE_URL . 'search?q=track:' . urlencode($row['track'])
27 . '+artist:' . urlencode($row['artist']) . '&type=track';
28
29 $trackReq = file_get_contents($track_search_url, false, $track_context);
30 if ($trackReq) {
31 $trackJSON = json_decode($trackReq, true);
32 $trackJSON = $trackJSON['tracks'];
33 if ($trackJSON['total'] === 0) {
34 continue;
35 }
36
37 $uri = $trackJSON['items'][0]['uri'];
38 $id = $row['id'];
39 $upstmt->execute();
40
41 sleep(1);
42 }
43 }
44 }
45