clThreadedDownload.py

00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 # ----------------------------------------------------------------------------
00005 # pyjama - python jamendo audioplayer
00006 # Copyright (c) 2008 Daniel Nögel
00007 #
00008 # This program is free software: you can redistribute it and/or modify
00009 # it under the terms of the GNU General Public License as published by
00010 # the Free Software Foundation, either version 3 of the License, or
00011 # (at your option) any later version.
00012 #
00013 # This program is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 # GNU General Public License for more details.
00017 # You should have received a copy of the GNU General Public License
00018 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 # ----------------------------------------------------------------------------
00020 
00021 import urllib
00022 import hashlib
00023 import os
00024 
00025 import threading
00026 import gtk
00027 
00028 import clSettings
00029 import functions
00030 from clWidgets import ProcessDialog
00031 
00032 from time import sleep, time
00033 
00034 # Gettext - Übersetzung
00035 functions.translation_gettext()
00036 #def _(string):
00037 #    return string
00038 
00039 settings = clSettings.settings()
00040 
00041 class Download_Images():
00042     def __init__(self, pyjama, imglist):
00043         self.__pyjama = pyjama
00044         self.__imglist = imglist
00045 
00046         self.threads = []
00047 
00048 
00049     def start(self):
00050         max_time_for_downloading = 10 + len(self.__imglist) * 0.1
00051         start_time = time()
00052 
00053         self.dia = ProcessDialog(self.__pyjama, _("Downloading %i images" % len(self.__imglist)))
00054         self.dia.set_description(_("Need to download %i images in order to display this page" % len(self.__imglist)))
00055         self.dia.show()
00056 
00057         self.__pyjama.window.do_events()
00058 
00059         counter = 0
00060         for image in self.__imglist:
00061             self.threads.append(Download(self.__pyjama, image, counter))
00062             self.threads[counter].start()
00063             counter += 1
00064         print "Downloading %i images" % counter
00065 
00066         threads_alive = self.count_threads()
00067         while threads_alive > 0:
00068             if start_time + max_time_for_downloading <= time():
00069                 print "Aborting image download"
00070                 self.dia.destroy()
00071                 return threads_alive
00072             percentage = 100.0  / len(self.__imglist) *  (len(self.__imglist) - threads_alive)
00073             self.dia.set_status(percentage, _("%i images remaining" % threads_alive))
00074             self.__pyjama.window.do_events()
00075             print "%i threads pending, will abort in %i seconds" % (threads_alive, max_time_for_downloading + start_time - time())
00076             sleep(self.__pyjama.settings.get_value("PERFORMANCE", "THREAD_WAIT"))
00077             threads_alive = self.count_threads() 
00078 
00079         self.dia.destroy()
00080 
00081     def count_threads(self):
00082         counter = 0
00083         for thread in self.threads:
00084             if thread.isAlive():
00085                 counter += 1
00086         return counter
00087 
00088     def threads_alive(self):
00089         alive = []
00090         for thread in self.threads:
00091             if thread.isAlive():
00092                 alive.append(thread)
00093         return alive
00094 
00095 class Download( threading.Thread ):
00096     def __init__( self, parent, url, num):
00097         if type(url) == type(()):
00098             if url[0] == "album":
00099                 self.saveas = "http://api.jamendo.com/get2/image/album/redirect/?id=%s&imagesize=100" % url[1]
00100                 self.url = "http://imgjam.com/albums/%i/covers/1.100.jpg" % url[1]
00101         else:
00102             self.saveas = url
00103             self.url = url
00104         self.parent = parent
00105         self.num = num
00106         threading.Thread.__init__(self)
00107   
00108     ###################################################################
00109     #
00110     # get a album's cover
00111     # RETURNS: None = error
00112     #
00113     def run( self ):
00114         md5hash = hashlib.md5(self.saveas).hexdigest()
00115         imagepath = os.path.join(self.parent.home, "images", md5hash)
00116         if not os.path.exists(imagepath) and self.url != "":
00117             if self.parent.debug_extreme:
00118                 print ("[%i] Trying to burst-download cover for '%s'[...]") % (self.num, self.url)
00119             try:
00120                 urllib.urlretrieve(self.url, imagepath)
00121                 if self.parent.debug_extreme:
00122                     print ("[%i] Burst-downloaded Cover from Jamendo") % self.num
00123                 return "0"
00124             except IOError:
00125                 print ("[%i] Could not get album cover from jamendo:") % self.num
00126                 print "'%s'" % self.url
00127                 return "1"
00128         elif self.url == "":
00129             return "2"
00130         else:
00131             return "0"
00132             if self.parent.debug_extreme:
00133                 print ("[%i] Not Burst-Downloading: Cover on HD") % self.num
00134 
00135 ###################################################################
00136 #
00137 # test if to much threads a running at the same time
00138 # RETURNS: True = to much threads, False = less than MAX_THREADS
00139 #
00140 def threadLimiter(threads):
00141     counter = 0
00142     for thread in threads:
00143         if threads[thread].isAlive():
00144             counter += 1
00145         if counter > settings.get_value("PERFORMANCE", "MAX_THREADS"):
00146             return True
00147     return False
00148 
00149 def threadCounter(threads):
00150     counter = 0
00151     for thread in threads:
00152         if threads[thread].isAlive():
00153             counter += 1
00154     return counter

Generated on Thu Jun 4 19:08:24 2009 for Pyjama by  doxygen 1.5.8