setstids 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python
  2. import sys;
  3. import adodb;
  4. class CleanUp:
  5. def __init__(self, dbname, username):
  6. self.conn = adodb.NewADOConnection('postgres')
  7. self.conn.Connect('user=%s dbname=%s' % (username, dbname))
  8. def doit(self):
  9. self.cursor = self.conn.Execute('SELECT DISTINCT artist, album, track, mbid from Scrobbles WHERE stid is NULL');
  10. while not self.cursor.EOF:
  11. row = self.cursor.GetRowAssoc(0)
  12. artist = row['artist']
  13. album = row['album']
  14. track = row['track']
  15. mbid = row['mbid']
  16. stid = self.lookupStid(artist,album,track,mbid)
  17. if not stid:
  18. tid = self.lookupTrack(artist, album, track, mbid)
  19. if tid:
  20. print "Adding stid for %s/%s/%s/%s/%s" % (tid,artist,album,track,mbid)
  21. self.acursor = self.conn.Execute("INSERT INTO Scrobble_Track (artist,album,name,mbid,track) VALUES(lower(%(artist)s),lower(%(album)s),lower(%(name)s),lower(%(mbid)s),%(track)s)", {'artist': artist, 'album': album, 'name': track, 'mbid': mbid, 'track': tid})
  22. else:
  23. print "Adding tid for %s/%s/%s/%s" % (artist,album,track,mbid)
  24. self.acursor = self.conn.Execute("INSERT INTO Track (artist,album,name,mbid) VALUES(%(artist)s,%(album)s,%(name)s,%(mbid)s)", {'artist': artist, 'album': album, 'name': track, 'mbid': mbid})
  25. else:
  26. print "Setting %d to match %s/%s/%s/%s" % (stid,artist,album,track,mbid)
  27. self.scursor = self.conn.Execute("UPDATE Scrobbles SET stid = %%(stid)s WHERE track = %%(track)s AND artist = %%(artist)s AND %s AND %s AND stid IS NULL" % (('lower(album) = lower(%(album)s)' if album else 'album IS NULL'), ('lower(mbid) = lower(%(mbid)s)' if mbid else 'mbid IS NULL')), {'stid': stid, 'track': track, 'artist': artist, 'album': album, 'mbid': mbid})
  28. self.cursor.MoveNext()
  29. def lookupStid(self, artist, album, track, mbid):
  30. self.lucursor = self.conn.Execute('SELECT * from Scrobble_Track WHERE lower(artist) = lower(%%(artist)s) AND %s AND lower(name) = lower(%%(track)s) AND %s' % (('lower(album) = lower(%(album)s)' if album else 'album IS NULL'), ('lower(mbid) = lower(%(mbid)s)' if mbid else 'mbid IS NULL')), {'artist': artist, 'album': album, 'track': track, 'mbid': mbid});
  31. if not self.lucursor.EOF:
  32. lrow = self.lucursor.GetRowAssoc(0)
  33. return lrow['id']
  34. else:
  35. return None
  36. def lookupTrack(self, artist, album, track, mbid):
  37. self.tucursor = self.conn.Execute('SELECT * from Track WHERE lower(artist) = lower(%%(artist)s) AND %s AND lower(name) = lower(%%(track)s) AND %s' % (('lower(album) = lower(%(album)s)' if album else 'album IS NULL'), ('lower(mbid) = lower(%(mbid)s)' if mbid else 'mbid IS NULL')), {'artist': artist, 'album': album, 'track': track, 'mbid': mbid});
  38. if not self.tucursor.EOF:
  39. trow = self.tucursor.GetRowAssoc(0)
  40. return trow['id']
  41. else:
  42. return None
  43. def close(self):
  44. self.cursor.Close()
  45. self.conn.Close()
  46. if __name__ == "__main__":
  47. if len(sys.argv) != 3:
  48. print "Usage: setstids <pgsql dbname> <pgsql username>"
  49. sys.exit(1)
  50. cleaner = CleanUp(sys.argv[1], sys.argv[2])
  51. cleaner.doit()
  52. cleaner.close()