Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ACTIVE_PROVIDERS = {
- "banshee": "BansheeProvider",
- "rhythmbox":"RhythmboxProvider"
- }
- class BansheeProvider:
- banshee_dbFile = os.getenv("HOME") + "/.config/banshee-1/banshee.db"
- artdir = os.getenv("HOME") + "/.cache/media-art/album-"
- def isActive(self):
- # Check that database exists
- return os.path.exists(self.banshee_dbFile)
- def fillTracksInfo(self):
- import sqlite3
- # TODO: Find out how to use dbus methods to access Banshee's library. In the meantime:
- # Time for a smash and grab Banzai charge on Banshee's database!
- # Charge!!! - Connect to Banshee's database
- conn = sqlite3.connect(self.banshee_dbFile)
- # Break open the safe
- c = conn.cursor()
- # Go through the safe and grab track names, their uris, the artist name, the album title and the track's mimetypes
- rows = c.execute('''SELECT coretracks.Title, coretracks.Uri, coreartists.Name, corealbums.Title, coretracks.MimeType, corealbums.ArtistName
- FROM coretracks, corealbums, coreartists
- WHERE coretracks.artistID = coreartists.artistID
- AND coretracks.albumID = corealbums.albumID
- AND coretracks.PrimarySourceID = 1''')
- # Pour all the treasure we found into a big bag marked 'swag'
- tracks = c.fetchall()
- # Close the safe and run away
- c.close()
- print "Updated tracks from Banshee database"
- return tracks
- def getAlbumArt(self, track):
- import unicodedata
- import hashlib
- hashname = unicode(track[5] + "\t" + track[3])
- hashname = unicodedata.normalize("NFKD", hashname)
- md5album = hashlib.md5()
- try:
- md5album.update(hashname)
- value = md5album.hexdigest()
- except:
- value = ""
- return "%s%s.jpg" % (self.artdir, value)
- class RhythmboxProvider:
- rhythmbox_dbFile = os.getenv("HOME") + "/.local/share/rhythmbox/rhythmdb.xml"
- artdir = os.getenv("HOME") + "/.cache/rhythmbox/covers"
- def isActive(self):
- # Check that database exists
- return os.path.exists(self.rhythmbox_dbFile)
- def fillTracksInfo(self):
- import xml.etree.cElementTree as ElementTree
- keys = set(("title", "artist", "album", "track-number", "location", "mimetype", ))
- tracks = []
- strmap = {}
- # Parse with iterparse; we get the elements when
- # they are finished, and can remove them directly after use.
- for event, entry in ElementTree.iterparse(self.rhythmbox_dbFile):
- if not (entry.tag == ("entry") and entry.get("type") == "song"):
- continue
- info = {}
- for child in entry.getchildren():
- if child.tag in keys:
- tag = self._lookup_string(child.tag, strmap)
- text = self._lookup_string(child.text, strmap)
- info[tag] = text
- track = {}
- track[0] = info['title']
- track[1] = info['location']
- track[2] = info['artist']
- track[3] = info['album']
- track[4] = info['mimetype']
- track[5] = info['artist']
- tracks.append(track)
- entry.clear()
- print "Updated tracks from Rhythmbox database"
- return tracks
- def getAlbumArt(self, track):
- return "%s/%s - %s.jpg" % (self.artdir, track[2], track[3])
- def _lookup_string(self, string, strmap):
- """Look up @string in the string map,
- and return the copy in the map.
- If not found, update the map with the string.
- """
- string = string or ""
- try:
- return strmap[string]
- except KeyError:
- strmap[string] = string
- return string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement