imms.py
branchimmsview
changeset 34 5bef7600193c
parent 33 ad808d18c693
child 38 785c66feccd3
equal deleted inserted replaced
33:ad808d18c693 34:5bef7600193c
   136         self.cu.execute(qry)
   136         self.cu.execute(qry)
   137         return self.cu.fetchall()
   137         return self.cu.fetchall()
   138     def get_uid_by_path(self, path):
   138     def get_uid_by_path(self, path):
   139         entries = self.get_library_entry(path = path)
   139         entries = self.get_library_entry(path = path)
   140         return map(lambda x: x[1], entries)
   140         return map(lambda x: x[1], entries)
       
   141     def get_alike(self, uid):
       
   142         self.cu.execute('SELECT sid FROM Library WHERE uid = %d;' % uid)
       
   143         sids = self.cu.fetchall()
       
   144         res = {}
       
   145         for sid in sids:
       
   146             sid = int(sid)
       
   147             self.cu.execute("SELECT uid FROM Library "
       
   148                             "WHERE sid = %d;" % sid)
       
   149             res[sid] = []
       
   150             for uid in self.cu.fetchall():
       
   151                 res[sid].append(int(uid))
       
   152         return res
   141     def get_ratings_and_paths(self, uids = None):
   153     def get_ratings_and_paths(self, uids = None):
   142         qry = '''SELECT l.uid, r.rating, l.path, ls.last
   154         qry = '''SELECT l.uid, r.rating, l.path, ls.last
   143                FROM Library l, Rating r, Last ls
   155                FROM Library l, Rating r, Last ls
   144                WHERE l.uid = r.uid AND l.sid = ls.sid'''
   156                WHERE l.uid = r.uid AND l.sid = ls.sid'''
   145         if uids:
   157         if uids:
   167     def get_ratings_and_infos(self):
   179     def get_ratings_and_infos(self):
   168         self.cu.execute('''SELECT r.rating, i.artist, i.title
   180         self.cu.execute('''SELECT r.rating, i.artist, i.title
   169             FROM Library l, Rating r, Info i
   181             FROM Library l, Rating r, Info i
   170             WHERE l.uid = r.uid AND l.sid = i.sid;''')
   182             WHERE l.uid = r.uid AND l.sid = i.sid;''')
   171         return self.cu.fetchall()
   183         return self.cu.fetchall()
       
   184     def clean_info(self):
       
   185         self.cu.execute("""DELETE FROM Info
       
   186             WHERE sid NOT IN
       
   187             (SELECT sid FROM Library);""")
       
   188     def clean_last(self):
       
   189         self.cu.execute("""DELETE FROM Last
       
   190             WHERE sid NOT IN
       
   191             (SELECT sid FROM Library);""")
       
   192     def clean_rating(self):
       
   193         self.cu.execute("""DELETE FROM Rating
       
   194             WHERE uid NOT IN
       
   195             (SELECT uid FROM Library);""")
       
   196     def clean_acoustic(self):
       
   197         self.cu.execute("""DELETE FROM Acoustic
       
   198             WHERE uid NOT IN
       
   199             (SELECT uid FROM Library);""")
       
   200     def clean_correlations(self):
       
   201         self.cu.execute("""DELETE FROM Correlations
       
   202             WHERE origin NOT IN (SELECT sid FROM Library)
       
   203             OR destination NOT IN (SELECT sid FROM Library);""")
       
   204     def clean_all(self):
       
   205         self.cu.execute("BEGIN TRANSACTION;")
       
   206         self.clean_info()
       
   207         self.clean_last()
       
   208         self.clean_rating()
       
   209         self.clean_acoustic()
       
   210         self.clean_correlations()
       
   211         self.cu.execute("COMMIT;")
   172 
   212 
   173 class IMMSCleaner:
   213 class IMMSCleaner:
   174     def __init__(self, db):
   214     def __init__(self, db):
   175         self.db = db
   215         self.db = db
   176     def check_uid(self, uid):
   216     def check_uid(self, uid):
   224             else:
   264             else:
   225                 print >> _log, "Renaming ", path, " into ", newfile
   265                 print >> _log, "Renaming ", path, " into ", newfile
   226                 self.db.update_filename(path, newfile)
   266                 self.db.update_filename(path, newfile)
   227         map(self.check_uid, unique(deleted_uids))
   267         map(self.check_uid, unique(deleted_uids))
   228         map(self.check_sid, unique(deleted_sids))
   268         map(self.check_sid, unique(deleted_sids))
   229     def clean_rating(self):
   269 ##     def clean_rating(self):
   230 	print >> _log, "Clean Rating"
   270 ## 	print >> _log, "Clean Rating"
   231         rates = self.db.get_ratings()
   271 ##         rates = self.db.get_ratings()
   232         rates = unique(map(lambda x: x[0], rates))
   272 ##         rates = unique(map(lambda x: x[0], rates))
   233         map(self.check_uid, rates)
   273 ##         map(self.check_uid, rates)
   234     def clean_acoustic(self):
   274 ##     def clean_acoustic(self):
   235 	print >> _log, "Clean Acoustic"
   275 ## 	print >> _log, "Clean Acoustic"
   236         uids = self.db.get_acoustics()
   276 ##         uids = self.db.get_acoustics()
   237 	uids = map(lambda x: x[0], uids )
   277 ## 	uids = map(lambda x: x[0], uids )
   238         map(self.check_uid, uids)
   278 ##         map(self.check_uid, uids)
   239     def clean_info(self):
   279 ##     def clean_info(self):
   240 	print >> _log, "Clean Info"
   280 ## 	print >> _log, "Clean Info"
   241         sids = map(lambda x: x[0], self.db.get_infos())
   281 ##         sids = map(lambda x: x[0], self.db.get_infos())
   242         map(self.check_sid, sids)
   282 ##         map(self.check_sid, sids)
   243     def clean_last(self):
   283 ##     def clean_last(self):
   244 	print >> _log, "Clean Last"
   284 ## 	print >> _log, "Clean Last"
   245         sids = map(lambda x: x[0], self.db.get_last())
   285 ##         sids = map(lambda x: x[0], self.db.get_last())
   246         map(self.check_sid, sids)
   286 ##         map(self.check_sid, sids)
   247     def clean_all(self):
   287     def clean_all(self):
   248         self.clean_library()
   288         self.db.clean_all()
   249         self.clean_rating()
   289 ##         self.clean_library()
   250         self.clean_acoustic()
   290 ##         self.clean_rating()
   251         self.clean_info()
   291 ##         self.clean_acoustic()
   252         self.clean_last()
   292 ##         self.clean_info()
       
   293 ##         self.clean_last()