Subversion Repositories cheapmusic

Rev

Rev 17 | Rev 38 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 - 1
<?php
20 - 2
error_reporting(E_ALL);
10 - 3
 
4
// BUGBUG {"message": "You are making requests too quickly."}
5
 
6
function findDiscogsMaster($str) {
17 - 7
    $maxMasterCount = 6;
8
    $cardHide = array ("", "hide-extra-small", "hide-small", "hide-small", "hide-medium", "hide-medium");
10 - 9
    $vendors = Vendors::getInstance();
10
    $config = $vendors->getVendor(Vendors::DISCOGS);
13 - 11
 
10 - 12
    $_SESSION["discogs"] = "";
13
 
14
    $url = "https://api.discogs.com/database/search?";
15
    $url .= "token=";
16
    $url .= $config['token'];
17
    if (empty($_SESSION["barcode"]['Type'])) {
18
        $url .= "&type=master";
19
        $url .= "&q=";
20
        $url .= urlencode($str);
21
    } else {
22
        $url .= "&barcode=";
23
        $url .= $_SESSION["barcode"]['Value'];
24
    }
25
 
13 - 26
	$searchResp = getUrl($url, $config['userAgent']);
10 - 27
	$searchResp = utf8_encode($searchResp);
28
	$searchResp = json_decode($searchResp);
29
 
13 - 30
    $_SESSION["discogs"] = "";
10 - 31
 
32
    if (!empty($searchResp->{'message'})) {
33
        if ($searchResp->{'message'} == "You are making requests too quickly.") {
34
// bugbug
35
            return;
36
        }
14 - 37
        return;
10 - 38
    }
39
 
40
    if ($searchResp->{'pagination'}->{'items'} < 1) {
41
        return;
42
    }
43
 
17 - 44
    $_SESSION["discogs"] .= "<div class=\"container-fluid bg-light\">";
13 - 45
    $_SESSION["discogs"] .= "<h4 class=\"text-center\">Matching Albums</h4>";
17 - 46
    $_SESSION["discogs"] .= "<div class=\"card-deck\">";
13 - 47
 
10 - 48
    $masterCount = 0;
49
    $processedMasters = [];
50
    foreach ($searchResp->{'results'} as $searchey => $searchValue) {
51
        if ($searchValue->{'type'} != "master" && empty($_SESSION["barcode"]['Type'])) {
52
            continue;
53
        }
54
 
17 - 55
        if (++$masterCount > 6) {
10 - 56
            break;
57
        }
58
 
59
        if (in_array($searchValue->{'master_id'}, $processedMasters)) {
60
            continue;
61
        }
62
        $processedMasters[] = $searchValue->{'master_id'};
63
 
20 - 64
	    $masterResp = getUrl($searchValue->{'master_id'} != 0 ? $searchValue->{'master_url'} : $searchValue->{'resource_url'}, $config['userAgent']);
10 - 65
	    $masterResp = utf8_encode($masterResp);
66
    	$masterResp = json_decode($masterResp);
67
 
68
        if (!empty($searchResp->{'message'})) {
69
            if ($searchResp->{'message'} == "You are making requests too quickly.") {
70
// bugbug
71
                return;
72
            }
17 - 73
 
14 - 74
            return;
10 - 75
        }
76
 
17 - 77
        $_SESSION["discogs"] .= processMaster($masterResp, $searchValue->{'thumb'}, $masterCount, $cardHide[$masterCount - 1]);
78
    }
10 - 79
 
20 - 80
// bugbug
17 - 81
    if ($masterCount < $maxMasterCount) {
82
        for (; $masterCount <= $maxMasterCount; $masterCount++) {
83
            $_SESSION["discogs"] .= "<div class=\"card invisible " . $cardHide[$masterCount - 1] . "\"></div>";
84
        }
10 - 85
    }
86
 
17 - 87
 
10 - 88
    $_SESSION["discogs"] .= "</div>";
13 - 89
    $_SESSION["discogs"] .= "</div>";
10 - 90
 
91
    return;
92
}
93
 
