-
Notifications
You must be signed in to change notification settings - Fork 604
Expand file tree
/
Copy pathAmpacheDiscogs.php
More file actions
295 lines (253 loc) · 10.7 KB
/
AmpacheDiscogs.php
File metadata and controls
295 lines (253 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
declare(strict_types=0);
/**
* vim:set softtabstop=4 shiftwidth=4 expandtab:
*
* LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
* Copyright Ampache.org, 2001-2026
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Ampache\Plugin;
use AmpacheDiscogs\Discogs;
use Ampache\Module\Authorization\AccessLevelEnum;
use Ampache\Repository\Model\Art;
use Ampache\Repository\Model\Preference;
use Ampache\Repository\Model\User;
use Exception;
class AmpacheDiscogs extends AmpachePlugin implements PluginGatherArtsInterface, PluginGetMetadataInterface
{
public string $name = 'Discogs';
public string $categories = 'metadata';
public string $description = 'Discogs metadata integration';
public string $url = 'http://www.discogs.com';
public string $version = '000001';
public string $min_ampache = '370021';
public string $max_ampache = '999999';
// These are internal settings used by this class, run this->load to fill them out
private Discogs $discogs;
/**
* Constructor
* This function does nothing
*/
public function __construct()
{
$this->description = T_('Discogs metadata integration');
}
/**
* install
* This is a required plugin function
*/
public function install(): bool
{
if (!Preference::insert('discogs_api_key', T_('Discogs consumer key'), '', AccessLevelEnum::MANAGER->value, 'string', 'plugins', $this->name)) {
return false;
}
return Preference::insert('discogs_secret_api_key', T_('Discogs secret'), '', AccessLevelEnum::MANAGER->value, 'string', 'plugins', $this->name);
}
/**
* uninstall
* This is a required plugin function
*/
public function uninstall(): bool
{
return (
Preference::delete('discogs_api_key') &&
Preference::delete('discogs_secret_api_key')
);
}
/**
* upgrade
* This is a recommended plugin function
*/
public function upgrade(): bool
{
return true;
}
/**
* load
* This is a required plugin function; here it populates the prefs we
* need for this object.
*/
public function load(User $user): bool
{
$user->set_preferences();
$data = $user->prefs;
// load system when nothing is given
if (!strlen(trim((string) $data['discogs_api_key'])) || !strlen(trim((string) $data['discogs_secret_api_key']))) {
$data = [];
$data['discogs_api_key'] = Preference::get_by_user(-1, 'discogs_api_key');
$data['discogs_secret_api_key'] = Preference::get_by_user(-1, 'discogs_secret_api_key');
}
if (strlen(trim((string) $data['discogs_api_key'])) !== 0) {
$api_key = trim((string) $data['discogs_api_key']);
} else {
debug_event(self::class, 'No Discogs api key, metadata plugin skipped', 3);
return false;
}
if (strlen(trim((string) $data['discogs_secret_api_key'])) !== 0) {
$secret = trim((string) $data['discogs_secret_api_key']);
} else {
debug_event(self::class, 'No Discogs secret, metadata plugin skipped', 3);
return false;
}
$this->discogs = new Discogs($api_key, $secret);
return true;
}
/**
* get_metadata
* Returns song metadata for what we're passed in.
* @param string[] $gather_types
* @param array<string, mixed> $media_info
* @return array<string, mixed>
*/
public function get_metadata(array $gather_types, array $media_info): array
{
debug_event(self::class, 'Getting metadata from Discogs...', 5);
// MUSIC metadata only
if (!in_array('music', $gather_types)) {
debug_event(self::class, 'Not a valid media type, skipped.', 5);
return [];
}
$results = [];
try {
if (!empty($media_info['artist']) && !in_array('album', $media_info)) {
$artists = $this->discogs->search_artist($media_info['artist']);
if (isset($artists['results']) && count($artists['results']) > 0) {
foreach ($artists['results'] as $result) {
if ($result['title'] === $media_info['artist']) {
$artist = $this->discogs->get_artist((int)$result['id']);
if (isset($artist['images']) && count($artist['images']) > 0) {
$results['art'] = $artist['images'][0]['uri'];
}
if (!empty($artist['cover_image'])) {
$results['art'] = $artist['cover_image'];
}
// add in the data response as well
$results['data'] = $artist;
break;
}
}
}
}
if (!empty($media_info['albumartist']) && !empty($media_info['album'])) {
/**
* https://api.discogs.com/database/search?type=master&release_title=Ghosts&artist=Ladytron&per_page=10&key=key@secret=secret
*/
$albums = $this->discogs->search_album($media_info['albumartist'], $media_info['album']);
if (empty($albums['results'])) {
$albums = $this->discogs->search_album($media_info['albumartist'], $media_info['album'], 'release');
}
// get the album that matches $artist - $album
if (!empty($albums['results'])) {
/**
* @var array{
* country: string,
* year: string,
* format: string[],
* label: string[],
* type: string,
* genre: string[],
* style: string[],
* id: ?int,
* barcode: string[],
* master_id: int,
* master_url: string,
* uri: string,
* catno: string,
* title: string,
* thumb: string,
* cover_image: string,
* resource_url: string,
* community: object,
* format_quantity: ?int,
* formats: ?object,
* } $albumSearch
*/
foreach ($albums['results'] as $albumSearch) {
if ($media_info['albumartist'] . ' - ' . $media_info['album'] === $albumSearch['title']) {
$album = $albumSearch;
break;
}
}
// look up the master release if we have one or the first release
if (!isset($album['id'])) {
/**
* @var array{
* id: ?int,
* main_release: int,
* most_recent_release: int,
* uri: string,
* versions_uri: string,
* main_release_uri: string,
* most_recent_release_uri: string,
* num_for_sale: int,
* lowest_price: int,
* images: object,
* genres: string[],
* styles: string[],
* year: int,
* tracklist: object,
* artists: object,
* title: string,
* data-quality: string,
* videos: object,
* } $album
*/
$album = (($albums['results'][0]['master_id'] ?? 0) > 0)
? $this->discogs->get_album((int)$albums['results'][0]['master_id'])
: $this->discogs->get_album((int)$albums['results'][0]['id'], 'releases');
}
// fallback to the initial search if we don't have a master
if (!isset($album['id'])) {
$album = $albums['results'][0];
}
if (isset($album['images']) && count($album['images']) > 0) {
$results['art'] = $album['images'][0]['uri'];
}
if (!empty($album['cover_image'])) {
$results['art'] = $album['cover_image'];
}
$genres = [];
foreach ($albums['results'] as $release) {
if (!empty($release['genre'])) {
$genres = array_merge($genres, $release['genre']);
}
}
if (!empty($release['style'])) {
$genres = array_merge($genres, $release['style']);
}
if ($genres !== []) {
$results['genre'] = array_unique($genres);
}
// add in the data response as well
$results['data'] = $album;
}
}
} catch (Exception $exception) {
debug_event(self::class, 'Error getting metadata: ' . $exception->getMessage(), 1);
}
return $results;
}
/**
* gather_arts
* Returns art items for the requested media type
* @return array<array{url: string, mime: string, title: string}>
*/
public function gather_arts(string $type, ?array $options = [], ?int $limit = 5): array
{
return array_slice(Art::gather_metadata_plugin($this, $type, ($options ?? [])), 0, $limit);
}
}