aboutsummaryrefslogtreecommitdiff
path: root/jamendo.lua
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 /jamendo.lua
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.
Diffstat (limited to 'jamendo.lua')
-rw-r--r--jamendo.lua42
1 files changed, 36 insertions, 6 deletions
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