17 - 94
function processMaster($master, $thumbnail, $cnt, $hideClass) {
10 - 95
    $artists = [];
96
    foreach ($master->{'artists'} as $key => $value) {
20 - 97
        $artists[] = trim(preg_replace('/\([0-9]+\)$/', "", (string)$value->{'name'}));
10 - 98
    }
99
 
100
    $genres = [];
101
    foreach ($master->{'genres'} as $key => $value) {
20 - 102
        $genres[] =  (string)$value;
10 - 103
    }
104
 
20 - 105
    $labels = [];
106
    if (isset($master->{'labels'})) {
107
        foreach ($master->{'labels'} as $key => $value) {
108
            $labels[] = trim(preg_replace('/\([0-9]+\)$/', "", (string)$value->{'name'}));
109
        }
110
    }
111
 
112
    $formats = [];
113
    if (isset($master->{'formats'})) {
114
        foreach ($master->{'formats'} as $key => $value) {
115
            $formats[] = trim(preg_replace('/\([0-9]+\)$/', "", (string)$value->{'name'}));
116
        }
117
    }
118
 
119
    $country = '';
120
    if (isset($master->{'country'})) {
121
        $country = (string)$master->{'country'};
122
    }
123
 
17 - 124
    $str = "<div class=\"card " . $hideClass . "\">";
125
    $str .= "<div class=\"card-header bg-secondary text-white\">";
20 - 126
    $str .= "<h6 class=\"card-title\">". (string)$master->{'title'} . " by ";
17 - 127
    $searchArtists = "";
20 - 128
    if (count($artists) < 5) {
14 - 129
        $str .= join(", ", $artists);
17 - 130
        if ($artists[0] != 'Various') { $searchArtists = join(" ", $artists);}
14 - 131
    } else {
132
        $str .= "Various Artists";
133
    }
134
    $str .= "</h6>";
135
    $str .= "</div>";
136
    if (empty($thumbnail)) {
137
        $thumbnail = "images/no-image-available.jpg";
138
    }
20 - 139
    $str .= "<img class=\"btn card-img-top mx-auto\" style=\"max-width:250px;\" src=\"" . $thumbnail . "\"  data-toggle=\"modal\" data-target=\"#masterModal" . $cnt . "\" title=\"Album Info\" data-toggle2=\"tooltip\" data-delay=\"200\" alt=\"Discogs Master Thumbnail\">";
140
    $str .= "<form method=\"post\" action=\"/index.php\" onsubmit=\"progressBar('Searching for ' + document.getElementById('discogsSearchTerm" . $cnt . "').value);\">";
141
    $str .= "  <input type=\"hidden\" name=\"sessionTab\" value=\"" . MySessionHandler::getSessionTab() . "\">";
142
    // bugbug barcode would be best
143
    $str .= "  <input id=\"discogsSearchTerm" . $cnt . "\" type=\"hidden\" name=\"discogsSearchTerm\" value=\"" . $searchArtists . " " . (string)$master->{'title'} . "\">";
144
    $str .= "  <button type=\"submit\" name=\"submit\" value=\"discogsSearch\" class=\"btn btn-primary btn-sm mt-2 float-right\"><i class=\"fas fa-search\" title=\"Find listings for this album\" data-toggle=\"tooltip\" data-delay=\"200\"></i></button>";
145
    $str .= "</form>";
14 - 146
    $str .= "</div>";
147
 
148
    $str .= "<div id=\"masterModal" . $cnt . "\" class=\"modal\">";
149
    $str .= "<div class=\"modal-dialog\">";
150
    $str .= "<div class=\"modal-content\">";
151
    $str .= "<div class=\"modal-header bg-primary\">";
20 - 152
    $str .= "<h4 class=\"modal-title\">". (string)$master->{'title'} . " by ";
153
    if (count($artists) < 5) {
14 - 154
        $str .= join(", ", $artists);
155
    } else {
156
        $str .= "Various Artists";
157
    }
158
    $str .= "</h4>";
159
    $str .= "<button type=\"button\" class=\"close\" data-dismiss=\"modal\"><i class=\"fas fa-window-close\" style=\"font-size:24px\"></i></button>";
160
    $str .= "</div>";
161
 
162
    $str .= "<div class=\"modal-body mx-auto\">";
163
 
164
    $str .= "<table class=\"table-borderless table-condensed small\">";
20 - 165
    $str .= "<tr><td class=\"px-1\">Title:</td><td class=\"px-1\">" . (string)$master->{'title'} . "</td></tr>";
14 - 166
 
167
    $str .= "<tr><td class=\"px-1\">Artist:</td><td class=\"px-1\">" . join(", ", $artists) . "</td></tr>";
168
 
20 - 169
    if (!empty($labels)) {
170
        $str .= "<tr><td class=\"px-1\">Label:</td><td class=\"px-1\">" . $labels[0] . "</td></tr>";
171
    }
172
 
173
    if (!empty($formats)) {
174
        $str .= "<tr><td class=\"px-1\">Format:</td><td class=\"px-1\">" . join(", ", $formats) . "</td></tr>";
175
    }
176
 
177
    if (!empty($country)) {
178
        $str .= "<tr><td class=\"px-1\">Country:</td><td class=\"px-1\">" . $country . "</td></tr>";
179
    }
180
 
14 - 181
    if ($master->{'year'} > 0) {
20 - 182
        $str .= "<tr><td class=\"px-1\">Year:</td><td class=\"px-1\">" . (string)$master->{'year'} . "</td></tr>";
14 - 183
    }
184
 
185
    $str .= "<tr><td class=\"px-1\">Genre:</td><td class=\"px-1\">" . join(", ", $genres) . "</td></tr>";
186
 
10 - 187
    if (isset($master->{'styles'})) {
188
        $styles = [];
189
        foreach ($master->{'styles'} as $key => $value) {
190
            $styles[] = $value;
191
        }
14 - 192
        $str .= "<tr><td class=\"px-1\">Style:</td><td class=\"px-1\">" . join(", ", $styles) . "</td></tr>";
10 - 193
    }
14 - 194
 
195
    if (is_array($master->{'tracklist'})) {
20 - 196
        $str .= "<tr><td colspan=\"2\" class=\"px-1\">Tracks:</td></tr>";
14 - 197
        $str .= "<tr><td colspan=\"2\">";
198
        $str .= "<ul class=\"pl-3 pt-0 small list-unstyled\">";
199
        foreach ($master->{'tracklist'} as $key => $value) {
20 - 200
            if ((string)$value->{'type_'} == "heading") {
201
                $str .= "<li class=\"font-weight-bold\">" . (string)$value->{'title'} . "</li>";
202
            } else if ((string)$value->{'type_'} == "index") {
203
                $str .= "<li><span class=\"font-italic\">" . (string)$value->{'title'} . "</span>";
14 - 204
                foreach ($value->{'sub_tracks'} as $subkey => $subvalue) {
205
                    $str .= "<ul class=\"pl-3 pt-0 list-unstyled\">";
206
                    $str .= processTrack($subvalue, false);
207
                    $str .= "</ul>";
17 - 208
                }
14 - 209
                $str .= "</li>";
20 - 210
            } else if ((string)$value->{'type_'} == "track") {
14 - 211
                $str .= processTrack($value, true);
212
            }
213
        }
214
        $str .= "</ul>";
17 - 215
        $str .= "</td></tr>";
216
    }
14 - 217
 
10 - 218
    $str .= "</table>";
14 - 219
    $str .= "</div>";
220
    $str .= "<div class=\"modal-footer justify-content-between\">";
20 - 221
    $str .= "<span class=\"font-weight-lighter small\">Data provided by <a href=\"" . (string)$master->{'uri'} . "\" target=\"_blank\">Discogs</a></span>";
14 - 222
    $str .= "<span class=\"float-right\"><button type=\"button\" class=\"btn btn-danger\" data-dismiss=\"modal\">Close</button></span>";
223
    $str .= "</div>";
224
    $str .= "</div>";
225
    $str .= "</div>";
226
    $str .= "</div>";
10 - 227
 
14 - 228
    return $str;
229
}
10 - 230
 
