2008-03-06 04:22:03 -08:00
|
|
|
import pygtk
|
|
|
|
import gtk
|
2008-03-07 05:06:44 -08:00
|
|
|
import gobject
|
|
|
|
import os
|
2008-03-06 04:22:03 -08:00
|
|
|
|
2008-03-07 05:06:44 -08:00
|
|
|
class artist:
|
|
|
|
artistID = None
|
|
|
|
name = None
|
|
|
|
image = None
|
2008-03-02 12:46:41 -08:00
|
|
|
|
2008-03-06 04:22:03 -08:00
|
|
|
def __init__(self, music_player):
|
|
|
|
self.music_player = music_player
|
2008-03-02 12:46:41 -08:00
|
|
|
|
|
|
|
def import_from_mythObject(self, mythObject):
|
|
|
|
try:
|
2008-03-07 05:06:44 -08:00
|
|
|
#self.artistID = mythObject['artist_id']
|
|
|
|
#self.name = mythObject['artist_name']
|
|
|
|
#self.image = mythObject['artist_image']
|
|
|
|
self.artistID = mythObject[0]
|
2008-03-02 12:46:41 -08:00
|
|
|
self.name = mythObject[1]
|
2008-03-06 04:22:03 -08:00
|
|
|
self.image = mythObject[2]
|
2008-03-07 05:06:44 -08:00
|
|
|
|
|
|
|
if self.image is None: self.image = "unset"
|
|
|
|
|
2008-03-02 12:46:41 -08:00
|
|
|
|
|
|
|
except IndexError, e:
|
|
|
|
print "Music_Player: Found difference in DB structure for artists. Attempting to continue."
|
2008-03-06 04:22:03 -08:00
|
|
|
|
2008-03-07 05:06:44 -08:00
|
|
|
def update_db(self):
|
|
|
|
print "Music_Player: Looks like this is the first time the Music Player has been started. Attempting to upgrade DB"
|
|
|
|
sql = "ALTER TABLE music_artists ADD artist_image TEXT AFTER artist_name;"
|
|
|
|
self.music_player.dbMgr.run_sql(sql)
|
|
|
|
print "Music_Player: DB Upgrade completed"
|
|
|
|
|
2008-03-06 04:22:03 -08:00
|
|
|
|
|
|
|
def get_image(self):
|
2008-03-07 05:06:44 -08:00
|
|
|
#If image is still equal to None it means that the artist_image column did not exist in the db, we should create it.
|
|
|
|
if self.image is None:
|
|
|
|
self.update_db()
|
|
|
|
self.image = "unset"
|
|
|
|
#If there was something in self.image, means we've probably already got a pic
|
|
|
|
if not self.image == "unset":
|
|
|
|
filename = self.music_player.images_dir + "artists/" + self.image
|
|
|
|
try:
|
|
|
|
pixbuf = gtk.gdk.pixbuf_new_from_file(filename)
|
|
|
|
except gobject.GError, e:
|
|
|
|
print "Music_Player: Attempted to open file '%s', but it does not exist" % (filename)
|
|
|
|
return
|
|
|
|
|
|
|
|
return pixbuf
|
|
|
|
#If self.image is a eqaul to 'unset', means the column exists but that the image entry is blank, we should try to find one
|
2008-03-06 04:22:03 -08:00
|
|
|
else:
|
2008-03-07 05:06:44 -08:00
|
|
|
pixbuf = self.get_image_from_lastFM()
|
|
|
|
if not pixbuf is None: self.save_image(pixbuf)
|
|
|
|
return pixbuf
|
2008-03-06 04:22:03 -08:00
|
|
|
|
|
|
|
def get_image_from_lastFM(self):
|
2008-03-07 05:06:44 -08:00
|
|
|
pixbuf = self.music_player.lastFM.get_artist_image(self.name)
|
|
|
|
return pixbuf
|
|
|
|
|
|
|
|
#Saves an image (pixbuf) to file and updates the Myth db
|
|
|
|
def save_image(self, pixbuf):
|
|
|
|
base_dir = self.music_player.images_dir + "artists/"
|
|
|
|
#Check to make sure the above directory exists
|
|
|
|
if not os.path.isdir(base_dir):
|
|
|
|
try:
|
|
|
|
os.makedirs(base_dir, int('777',8))
|
|
|
|
except OSError:
|
|
|
|
print "Music Player Error: Unable to write to music directory '%s'" % (base_dir)
|
|
|
|
return
|
|
|
|
|
|
|
|
self.name = self.name.replace("'","")
|
|
|
|
filename = base_dir + self.name + ".png"
|
|
|
|
try:
|
|
|
|
pixbuf.save(filename, "png")
|
|
|
|
except gobject.GError:
|
|
|
|
print "Music_Player: Permission denied trying to save '%s'" % (filename)
|
|
|
|
return
|
|
|
|
|
|
|
|
#Now insert an entry into the db for this
|
|
|
|
filename_short = self.name + ".png"
|
|
|
|
sql = "UPDATE music_artists SET artist_image = '%s' WHERE artist_id = '%s'" % (filename_short, self.artistID)
|
|
|
|
self.music_player.dbMgr.run_sql(sql)
|
2008-03-02 12:46:41 -08:00
|
|
|
|