From 512e9f103a8af742c8df398d1788f17ee9a45801 Mon Sep 17 00:00:00 2001 From: Afterster Date: Thu, 10 Dec 2015 23:31:40 +0100 Subject: [PATCH] Add Discogs metadata/art plugin Update license information on composer plugins --- README.md | 2 +- lib/init.php | 2 +- modules/plugins/Discogs/Discogs.plugin.php | 176 ++++++++++++++++++ modules/plugins/Growl/composer.json | 2 +- .../plugins/TheAudioDb/TheAudioDb.plugin.php | 22 +-- modules/plugins/Tmdb/composer.json | 2 +- modules/plugins/Tvdb/composer.json | 2 +- 7 files changed, 192 insertions(+), 16 deletions(-) create mode 100644 modules/plugins/Discogs/Discogs.plugin.php diff --git a/README.md b/README.md index 66e2c68b..79be9fae 100755 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ License ------- Ampache is free software; you can redistribute it and/or -modify it under the terms of the GNU Affero General Public License v3 +modify it under the terms of the GNU Affero General Public License v3 (AGPLv3) as published by the Free Software Foundation. Ampache includes some external modules that carry their own licensing. diff --git a/lib/init.php b/lib/init.php index c91ebfa0..f1dc6a61 100644 --- a/lib/init.php +++ b/lib/init.php @@ -67,7 +67,7 @@ if (!empty($link)) { $results['load_time_begin'] = $load_time_begin; /** This is the version.... fluf nothing more... **/ -$results['version'] = '3.8.1'; +$results['version'] = '3.8.2-develop'; $results['int_config_version'] = '32'; if (!empty($results['force_ssl'])) { diff --git a/modules/plugins/Discogs/Discogs.plugin.php b/modules/plugins/Discogs/Discogs.plugin.php new file mode 100644 index 00000000..7a9382e4 --- /dev/null +++ b/modules/plugins/Discogs/Discogs.plugin.php @@ -0,0 +1,176 @@ +. + * + */ + +class AmpacheDiscogs +{ + public $name = 'Discogs'; + public $categories = 'metadata'; + public $description = 'Discogs metadata integration'; + public $url = 'http://www.discogs.com'; + public $version = '000001'; + public $min_ampache = '370021'; + public $max_ampache = '999999'; + + private $api_key; + private $secret; + + /** + * Constructor + * This function does nothing + */ + public function __construct() + { + return true; + } + + /** + * install + * This is a required plugin function + */ + public function install() + { + if (Preference::exists('discogs_api_key')) { + return false; + } + Preference::insert('discogs_api_key','Discogs consumer key','','75','string','plugins'); + Preference::insert('discogs_secret_api_key','Discogs secret','','75','string','plugins'); + return true; + } // install + + /** + * uninstall + * This is a required plugin function + */ + public function uninstall() + { + Preference::delete('discogs_api_key'); + Preference::delete('discogs_secret_api_key'); + return true; + } // uninstall + + /** + * load + * This is a required plugin function; here it populates the prefs we + * need for this object. + */ + public function load($user) + { + $user->set_preferences(); + $data = $user->prefs; + + if (strlen(trim($data['discogs_api_key']))) { + $this->api_key = trim($data['discogs_api_key']); + } else { + debug_event($this->name,'No Discogs api key, metadata plugin skipped','3'); + return false; + } + if (strlen(trim($data['discogs_secret_api_key']))) { + $this->secret = trim($data['discogs_secret_api_key']); + } else { + debug_event($this->name,'No Discogs secret, metadata plugin skipped','3'); + return false; + } + + return true; + } // load + + protected function query_discogs($query) + { + $url = 'https://api.discogs.com/' . $query; + $url .= (strpos($query, '?') !== false) ? '&' : '?'; + $url .= 'key=' . $this->api_key . '&secret=' . $this->secret; + debug_event('discogs', 'Discogs request: ' . $url, 5); + $request = Requests::get($url); + return json_decode($request->body, true); + } + + protected function search_artist($artist) + { + $query = "database/search?type=artist&title=" . rawurlencode($artist) . "&per_page=10"; + return $this->query_discogs($query); + } + + protected function get_artist($id) + { + $query = "artists/" . $id; + return $this->query_discogs($query); + } + + protected function search_album($artist, $album) + { + $query = "database/search?type=master&release_title=" . rawurlencode($album) . "&artist=" . rawurlencode($artist) . "&per_page=10"; + return $this->query_discogs($query); + } + + protected function get_album($id) + { + $query = "masters/" . $id; + return $this->query_discogs($query); + } + + /** + * get_metadata + * Returns song metadata for what we're passed in. + */ + public function get_metadata($gather_types, $media_info) + { + debug_event('discogs', 'Getting metadata from Discogs...', '5'); + + // TVShow and Movie metadata only + if (!in_array('music', $gather_types)) { + debug_event('discogs', 'Not a valid media type, skipped.', '5'); + return null; + } + + $results = array(); + try { + if (in_array('artist', $gather_types)) { + $artists = $this->search_artist($media_info['title']); + if (count($artists['results']) > 0) { + $artist = $this->get_artist($artists['results'][0]['id']); + if (count($artist['images']) > 0) { + $results['art'] = $artist['images'][0]['uri']; + } + } + } else { + if (in_array('album', $gather_types)) { + $albums = $this->search_album($media_info['artist'], $media_info['title']); + if (count($albums['results']) > 0) { + $album = $this->get_album($albums['results'][0]['id']); + if (count($album['images']) > 0) { + $results['art'] = $album['images'][0]['uri']; + } + } + } + } + } catch (Exception $e) { + debug_event('discogs', 'Error getting metadata: ' . $e->getMessage(), '1'); + } + + return $results; + } // get_metadata + + public function gather_arts($type, $options = array(), $limit = 5) + { + return Art::gather_metadata_plugin($this, $type, $options); + } +} diff --git a/modules/plugins/Growl/composer.json b/modules/plugins/Growl/composer.json index 0199309b..c80ab525 100644 --- a/modules/plugins/Growl/composer.json +++ b/modules/plugins/Growl/composer.json @@ -4,7 +4,7 @@ "homepage": "http://ampache.org", "keywords": ["ampache", "growl", "plugin"], "type": "ampache-plugin", - "license": "GPL-2.0", + "license": "AGPL-3.0", "require": { "jamiebicknell/Growl-GNTP": "1.*" }, diff --git a/modules/plugins/TheAudioDb/TheAudioDb.plugin.php b/modules/plugins/TheAudioDb/TheAudioDb.plugin.php index 4c61d463..297da2e3 100644 --- a/modules/plugins/TheAudioDb/TheAudioDb.plugin.php +++ b/modules/plugins/TheAudioDb/TheAudioDb.plugin.php @@ -105,17 +105,7 @@ class AmpacheTheaudiodb } try { - if ($media_info['mb_trackid'] && in_array('song', $gather_types)) { - $track = $this->get_track($media_info['mb_trackid']); - if ($track) { - $track = $track->track[0]; - $results['mb_artistid'] = $track->strMusicBrainzArtistID; - $results['mb_albumid_group'] = $track->strMusicBrainzAlbumID; - $results['album'] = $track->strAlbum; - $results['artist'] = $track->strArtist; - $results['title'] = $track->strTrack; - } - } elseif (in_array('album', $gather_types)) { + if (in_array('album', $gather_types)) { $release = null; if ($media_info['mb_albumid_group']) { $album = $this->get_album($media_info['mb_albumid_group']); @@ -153,6 +143,16 @@ class AmpacheTheaudiodb $results['summary'] = $release->strBiographyEN; $results['yearformed'] = $release->intFormedYear; } + } elseif ($media_info['mb_trackid']) { + $track = $this->get_track($media_info['mb_trackid']); + if ($track) { + $track = $track->track[0]; + $results['mb_artistid'] = $track->strMusicBrainzArtistID; + $results['mb_albumid_group'] = $track->strMusicBrainzAlbumID; + $results['album'] = $track->strAlbum; + $results['artist'] = $track->strArtist; + $results['title'] = $track->strTrack; + } } } catch (Exception $e) { debug_event('tadb', 'Error getting metadata: ' . $e->getMessage(), '1'); diff --git a/modules/plugins/Tmdb/composer.json b/modules/plugins/Tmdb/composer.json index 23e77a45..6250b8d8 100644 --- a/modules/plugins/Tmdb/composer.json +++ b/modules/plugins/Tmdb/composer.json @@ -4,7 +4,7 @@ "homepage": "http://ampache.org", "keywords": ["ampache", "tmdb", "plugin"], "type": "ampache-plugin", - "license": "GPL-2.0", + "license": "AGPL-3.0", "require": { "php-tmdb/api": "2.*" } diff --git a/modules/plugins/Tvdb/composer.json b/modules/plugins/Tvdb/composer.json index 1970b2ba..2eaac002 100644 --- a/modules/plugins/Tvdb/composer.json +++ b/modules/plugins/Tvdb/composer.json @@ -4,7 +4,7 @@ "homepage": "http://ampache.org", "keywords": ["ampache", "tvdb", "plugin"], "type": "ampache-plugin", - "license": "GPL-2.0", + "license": "AGPL-3.0", "require": { "moinax/tvdb": "1.*" }