]> git.sesse.net Git - vlc/blob - share/luameta/googleimage.lua
f3d565f8af778be430ef933b83990a9d05f5d2c1
[vlc] / share / luameta / googleimage.lua
1 -- Get's an artwork from images.google.com
2 -- $Id$
3
4 -- Replace non alphanumeric char by +
5 function get_query( title )
6     -- If we have a .EXT remove the extension.
7     str = string.gsub( title, "(.*)%....$", "%1" )
8     return string.gsub( str, "([^%w ])",
9          function (c) return string.format ("%%%02X", string.byte(c)) end)
10 end
11
12 -- Return the artwork
13 function fetch_art()
14     if vlc.artist and vlc.album then
15         title = vlc.artist.." "..vlc.album
16     elseif vlc.title and vlc.artist then
17         title = vlc.artist.." "..vlc.title
18     elseif vlc.title then
19         title = vlc.title
20     elseif vlc.name then
21         title = vlc.name
22     else
23         return nil
24     end
25     fd = vlc.stream_new( "http://images.google.com/images?q=" .. get_query( title ) )
26     page = vlc.stream_read( fd, 65653 )
27     vlc.stream_delete( fd )
28     _, _, arturl = string.find( page, "imgurl=([^&]+)" )
29     return vlc.decode_uri(arturl)
30 end