search.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. $title = htmlspecialchars($_GET["query"]);
  3. ?>
  4. <?php include "elements/header.php" ?>
  5. <?php
  6. $ch = curl_init("https://bandcamp.com/api/bcsearch_public_api/1/autocomplete_elastic");
  7. curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
  8. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
  9. "search_text" => $_GET["query"],
  10. "search_filter" => "",
  11. "full_page" => true
  12. ]));
  13. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  14. $results = json_decode(curl_exec($ch))->auto->results;
  15. echo "<div class=results>";
  16. foreach ($results as $result) {
  17. $link = $result->item_url_path ?? $result->item_url_root;
  18. switch ($result->type) {
  19. case "b":
  20. $domain = explode(".", parse_url($result->item_url_root, PHP_URL_HOST));
  21. if (end($domain) === "com" && prev($domain) === "bandcamp")
  22. /* TODO: Some artists and labels use custom domains for their pages.
  23. Blindly sending requests to these could be a security risk.
  24. Is there a good way to support these? */
  25. $link = "artist.php?name=" . prev($domain);
  26. break;
  27. };
  28. $image = $result->type === "b"
  29. ? $result->img
  30. : str_replace("/img/", "/img/a", $result->img);
  31. $image = "image.php?file=" . basename($image);
  32. echo "<a href=" . $link . ">";
  33. echo "<div>";
  34. echo "<img src=" . $image . ">";
  35. echo "<p>";
  36. echo htmlspecialchars($result->name);
  37. switch ($result->type) {
  38. case "a":
  39. echo "<br>";
  40. echo "<small>by " . htmlspecialchars($result->band_name) . "</small>";
  41. break;
  42. case "b":
  43. /* TODO: Some artists seem to use a different image URL scheme prefixed
  44. with an `a`, similar to releases and tracks. Is it possible to
  45. determine which scheme is used when? */
  46. break;
  47. case "t":
  48. echo "<br>";
  49. echo "<small>";
  50. echo "by " . htmlspecialchars($result->band_name);
  51. echo "<br>";
  52. echo "on " . htmlspecialchars($result->album_name ?? $result->name);
  53. echo "</small>";
  54. break;
  55. };
  56. echo "</p>";
  57. echo "</div>";
  58. echo "</a>";
  59. };
  60. echo "</div>";
  61. ?>
  62. <?php include "elements/footer.php" ?>