[grilo-plugins/wip/jfelder/musicbrainz-release-group-coverart: 6/6] musicbrainz: Support release-group cover art



commit 05bdbc8d7159cf1866439c76e50e3ff70ec5ba09
Author: Jean Felder <jfelder src gnome org>
Date:   Fri Jul 27 16:13:47 2018 +0200

    musicbrainz: Support release-group cover art
    
    MusicBrainz API supports both release and release-group cover art
    retrieval.
    See https://musicbrainz.org/doc/Cover_Art_Archive/API
    
    First, try to get the cover art associated with the release
    id (mb_album_id key). If this cover art does not exist, try the
    release-group one (mb_release_group_id key). If none of them exist,
    return nothing.
    
    Closes: #9

 src/lua-factory/sources/grl-musicbrainz.lua | 77 +++++++++++++++++++++++------
 1 file changed, 63 insertions(+), 14 deletions(-)
---
diff --git a/src/lua-factory/sources/grl-musicbrainz.lua b/src/lua-factory/sources/grl-musicbrainz.lua
index da93f38..566e025 100644
--- a/src/lua-factory/sources/grl-musicbrainz.lua
+++ b/src/lua-factory/sources/grl-musicbrainz.lua
@@ -37,32 +37,81 @@ source = {
   tags = { 'music', 'net:internet' },
 }
 
-MUSICBRAINZ_DEFAULT_QUERY = "http://coverartarchive.org/release/%s/front";
+netopts = {
+  user_agent = "Grilo Source Musicbrainz/0.3.8",
+}
+
+MUSICBRAINZ_DEFAULT_QUERY = "https://coverartarchive.org/%s/%s";
+MUSICBRAINZ_RELEASES = {
+  {name = "release", id = "mb_release_id"},
+  {name = "release-group", id = "mb_release_group_id"}
+}
 
 ---------------------------------
 -- Handlers of Grilo functions --
 ---------------------------------
 
 function grl_source_resolve()
-  local url, req
-  local id
-
   req = grl.get_media_keys()
-  id = req.mb_album_id
-  -- FIXME add more checks on MB ID too
-  if not req or not id or #id == 0 then
+  if not req then
+    grl.callback()
+    return
+  end
+
+  -- try to get the cover art associated with the mb_album_id
+  -- if it does not exist, try the mb_release_group_id one
+  -- if none of them exist, return nothing.
+  local urls = {}
+  for _, release in ipairs(MUSICBRAINZ_RELEASES) do
+    id = req[release.id]
+    if id and #id > 0 then
+      urls[#urls + 1] = string.format(MUSICBRAINZ_DEFAULT_QUERY, release.name, id)
+    end
+  end
+
+  grl.fetch(urls, fetch_results_cb, netopts)
+end
+
+---------------
+-- Utilities --
+---------------
+
+function fetch_results_cb(results)
+  local json_results = nil
+
+  for index, feed in ipairs(results) do
+    local json = grl.lua.json.string_to_table (feed)
+    if json and json.images then
+      json_results = json.images
+      break
+    end
+  end
+
+  if not json_results then
     grl.callback()
     return
   end
 
-  -- Prepare artist and title strings to the url
-  media = {}
+  media = build_media(json_results)
+  grl.callback(media)
+end
+
+function build_media(results)
+  local media = {}
+  local res = {}
 
-  res = {}
-  res[#res + 1] = string.format(MUSICBRAINZ_DEFAULT_QUERY, id)
-  res[#res + 1] = string.format(MUSICBRAINZ_DEFAULT_QUERY .. '-500', id)
-  res[#res + 1] = string.format(MUSICBRAINZ_DEFAULT_QUERY .. '-250', id)
+  if results and #results > 0 then
+    local result = results[1]
+    -- force urls to https
+    res[1] = result.image and result.image:gsub("http://";, "https://";) or nil
+
+    if result.thumbnails then
+      for _, url in pairs(result.thumbnails) do
+        res[#res + 1] = url and url:gsub("http://";, "https://";) or nil
+      end
+    end
+  end
 
   media.thumbnail = res
-  grl.callback(media, 0)
+  return media
 end


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