]> gitweb.fluxo.info Git - awesompd.git/commitdiff
Asynchronous download album cover for the next track
authorAlexander Yakushev <yakushev.alex@gmail.com>
Tue, 3 Jan 2012 13:40:42 +0000 (15:40 +0200)
committerAlexander Yakushev <yakushev.alex@gmail.com>
Tue, 3 Jan 2012 13:40:42 +0000 (15:40 +0200)
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.

awesompd.lua
jamendo.lua

index d88f8d16667efb9db99af486b6a4ea97c13495c6..10c45f36c28a3cc2a25b906aff45a39459859d03 100644 (file)
@@ -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)
index 3f985c48fc990d8351604fbbcb0ec8f9c36d07cc..164a7b8e3cbbf871cb8ca8f6e2e2031f20adcd7c 100644 (file)
@@ -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