14 - 231
function processTrack($value, $posFlag) {
232
    $str = "<li>";
233
    if ($posFlag && !empty($value->{'position'})) {
20 - 234
        if (!preg_match("/^[a-zA-Z][0-9]/", (string)$value->{'position'}) && !preg_match("/^[a-zA-Z]$/", (string)$value->{'position'})) {
235
            $str .= (string)$value->{'position'} . '. ';
10 - 236
        }
14 - 237
    }
17 - 238
 
20 - 239
    $str .= (string)$value->{'title'};
10 - 240
 
14 - 241
    $trackArtists = [];
242
    if (isset($value->{'artists'})) {
243
        foreach ($value->{'artists'} as $taKey => $taValue) {
20 - 244
            $trackArtists[] = trim(preg_replace('/\([0-9]+\)$/', "", (string)$taValue->{'name'}));
10 - 245
        }
14 - 246
        if (count($trackArtists)) {
247
            $str .= " - " . join(", ", $trackArtists);
248
        }
249
    }
10 - 250
 
14 - 251
    if (!empty($value->{'duration'})) {
20 - 252
        $str .= " [" . (string)$value->{'duration'} . "]";
10 - 253
    }
13 - 254
 
14 - 255
    $str .= "</li>";
256
 
17 - 257
    return $str;
14 - 258
}