aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Yakushev <yakushev.alex@gmail.com>2012-01-03 15:40:42 +0200
committerAlexander Yakushev <yakushev.alex@gmail.com>2012-01-03 15:40:42 +0200
commit45dbf9edbdc728e6835a473fff08c62829961de2 (patch)
tree176df9e4db2cc6097729c7dd76a61219e4888cf1
parentd5363cbbc1a05881bed2490904bae5953abc5869 (diff)
downloadawesompd-45dbf9edbdc728e6835a473fff08c62829961de2.tar.gz
awesompd-45dbf9edbdc728e6835a473fff08c62829961de2.tar.bz2
Asynchronous download album cover for the next track
When the current track changes and if the next track is a Jamendo stream, asynchronously download the album cover for it (if it is not already there) to avoid freezes because of the slow connection.
-rw-r--r--awesompd.lua11
-rw-r--r--jamendo.lua42
2 files changed, 47 insertions, 6 deletions
diff --git a/awesompd.lua b/awesompd.lua
index d88f8d1..10c45f3 100644
--- a/awesompd.lua
+++ b/awesompd.lua
@@ -25,6 +25,7 @@ function awesompd.try_require(module)
end
awesompd.try_require("utf8")
+awesompd.try_require("asyncshell")
awesompd.try_require("jamendo")
local beautiful = require('beautiful')
local naughty = naughty
@@ -940,6 +941,16 @@ function awesompd:update_track(file)
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)
diff --git a/jamendo.lua b/jamendo.lua
index 3f985c4..164a7b8 100644
--- a/jamendo.lua
+++ b/jamendo.lua
@@ -17,6 +17,7 @@ local print = print
local tonumber = tonumber
local math = math
local tostring = tostring
+local asyncshell = asyncshell
module('jamendo')
@@ -388,10 +389,10 @@ end
-- 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
@@ -417,8 +418,20 @@ function get_album_cover(track_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
@@ -429,6 +442,15 @@ function get_album_cover(track_id)
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)
@@ -438,6 +460,14 @@ 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