Advertisement
virpara

Music lens needs Banshee or RhythmBox

Jun 19th, 2012
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 KB | None | 0 0
  1. ACTIVE_PROVIDERS = {
  2.     "banshee":  "BansheeProvider",
  3.     "rhythmbox":"RhythmboxProvider"
  4. }
  5.  
  6. class BansheeProvider:
  7.     banshee_dbFile = os.getenv("HOME") + "/.config/banshee-1/banshee.db"
  8.     artdir = os.getenv("HOME") + "/.cache/media-art/album-"
  9.  
  10.     def isActive(self):
  11.         # Check that database exists
  12.         return os.path.exists(self.banshee_dbFile)
  13.  
  14.     def fillTracksInfo(self):      
  15.         import sqlite3
  16.  
  17.         # TODO: Find out how to use dbus methods to access Banshee's library. In the meantime:
  18.        
  19.         # Time for a smash and grab Banzai charge on Banshee's database!
  20.         # Charge!!! - Connect to Banshee's database
  21.         conn = sqlite3.connect(self.banshee_dbFile)
  22.         # Break open the safe
  23.         c = conn.cursor()
  24.         # Go through the safe and grab track names, their uris, the artist name, the album title and the track's mimetypes
  25.         rows = c.execute('''SELECT coretracks.Title, coretracks.Uri, coreartists.Name, corealbums.Title, coretracks.MimeType, corealbums.ArtistName
  26.                             FROM coretracks, corealbums, coreartists
  27.                             WHERE coretracks.artistID = coreartists.artistID
  28.                                 AND coretracks.albumID = corealbums.albumID
  29.                                 AND coretracks.PrimarySourceID = 1''')
  30.         # Pour all the treasure we found into a big bag marked 'swag'
  31.         tracks = c.fetchall()
  32.         # Close the safe and run away
  33.         c.close()
  34.         print "Updated tracks from Banshee database"
  35.         return tracks
  36.  
  37.     def getAlbumArt(self, track):
  38.         import unicodedata
  39.         import hashlib
  40.         hashname = unicode(track[5] + "\t" + track[3])
  41.         hashname = unicodedata.normalize("NFKD", hashname)
  42.         md5album = hashlib.md5()
  43.         try:
  44.             md5album.update(hashname)
  45.             value = md5album.hexdigest()
  46.         except:
  47.             value = ""
  48.         return "%s%s.jpg" % (self.artdir, value)
  49.  
  50. class RhythmboxProvider:
  51.     rhythmbox_dbFile = os.getenv("HOME") + "/.local/share/rhythmbox/rhythmdb.xml"
  52.     artdir = os.getenv("HOME") + "/.cache/rhythmbox/covers"
  53.  
  54.     def isActive(self):
  55.         # Check that database exists
  56.         return os.path.exists(self.rhythmbox_dbFile)
  57.  
  58.     def fillTracksInfo(self):
  59.         import xml.etree.cElementTree as ElementTree
  60.         keys = set(("title", "artist", "album", "track-number", "location", "mimetype", ))
  61.         tracks = []
  62.         strmap = {}
  63.  
  64.         # Parse with iterparse; we get the elements when
  65.         # they are finished, and can remove them directly after use.
  66.  
  67.         for event, entry in ElementTree.iterparse(self.rhythmbox_dbFile):
  68.             if not (entry.tag == ("entry") and entry.get("type") == "song"):
  69.                 continue
  70.  
  71.             info = {}
  72.             for child in entry.getchildren():
  73.                 if child.tag in keys:
  74.                     tag = self._lookup_string(child.tag, strmap)
  75.                     text = self._lookup_string(child.text, strmap)
  76.                     info[tag] = text
  77.  
  78.             track = {}
  79.             track[0] = info['title']
  80.             track[1] = info['location']
  81.             track[2] = info['artist']
  82.             track[3] = info['album']
  83.             track[4] = info['mimetype']
  84.             track[5] = info['artist']
  85.  
  86.             tracks.append(track)
  87.             entry.clear()
  88.            
  89.         print "Updated tracks from Rhythmbox database"
  90.         return tracks
  91.  
  92.     def getAlbumArt(self, track):
  93.         return "%s/%s - %s.jpg" % (self.artdir, track[2], track[3])
  94.  
  95.     def _lookup_string(self, string, strmap):
  96.         """Look up @string in the string map,
  97.         and return the copy in the map.
  98.  
  99.         If not found, update the map with the string.
  100.         """
  101.         string = string or ""
  102.         try:
  103.             return strmap[string]
  104.         except KeyError:
  105.             strmap[string] = string
  106.             return string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement