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



commit 612c2900d16404ade284073edffa4c3f5ac0048b
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 | 68 ++++++++++++++++++++++++-----
 1 file changed, 56 insertions(+), 12 deletions(-)
---
diff --git a/src/lua-factory/sources/grl-musicbrainz.lua b/src/lua-factory/sources/grl-musicbrainz.lua
index da93f38..2b9cf64 100644
--- a/src/lua-factory/sources/grl-musicbrainz.lua
+++ b/src/lua-factory/sources/grl-musicbrainz.lua
@@ -34,35 +34,79 @@ source = {
     ["type"] = "audio",
     required = { "mb-album-id" },
   },
-  tags = { 'music', 'net:internet' },
+  tags = { 'music', 'net:plaintext' },
 }
 
-MUSICBRAINZ_DEFAULT_QUERY = "http://coverartarchive.org/release/%s/front";
+netopts = {
+  user_agent = "Grilo Source Musicbrainz/0.3.8",
+}
+
+MUSICBRAINZ_DEFAULT_QUERY = "http://coverartarchive.org/%s/%s";
+MUSICBRAINZ_RELEASES = {
+   release = "mb_album_id",
+   ["release-group"] = "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
+  if not req then
+     grl.callback()
+  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.
+  test_thumbnail_url(nil)
+end
+
+---------------
+-- Utilities --
+---------------
+
+function test_thumbnail_url(previous_release)
+  local release, key, id
+  local url
+
+  release, key = next(MUSICBRAINZ_RELEASES, previous_release)
+  id = req[key]
+  -- no more url to test, exit
   -- FIXME add more checks on MB ID too
-  if not req or not id or #id == 0 then
+  if not release or not id or #id == 0 then
     grl.callback()
     return
   end
 
-  -- Prepare artist and title strings to the url
+  -- try the next candidate
+  url = string.format(MUSICBRAINZ_DEFAULT_QUERY, release, id)
+  grl.fetch(url, netopts, test_thumbnail_url_cb, release)
+end
+
+function test_thumbnail_url_cb(result, current_release)
+  -- url retrieval failed, try the next candidate
+  if not result or result == "" then
+    test_thumbnail_url(current_release)
+    return
+  end
+
+  -- valid url found, generate the thumbnail
+  media = build_media(current_release)
+  grl.callback(media)
+end
+
+function build_media(release)
+  local id = req[MUSICBRAINZ_RELEASES[release]]
   media = {}
 
   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)
+  res[#res + 1] = string.format(MUSICBRAINZ_DEFAULT_QUERY, release, id)
+  res[#res + 1] = string.format(MUSICBRAINZ_DEFAULT_QUERY .. '-500', release, id)
+  res[#res + 1] = string.format(MUSICBRAINZ_DEFAULT_QUERY .. '-250', release, id)
 
   media.thumbnail = res
-  grl.callback(media, 0)
+
+  return media
 end


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