|
1 | | -# AmpacheDiscogs-PHP |
| 1 | +# php-discogs-api (AmpacheDiscogs) |
2 | 2 |
|
3 | | -Simple Discogs query library exported from Ampache |
| 3 | +This library is a simple Discogs query library exported from the [Ampache Discogs plugin](https://github.com/ampache/ampache/blob/develop/src/Plugin/AmpacheDiscogs.php). |
| 4 | + |
| 5 | +The focus here is on keeping it small and simple. |
| 6 | + |
| 7 | +All data is JSON decoded with objects converted into associative arrays. |
| 8 | + |
| 9 | +## Requirements |
| 10 | + |
| 11 | +* PHP8.2+ |
| 12 | +* rmccue/requests |
| 13 | + |
| 14 | +## Usage Example |
| 15 | + |
| 16 | +```php |
| 17 | +<?php |
| 18 | + |
| 19 | +use AmpacheDiscogs\Discogs; |
| 20 | + |
| 21 | +require dirname(__DIR__) . '/vendor/autoload.php'; |
| 22 | + |
| 23 | +$media = [ |
| 24 | + [ |
| 25 | + 'album' => 'The Shape', |
| 26 | + 'albumartist' => 'Code 64', |
| 27 | + ], |
| 28 | +]; |
| 29 | + |
| 30 | +echo "Checking: " . print_r($media, true) . PHP_EOL; |
| 31 | +try { |
| 32 | + // your own username and password are required to use the Discogs API |
| 33 | + $username = 'username'; |
| 34 | + $password = 'password'; |
| 35 | + $discogs = new Discogs($username, $password); |
| 36 | + |
| 37 | + /** |
| 38 | + * https://api.discogs.com/database/search?type=master&release_title=The+Shape&artist=Code+64&per_page=10&key=key@secret=secret |
| 39 | + */ |
| 40 | + $albums = $discogs->search_album($media['albumartist'], $media['album']); |
| 41 | + if (empty($albums['results'])) { |
| 42 | + $albums = $discogs->search_album($media['albumartist'], $media['album'], 'release'); |
| 43 | + } |
| 44 | + |
| 45 | + // get the album that matches $artist - $album |
| 46 | + if (!empty($albums['results'])) { |
| 47 | + foreach ($albums['results'] as $albumSearch) { |
| 48 | + if ($media['albumartist'] . ' - ' . $media['album'] === $albumSearch['title']) { |
| 49 | + /** |
| 50 | + * @var array{ |
| 51 | + * country: string, |
| 52 | + * year: string, |
| 53 | + * format: string[], |
| 54 | + * label: string[], |
| 55 | + * type: string, |
| 56 | + * genre: string[], |
| 57 | + * style: string[], |
| 58 | + * id: ?int, |
| 59 | + * barcode: string[], |
| 60 | + * master_id: int, |
| 61 | + * master_url: string, |
| 62 | + * uri: string, |
| 63 | + * catno: string, |
| 64 | + * title: string, |
| 65 | + * thumb: string, |
| 66 | + * cover_image: string, |
| 67 | + * resource_url: string, |
| 68 | + * community: object, |
| 69 | + * format_quantity: ?int, |
| 70 | + * formats: ?object, |
| 71 | + * } $albumSearch |
| 72 | + */ |
| 73 | + $album = $albumSearch; |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // look up the master release if we have one or the first release |
| 79 | + if (!isset($album['id'])) { |
| 80 | + /** |
| 81 | + * @var array{ |
| 82 | + * id: ?int, |
| 83 | + * main_release: int, |
| 84 | + * most_recent_release: int, |
| 85 | + * uri: string, |
| 86 | + * versions_uri: string, |
| 87 | + * main_release_uri: string, |
| 88 | + * most_recent_release_uri: string, |
| 89 | + * num_for_sale: int, |
| 90 | + * lowest_price: int, |
| 91 | + * images: object, |
| 92 | + * genres: string[], |
| 93 | + * styles: string[], |
| 94 | + * year: int, |
| 95 | + * tracklist: object, |
| 96 | + * artists: object, |
| 97 | + * title: string, |
| 98 | + * data-quality: string, |
| 99 | + * videos: object, |
| 100 | + * } $album |
| 101 | + */ |
| 102 | + $album = (($albums['results'][0]['master_id'] ?? 0) > 0) |
| 103 | + ? $discogs->get_album((int)$albums['results'][0]['master_id']) |
| 104 | + : $discogs->get_album((int)$albums['results'][0]['id'], 'releases'); |
| 105 | + } |
| 106 | + |
| 107 | + // fallback to the initial search if we don't have a master |
| 108 | + if (!isset($album['id'])) { |
| 109 | + $album = $albums['results'][0]; |
| 110 | + } |
| 111 | + |
| 112 | + print_r($album); |
| 113 | + } |
| 114 | + |
| 115 | +} catch (Exception $exception) { |
| 116 | + print_r($exception->getMessage()); |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +Look in the [/examples](https://github.com/ampache/php-discogs-api/tree/master/examples) folder for more. |
0 commit comments