import types import datetime import track class serverTrack: def __init__(self,db,loggedInUsers,locations): self.db=db self.loggedInUsers=loggedInUsers self.locations=locations ### ******************************************************************************* def getTrackShortInfo(self, sid, trackIds): try: locationId=self.loggedInUsers[int(sid)][2] except KeyError: return ("error","no such sid") try: machine=self.locations[locationId] except KeyError: return ("error","no such location") errorTrackIds=[] tracks=[] for trackId in trackIds: if not int(trackId)==0: c=self.db.cursor() trackExist=c.execute("""SELECT trackId,title,trackno FROM Track WHERE trackId=%s""", (trackId,)) if (trackExist<>1): errorTrackIds.append(trackId) else: result=c.fetchone() rate=self.getTrackRate(sid,trackId) tracks.append( (result[0],result[1],result[2],rate[1]) ) c.close() if len(errorTrackIds)>0: return ("error","trackId problem",errorTrackIds,tracks) return ("ok",tracks) ### ******************************************************************************* ### --> [trackId,title,trackno,length,copyrightYear,gain,peak,quality, ### codecVersion,codecVendor, rate, albumid, albumname] ### ******************************************************************************* def getTrackLongInfo(self, sid, trackId): try: locationId=self.loggedInUsers[int(sid)][2] except KeyError: return ("error","no such sid") try: machine=self.locations[locationId] except KeyError: return ("error","no such location") if (type(trackId)==types.ListType): return ("error","This command only takes on trackId as argument") info=[] if not (int(trackId)==0): c=self.db.cursor() # get track info trackExist=c.execute("""SELECT trackId,title,trackno,length,copyrightYear,gain,peak,quality,codecVersion,codecVendor FROM Track WHERE trackId=%s""", (trackId,)) if (trackExist<>1): c.close() return ("error","trackId doesn't exist", trackId) else: result=c.fetchone() rate=self.getTrackRate(sid,trackId) for i in result: if (i == None): # xmlrpc cannot handle None i = "" elif (type(i)==datetime.timedelta): i=i.seconds elif (type(i)==datetime.time): i=i.isoformat() elif (type(i)==datetime.date): i=i.year info.append(i) info.append(rate[1]) # get album info albumExist=c.execute("""SELECT Album.albumId, Album.name FROM Track, Album, Record WHERE Record.recordId = Track.recordId and Record.albumId = Album.albumId and Track.trackId=%s""", (trackId,)) if (albumExist<>1): # No album! Return empty string info.append("") info.append("") else: result=c.fetchone() info.append(result[0]) info.append(result[1]) # get artists artistExist=c.execute("""SELECT Artist.artistId, Artist.name FROM Artist, TrackArtist WHERE Artist.artistId = TrackArtist.artistId AND TrackArtist.trackId = %s""", (trackId,)) artists=[] if (artistExist<1): c.close() return ("error","artists don't exist", trackId) else: result=c.fetchall() # Append all artists for i in result: artists.append(i) info.append(artists) c.close() return ("ok",info) ### ******************************************************************************* def setTrackRate(self, sid, trackid, rate): try: locationId=self.loggedInUsers[int(sid)][2] userId=self.loggedInUsers[int(sid)][0] except KeyError: return ("error","no such sid") try: machine=self.locations[locationId] except KeyError: return ("error","no such location") int_TrackId=int(trackid) int_Rate=int(rate) if (int_Rate<-2 or int_Rate>2): return ("error","invalid rate") try: aTrack=track.Track(self.db,int_TrackId) except TypeError: return ("error","track not found") aTrack.setRate(userId,int_Rate) return ("ok",) ### ******************************************************************************* def getTrackRate(self, sid, trackId): try: locationId=self.loggedInUsers[int(sid)][2] userId=self.loggedInUsers[int(sid)][0] except KeyError: return ("error","no such sid") try: machine=self.locations[locationId] except KeyError: return ("error","no such location") int_trackId=int(trackId) try: aTrack=track.Track(self.db,int_trackId) except TypeError: return("error","trackid not found") rate=aTrack.getRate(userId) return ("ok",rate) ### ******************************************************************************* def getTrackGenre(self, sid, trackId): try: locationId=self.loggedInUsers[int(sid)][2] userId=self.loggedInUsers[int(sid)][0] except KeyError: return ("error","no such sid") try: machine=self.locations[locationId] except KeyError: return ("error","no such location") c=self.db.cursor() globalTrackGenreExist=c.execute("""SELECT GenreMapping.genreId,Genre.name FROM GenreMapping,Genre WHERE GenreMapping.genreId=Genre.genreId and referenceId=%s and userId is NULL and referenceType='track'""", (trackId,)) globalTrackGenre=c.fetchall() trackGenreExist=c.execute("""SELECT GenreMapping.genreId,Genre.name,genreMappingId FROM GenreMapping,Genre WHERE GenreMapping.genreId=Genre.genreId and referenceId=%s and userId=%s and referenceType='track'""", (trackId,userId)) userTrackGenre=c.fetchall() c.close() return ("ok",(globalTrackGenre,userTrackGenre)) ### ******************************************************************************* def setTrackGenre(self, sid, trackId, genreId): try: locationId=self.loggedInUsers[int(sid)][2] userId=self.loggedInUsers[int(sid)][0] except KeyError: return ("error","no such sid") try: machine=self.locations[locationId] except KeyError: return ("error","no such location") c=self.db.cursor() trackGenreExist=c.execute("""SELECT genreMappingId FROM GenreMapping WHERE genreId=%s and referenceId=%s and userId=%s and referenceType='track'""", (genreId,trackId,userId)) if (trackGenreExist==0): Exist=c.execute("""INSERT INTO GenreMapping (genreId,referenceType,referenceId,userId) values (%s,'track',%s,%s)""", (genreId,trackId,userId)) self.db.commit() c.close() return ("ok",) ### ******************************************************************************* def deleteTrackGenre(self, sid, genreMappingId): try: locationId=self.loggedInUsers[int(sid)][2] userId=self.loggedInUsers[int(sid)][0] except KeyError: return ("error","no such sid") try: machine=self.locations[locationId] except KeyError: return ("error","no such location") c=self.db.cursor() trackGenreExist=c.execute("""SELECT genreId FROM GenreMapping WHERE genreMappingId=%s and userId=%s""", (genreMappingId,userId)) if (trackGenreExist==0): c.close() return ("error","Genre don't exist or are not create by you") genreId=c.fetchone() rateExist=c.execute("""SELECT userId from UserRate where referenceType='track' and referenceId=%s and not userId=%s""",(genreId,userId)) if rateExist is not None: c.close() return ("error","Genre is rated by another user") Exist=c.execute("""DELETE FROM GenreMapping WHERE genreMappingId=%s""", (genreMappingId,)) self.db.commit() c.close() return ("ok",)