end
awesompd.try_require("utf8")
+awesompd.try_require("asyncshell")
awesompd.try_require("jamendo")
local beautiful = require('beautiful')
local naughty = naughty
self.recreate_list = true
self.current_number = tonumber(self.find_pattern(status_line,"%d+"))
self:update_widget_text()
+
+ -- If the track is not the last, asynchronously download
+ -- the cover for the next track.
+ if self.list_array and self.current_number ~= table.getn(self.list_array) then
+ -- Get the link (in case it is Jamendo stream) to the next track
+ local next_track =
+ self:command_read('playlist -f "%file%" | head -' ..
+ self.current_number + 1 .. ' | tail -1', "*line")
+ jamendo.try_get_cover_async(next_track)
+ end
end
local tmp_pst = string.find(status_line,"%d+%:%d+%/")
local progress = self.find_pattern(status_line,"%#%d+/%d+") .. " " .. string.sub(status_line,tmp_pst)
local tonumber = tonumber
local math = math
local tostring = tostring
+local asyncshell = asyncshell
module('jamendo')
-- Retrieve cache on initialization
retrieve_cache()
--- Returns a file containing an album cover for given track id. First
--- searches in the cache folder. If file is not there, fetches it from
--- the Internet and saves into the cache folder.
-function get_album_cover(track_id)
+-- Returns a filename of the album cover and formed wget request that
+-- downloads the album cover for the given track name. If the album
+-- cover already exists returns nil as the second argument.
+function fetch_album_cover_request(track_id)
local track = jamendo_list[track_id]
local album_id = track.album_id
prefix, a_id)
end
- f = io.popen("wget " .. track.album_image .. " -O "
- .. file_path .. " > /dev/null")
+ return file_path, string.format("wget %s -O %s > /dev/null",
+ track.album_image, file_path)
+ else -- Cover already downloaded, return its filename and nil
+ return file_path, nil
+ end
+end
+
+-- Returns a file containing an album cover for given track id. First
+-- searches in the cache folder. If file is not there, fetches it from
+-- the Internet and saves into the cache folder.
+function get_album_cover(track_id)
+ local file_path, fetch_req = fetch_album_cover_request(track_id)
+ if fetch_req then
+ local f = io.popen(fetch_req)
f:close()
-- Let's check if file is finally there, just in case
return file_path
end
+-- Same as get_album_cover, but downloads (if necessary) the cover
+-- asynchronously.
+function get_album_cover_async(track_id)
+ local file_path, fetch_req = fetch_album_cover_request(track_id)
+ if fetch_req then
+ asyncshell.request(fetch_req)
+ end
+end
+
-- Checks if track_name is actually a link to Jamendo stream. If true
-- returns the file with album cover for the track.
function try_get_cover(track_name)
end
end
+-- Same as try_get_cover, but calls get_album_cover_async inside.
+function try_get_cover_async(track_name)
+ local id = get_id_from_link(track_name)
+ if id then
+ return get_album_cover_async(id)
+ end
+end
+
-- Returns the track table for given query and search method.
-- what - search method - SEARCH_ARTIST, ALBUM or TAG
-- s - string to search