|
| 1 | +<?php |
| 2 | + |
| 3 | +use AmpacheDiscogs\Discogs; |
| 4 | + |
| 5 | +require dirname(__DIR__) . '/vendor/autoload.php'; |
| 6 | + |
| 7 | +// your own username nad password are required to use the Discogs API |
| 8 | +$username = null; |
| 9 | +$password = null; |
| 10 | +$discogs = new Discogs($username, $password); |
| 11 | + |
| 12 | +$media_info = [ |
| 13 | + [ |
| 14 | + 'album' => 'The Shape', |
| 15 | + 'albumartist' => 'Code 64', |
| 16 | + ], |
| 17 | + [ |
| 18 | + 'album' => null, |
| 19 | + 'artist' => 'Code 64', |
| 20 | + ], |
| 21 | + [ |
| 22 | + 'album' => 'nothingishereandneverwillbe', |
| 23 | + 'artist' => 'nothingishereandneverwillbe', |
| 24 | + ], |
| 25 | +]; |
| 26 | + |
| 27 | +foreach ($media_info as $media) { |
| 28 | + echo "Checking: " . print_r($media, true) . PHP_EOL; |
| 29 | + $results = []; |
| 30 | + try { |
| 31 | + if (isset($media['artist']) && ($media['artist'] !== '' && $media['artist'] !== '0') && !in_array('album', $media)) { |
| 32 | + $artists = $discogs->search_artist($media['artist']); |
| 33 | + if (isset($artists['results']) && count($artists['results']) > 0) { |
| 34 | + foreach ($artists['results'] as $result) { |
| 35 | + if ($result['title'] === $media['artist']) { |
| 36 | + $artist = $discogs->get_artist((int)$result['id']); |
| 37 | + if (isset($artist['images']) && count($artist['images']) > 0) { |
| 38 | + $results['art'] = $artist['images'][0]['uri']; |
| 39 | + } |
| 40 | + |
| 41 | + if (!empty($artist['cover_image'])) { |
| 42 | + $results['art'] = $artist['cover_image']; |
| 43 | + } |
| 44 | + |
| 45 | + // add in the data response as well |
| 46 | + $results['data'] = $artist; |
| 47 | + break; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if (!empty($media['albumartist']) && !empty($media['album'])) { |
| 54 | + /** |
| 55 | + * https://api.discogs.com/database/search?type=master&release_title=Ghosts&artist=Ladytron&per_page=10&key=key@secret=secret |
| 56 | + */ |
| 57 | + $albums = $discogs->search_album($media['albumartist'], $media['album']); |
| 58 | + if (empty($albums['results'])) { |
| 59 | + $albums = $discogs->search_album($media['albumartist'], $media['album'], 'release'); |
| 60 | + } |
| 61 | + |
| 62 | + // get the album that matches $artist - $album |
| 63 | + if (!empty($albums['results'])) { |
| 64 | + /** |
| 65 | + * @var array{ |
| 66 | + * country: string, |
| 67 | + * year: string, |
| 68 | + * format: string[], |
| 69 | + * label: string[], |
| 70 | + * type: string, |
| 71 | + * genre: string[], |
| 72 | + * style: string[], |
| 73 | + * id: ?int, |
| 74 | + * barcode: string[], |
| 75 | + * master_id: int, |
| 76 | + * master_url: string, |
| 77 | + * uri: string, |
| 78 | + * catno: string, |
| 79 | + * title: string, |
| 80 | + * thumb: string, |
| 81 | + * cover_image: string, |
| 82 | + * resource_url: string, |
| 83 | + * community: object, |
| 84 | + * format_quantity: ?int, |
| 85 | + * formats: ?object, |
| 86 | + * } $albumSearch |
| 87 | + */ |
| 88 | + foreach ($albums['results'] as $albumSearch) { |
| 89 | + if ($media['albumartist'] . ' - ' . $media['album'] === $albumSearch['title']) { |
| 90 | + $album = $albumSearch; |
| 91 | + break; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // look up the master release if we have one or the first release |
| 96 | + if (!isset($album['id'])) { |
| 97 | + /** |
| 98 | + * @var array{ |
| 99 | + * id: ?int, |
| 100 | + * main_release: int, |
| 101 | + * most_recent_release: int, |
| 102 | + * uri: string, |
| 103 | + * versions_uri: string, |
| 104 | + * main_release_uri: string, |
| 105 | + * most_recent_release_uri: string, |
| 106 | + * num_for_sale: int, |
| 107 | + * lowest_price: int, |
| 108 | + * images: object, |
| 109 | + * genres: string[], |
| 110 | + * styles: string[], |
| 111 | + * year: int, |
| 112 | + * tracklist: object, |
| 113 | + * artists: object, |
| 114 | + * title: string, |
| 115 | + * data-quality: string, |
| 116 | + * videos: object, |
| 117 | + * } $album |
| 118 | + */ |
| 119 | + $album = (($albums['results'][0]['master_id'] ?? 0) > 0) |
| 120 | + ? $discogs->get_album((int)$albums['results'][0]['master_id']) |
| 121 | + : $discogs->get_album((int)$albums['results'][0]['id'], 'releases'); |
| 122 | + } |
| 123 | + |
| 124 | + // fallback to the initial search if we don't have a master |
| 125 | + if (!isset($album['id'])) { |
| 126 | + $album = $albums['results'][0]; |
| 127 | + } |
| 128 | + |
| 129 | + if (isset($album['images']) && count($album['images']) > 0) { |
| 130 | + $results['art'] = $album['images'][0]['uri']; |
| 131 | + } |
| 132 | + |
| 133 | + if (!empty($album['cover_image'])) { |
| 134 | + $results['art'] = $album['cover_image']; |
| 135 | + } |
| 136 | + |
| 137 | + $genres = []; |
| 138 | + foreach ($albums['results'] as $release) { |
| 139 | + if (!empty($release['genre'])) { |
| 140 | + $genres = array_merge($genres, $release['genre']); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + if (!empty($release['style'])) { |
| 145 | + $genres = array_merge($genres, $release['style']); |
| 146 | + } |
| 147 | + |
| 148 | + if ($genres !== []) { |
| 149 | + $results['genre'] = array_unique($genres); |
| 150 | + } |
| 151 | + |
| 152 | + // add in the data response as well |
| 153 | + $results['data'] = $album; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + print_r($results); |
| 158 | + } catch (Exception $exception) { |
| 159 | + print_r($exception->getMessage()); |
| 160 | + } |
| 161 | +} |
0 commit comments