[grilo-plugins/wip/jfelder/theaudiodb-artist-art: 4/4] theaudiobd: Add support for artist art



commit 798b21791cbaca3c049f13d55f2156e91fd227f2
Author: Jean Felder <jfelder src gnome org>
Date:   Sat Jul 27 23:18:18 2019 +0200

    theaudiobd: Add support for artist art
    
    Resolve operation is noew able to download artist arts or album cover
    arts.
    To download an album cover art, both artist and album keys have to be
    set.
    To download an artist art, only the artist key has to be set.

 src/lua-factory/sources/grl-theaudiodb-cover.lua | 44 ++++++++++++++++--------
 1 file changed, 30 insertions(+), 14 deletions(-)
---
diff --git a/src/lua-factory/sources/grl-theaudiodb-cover.lua 
b/src/lua-factory/sources/grl-theaudiodb-cover.lua
index b7127336..b8762c3f 100644
--- a/src/lua-factory/sources/grl-theaudiodb-cover.lua
+++ b/src/lua-factory/sources/grl-theaudiodb-cover.lua
@@ -27,7 +27,7 @@
 source = {
   id = "grl-theaudiodb-cover",
   name = "TheAudioDB Cover",
-  description = "a source for music covers",
+  description = "a source for album covers and artist art",
   supported_keys = { 'thumbnail' },
   supported_media = { 'audio' },
   config_keys = {
@@ -35,7 +35,7 @@ source = {
   },
   resolve_keys = {
     ["type"] = "audio",
-    required = { "artist", "album" },
+    required = { "artist" },
   },
   tags = { 'music', 'net:internet' },
 }
@@ -48,9 +48,14 @@ netopts = {
 -- Source utils --
 ------------------
 theaudiodb = {}
-covers_fields = {"strAlbumThumb", "strAlbumThumbBack", "strAlbumCDart", "strAlbumSpine"}
+covers_fields = {
+  album = {"strAlbumThumb", "strAlbumThumbBack", "strAlbumCDart", "strAlbumSpine"},
+  artists = {"strArtistThumb", "strArtistClearArt", "strArtistFanart", "strArtistFanart2", 
"strArtistFanart3"}
+}
 
-THEAUDIODB_SEARCH_ALBUM = 'https://theaudiodb.com/api/v1/json/%s/searchalbum.php?s=%s&a=%s'
+THEAUDIODB_ROOT_URL = "https://theaudiodb.com/api/v1/json/%s/";
+THEAUDIODB_SEARCH_ALBUM = THEAUDIODB_ROOT_URL .. "searchalbum.php?s=%s&a=%s"
+THEAUDIODB_SEARCH_ARTIST = THEAUDIODB_ROOT_URL .. "search.php?s=%s"
 
 ---------------------------------
 -- Handlers of Grilo functions --
@@ -61,29 +66,40 @@ function grl_source_init (configs)
   return true
 end
 
+-- Resolve operation is able to download artist arts or album cover arts.
+-- To download an album cover art, both artist and album keys have to be set.
+-- To download an artist art, only the artist key has to be set.
 function grl_source_resolve()
   local url, keys
   local artist, album
+  local search_type
 
   keys = grl.get_media_keys()
-  if not keys or not keys.artist or not keys.album
-    or #keys.artist == 0 or #keys.album == 0 then
+
+  if not keys.artist or #keys.artist == 0 then
     grl.callback()
     return
   end
 
-  -- Prepare artist and album strings to the url
+  -- Prepare artist and optional album strings to the url
   artist = grl.encode(keys.artist)
-  album = grl.encode(keys.album)
-  url = string.format(THEAUDIODB_SEARCH_ALBUM, theaudiodb.api_key, artist, album)
-  grl.fetch(url, netopts, fetch_cb)
+  if keys.album and  #keys.album > 0 then
+    search_type = "album"
+    album = grl.encode(keys.album)
+    url = string.format(THEAUDIODB_SEARCH_ALBUM, theaudiodb.api_key, artist, album)
+  else
+    search_type = "artists"
+    url = string.format(THEAUDIODB_SEARCH_ARTIST, theaudiodb.api_key, artist)
+  end
+
+  grl.fetch(url, netopts, fetch_cb, search_type)
 end
 
 ---------------
 -- Utilities --
 ---------------
 
-function fetch_cb(result)
+function fetch_cb(result, search_type)
   local json = {}
 
   if not result then
@@ -92,15 +108,15 @@ function fetch_cb(result)
   end
 
   json = grl.lua.json.string_to_table(result)
-  if not json or not json.album or #json.album == 0 then
+  if not json or not json[search_type] or #json[search_type] == 0 then
     grl.callback()
     return
   end
 
   local media = {}
   local thumb = {}
-  for _, val in ipairs(covers_fields) do
-    thumb[#thumb + 1] = json.album[1][val] or nil
+  for _, val in ipairs(covers_fields[search_type]) do
+    thumb[#thumb + 1] = json[search_type][1][val] or nil
   end
 
   media.thumbnail = thumb


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]