PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/plugins/TheAudioDb.plugin.php

https://gitlab.com/x33n/ampache
PHP | 209 lines | 133 code | 31 blank | 45 comment | 19 complexity | 69bf2647ffcb09bbed6cb3b7ef58ca0a MD5 | raw file
  1. <?php
  2. /* vim:set softtabstop=4 shiftwidth=4 expandtab: */
  3. /**
  4. *
  5. * LICENSE: GNU General Public License, version 2 (GPLv2)
  6. * Copyright 2001 - 2015 Ampache.org
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License v2
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. *
  21. */
  22. class AmpacheTheaudiodb {
  23. public $name = 'TheAudioDb';
  24. public $categories = 'metadata';
  25. public $description = 'TheAudioDb metadata integration';
  26. public $url = 'http://www.theaudiodb.com';
  27. public $version = '000001';
  28. public $min_ampache = '370009';
  29. public $max_ampache = '999999';
  30. // These are internal settings used by this class, run this->load to
  31. // fill them out
  32. private $api_key;
  33. /**
  34. * Constructor
  35. * This function does nothing
  36. */
  37. public function __construct() {
  38. return true;
  39. }
  40. /**
  41. * install
  42. * This is a required plugin function
  43. */
  44. public function install() {
  45. if (Preference::exists('tadb_api_key')) { return false; }
  46. // API Key requested in TheAudioDB forum, see http://www.theaudiodb.com/forum/viewtopic.php?f=6&t=8&start=140
  47. Preference::insert('tadb_api_key','TheAudioDb api key','41214789306c4690752dfb','75','string','plugins');
  48. return true;
  49. } // install
  50. /**
  51. * uninstall
  52. * This is a required plugin function
  53. */
  54. public function uninstall() {
  55. Preference::delete('tadb_api_key');
  56. return true;
  57. } // uninstall
  58. /**
  59. * load
  60. * This is a required plugin function; here it populates the prefs we
  61. * need for this object.
  62. */
  63. public function load($user) {
  64. $user->set_preferences();
  65. $data = $user->prefs;
  66. if (strlen(trim($data['tadb_api_key']))) {
  67. $this->api_key = trim($data['tadb_api_key']);
  68. }
  69. else {
  70. debug_event($this->name,'No TheAudioDb api key, metadata plugin skipped','3');
  71. return false;
  72. }
  73. return true;
  74. } // load
  75. /**
  76. * get_metadata
  77. * Returns song metadata for what we're passed in.
  78. */
  79. public function get_metadata($gather_types, $media_info) {
  80. debug_event('tadb', 'Getting metadata from TheAudioDb...', '5');
  81. // Music metadata only
  82. if (!in_array('music', $gather_types)) {
  83. debug_event('tadb', 'Not a valid media type, skipped.', '5');
  84. return null;
  85. }
  86. try {
  87. if ($media_info['mb_trackid'] && in_array('song', $gather_types)) {
  88. $track = $this->get_track($media_info['mb_trackid']);
  89. if ($track) {
  90. $track = $track->track[0];
  91. $results['mb_artistid'] = $track->strMusicBrainzArtistID;
  92. $results['mb_albumid_group'] = $track->strMusicBrainzAlbumID;
  93. $results['album'] = $track->strAlbum;
  94. $results['artist'] = $track->strArtist;
  95. $results['title'] = $track->strTrack;
  96. }
  97. } elseif (in_array('album', $gather_types)) {
  98. $release = null;
  99. if ($media_info['mb_albumid_group']) {
  100. $album = $this->get_album($media_info['mb_albumid_group']);
  101. if ($album) {
  102. $release = $album->album[0];
  103. }
  104. } else {
  105. $albums = $this->search_album($media_info['artist'], $media_info['title']);
  106. if ($albums) {
  107. $release = $albums->album[0];
  108. }
  109. }
  110. if ($release) {
  111. $results['art'] = $release->strAlbumThumb;
  112. $results['title'] = $release->strAlbum;
  113. }
  114. } elseif (in_array('artist', $gather_types)) {
  115. $release = null;
  116. if ($media_info['mb_artistid']) {
  117. $artist = $this->get_artist($media_info['mb_artistid']);
  118. if ($artist) {
  119. $release = $artist->artist[0];
  120. }
  121. } else {
  122. $artists = $this->search_artists($media_info['title']);
  123. if ($artists) {
  124. $release = $artists->artist[0];
  125. }
  126. }
  127. if ($release) {
  128. $results['art'] = $release->strArtistThumb;
  129. $results['title'] = $release->strArtist;
  130. $results['summary'] = $release->strBiographyEN;
  131. $results['yearformed'] = $release->intFormedYear;
  132. }
  133. }
  134. } catch (Exception $e) {
  135. debug_event('tadb', 'Error getting metadata: ' . $e->getMessage(), '1');
  136. }
  137. return $results;
  138. } // get_metadata
  139. public function gather_arts($type, $options = array(), $limit = 5)
  140. {
  141. debug_event('tadb', 'gather_arts for type `' . $type . '`', 5);
  142. return Art::gather_metadata_plugin($this, $type, $options);
  143. }
  144. private function api_call($func)
  145. {
  146. $url = 'http://www.theaudiodb.com/api/v1/json/' . $this->api_key . '/' . $func;
  147. debug_event('tadb', 'API call: ' . $url, 5);
  148. $request = Requests::get($url, array(), Core::requests_options());
  149. if ($request->status_code != 200)
  150. return null;
  151. return json_decode($request->body);
  152. }
  153. private function search_artists($name)
  154. {
  155. return $this->api_call('search.php?s=' . rawurlencode($name));
  156. }
  157. private function get_artist($mbid)
  158. {
  159. return $this->api_call('artist-mb.php?i=' . $mbid);
  160. }
  161. private function search_album($artist, $album)
  162. {
  163. return $this->api_call('searchalbum.php?s=' . rawurlencode($artist) . '&a=' . rawurlencode($album));
  164. }
  165. private function get_album($mbid)
  166. {
  167. return $this->api_call('album-mb.php?i=' . $mbid);
  168. }
  169. private function search_track($artist, $title)
  170. {
  171. return $this->api_call('searchtrack.php?s=' . rawurlencode($artist) . '&t=' . rawurlencode($title));
  172. }
  173. private function get_track($mbid)
  174. {
  175. return $this->api_call('track-mb.php?i=' . $mbid);
  176. }
  177. } // end AmpacheTheaudiodb
  178. ?>