home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / rhythmbox / plugins / artdisplay / CoverArtDatabase.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-30  |  4.0 KB  |  124 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import rhythmdb
  5. import os
  6. import gtk
  7. from AmazonCoverArtSearch import AmazonCoverArtSearch
  8. from Loader import Loader
  9. ART_FOLDER = '~/.gnome2/rhythmbox/covers'
  10.  
  11. class CoverArtDatabase(object):
  12.     
  13.     def __init__(self):
  14.         self.loader = Loader()
  15.  
  16.     
  17.     def create_search(self):
  18.         return AmazonCoverArtSearch(self.loader)
  19.  
  20.     
  21.     def build_art_cache_filename(self, album, artist, extension):
  22.         art_folder = os.path.expanduser(ART_FOLDER)
  23.         if not os.path.exists(art_folder):
  24.             os.mkdir(art_folder)
  25.         
  26.         if extension is None:
  27.             extension = 'jpg'
  28.         
  29.         return art_folder + '/%s - %s.%s' % (artist.replace('/', '-'), album.replace('/', '-'), extension)
  30.  
  31.     
  32.     def get_pixbuf(self, db, entry, callback):
  33.         if entry is None:
  34.             callback(entry, None)
  35.             return None
  36.         
  37.         st_artist = db.entry_get(entry, rhythmdb.PROP_ARTIST)
  38.         st_album = db.entry_get(entry, rhythmdb.PROP_ALBUM)
  39.         if st_album == '':
  40.             st_album = 'Unknown'
  41.         
  42.         if st_artist == '':
  43.             st_artist = 'Unknown'
  44.         
  45.         if st_album == 'Unknown' and st_artist == 'Unknown':
  46.             callback(entry, None)
  47.             return None
  48.         
  49.         for char in [
  50.             '"']:
  51.             st_artist = st_artist.replace(char, '')
  52.             st_album = st_album.replace(char, '')
  53.         
  54.         art_location = self.build_art_cache_filename(st_album, st_artist, 'jpg')
  55.         blist_location = self.build_art_cache_filename(st_album, st_artist, 'rb-blist')
  56.         if os.path.exists(art_location):
  57.             pixbuf = gtk.gdk.pixbuf_new_from_file(art_location)
  58.             callback(entry, pixbuf)
  59.         elif os.path.exists(blist_location):
  60.             callback(entry, None)
  61.         else:
  62.             se = self.create_search()
  63.             se.search(db, entry, self.on_search_engine_results, callback)
  64.  
  65.     
  66.     def on_search_engine_results(self, search_engine, entry, results, callback):
  67.         if results is None:
  68.             self._do_blacklist_and_callback(search_engine, callback)
  69.             return None
  70.         
  71.         best_match = search_engine.get_best_match(results)
  72.         if best_match is None:
  73.             self._do_blacklist_and_callback(search_engine, callback)
  74.             return None
  75.         
  76.         pic_url = str(best_match.ImageUrlLarge)
  77.         self.loader.get_url(pic_url, self.on_image_data_received, search_engine, 'large', callback, best_match)
  78.  
  79.     
  80.     def _do_blacklist_and_callback(self, search_engine, callback):
  81.         self._create_blacklist(search_engine.st_artist, search_engine.st_album)
  82.         callback(search_engine.entry, None)
  83.  
  84.     
  85.     def _create_blacklist(self, artist, album):
  86.         location = self.build_art_cache_filename(album, artist, 'rb-blist')
  87.         f = file(location, 'w')
  88.         f.close()
  89.         return location
  90.  
  91.     
  92.     def _create_artwork(self, artist, album, image_data):
  93.         location = self.build_art_cache_filename(album, artist, 'jpg')
  94.         f = file(location, 'wb')
  95.         f.write(image_data)
  96.         f.close()
  97.         return location
  98.  
  99.     
  100.     def on_image_data_received(self, image_data, search_engine, image_version, callback, best_match):
  101.         if image_data is None:
  102.             res = search_engine.search_next()
  103.             if not res:
  104.                 self._do_blacklist_and_callback(search_engine, callback)
  105.             
  106.             return None
  107.         
  108.         if len(image_data) < 1000:
  109.             if image_version == 'large' and best_match is not None:
  110.                 pic_url = str(best_match.ImageUrlMedium)
  111.                 self.loader.get_url(pic_url, self.on_image_data_received, search_engine, 'medium', callback, best_match)
  112.                 return None
  113.             
  114.             res = search_engine.search_next()
  115.             if not res:
  116.                 self._do_blacklist_and_callback(search_engine, callback)
  117.             
  118.         else:
  119.             location = self._create_artwork(search_engine.st_artist, search_engine.st_album, image_data)
  120.             pixbuf = gtk.gdk.pixbuf_new_from_file(location)
  121.             callback(search_engine.entry, pixbuf)
  122.  
  123.  
  124.