MusicBrainz: don't lookup front art when not available
[vlc.git] / share / lua / meta / art / 00_musicbrainz.lua
blob262ca353f28073d3d643fcb6fe8749b82a22f518
1 --[[
2 Gets an artwork from the Cover Art Archive or Amazon
4 $Id$
5 Copyright © 2007-2010 the VideoLAN team
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
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.
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20 --]]
22 function descriptor()
23 return { scope="network" }
24 end
26 function try_query(mbid)
27 local relquery = "http://mb.videolan.org/ws/2/release/" .. mbid
28 local s = vlc.stream( relquery )
29 if not s then return nil end
30 local page = s:read( 65653 )
32 found, _ = string.find( page, "<artwork>true</artwork>" )
33 if found then
34 front, _ = string.find( page, "<front>true</front>" )
35 if front then
36 return "http://coverartarchive.org/release/"..mbid.."/front-500"
37 end
38 end
40 -- FIXME: multiple results may be available
41 _, _, asin = string.find( page, "<asin>(%w+)</asin>" )
42 if asin then
43 return "http://images.amazon.com/images/P/"..asin..".01._SCLZZZZZZZ_.jpg"
44 end
45 vlc.msg.dbg("Neither coverartarchive.org nor amazon have cover art for this release")
46 return nil
47 end
49 -- Return the mbid for the first release returned by the MusicBrainz search server for query
50 function get_releaseid(query)
51 local s = vlc.stream( query )
52 if not s then return nil end
53 local page = s:read( 65653 )
55 -- FIXME: multiple results may be available and the first one is not
56 -- guaranteed to have asin, so if it doesnt, we wouldnt get any art
57 _, _, releaseid = string.find( page, "<release id=\"([%x%-]-)\"" )
58 if releaseid then
59 return releaseid
60 end
61 return nil
62 end
64 -- Return the artwork
65 function fetch_art()
66 local meta = vlc.item:metas()
68 if meta["Listing Type"] == "radio"
69 or meta["Listing Type"] == "tv"
70 then return nil end
72 local releaseid = nil
74 if meta["MB_ALBUMID"] then
75 releaseid = meta["MB_ALBUMID"]
76 end
78 if not releaseid and meta["artist"] and meta["album"] then
79 query = "artist:\"" .. meta["artist"] .. "\" AND release:\"" .. meta["album"] .. "\""
80 relquery = "http://mb.videolan.org/ws/2/release/?query=" .. vlc.strings.encode_uri_component( query )
81 releaseid = get_releaseid( relquery )
82 end
83 if not releaseid and meta["artist"] and meta["title"] then
84 query = "artist:\"" .. meta["artist"] .. "\" AND recording:\"" .. meta["title"] .. "\""
85 recquery = "http://mb.videolan.org/ws/2/recording/?query=" .. vlc.strings.encode_uri_component( query )
86 releaseid = get_releaseid( recquery )
87 end
88 if releaseid then
89 return try_query( releaseid )
90 else
91 return nil
92 end
93 end