scrobbles-revisionism 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python
  2. import sys;
  3. import MySQLdb as ordbms
  4. class CleanUp:
  5. def __init__(self, hostname, username, password, database):
  6. self.conn = ordbms.connect (host = hostname,
  7. user = username,
  8. passwd = password,
  9. db = database)
  10. self.cursor = self.conn.cursor ()
  11. self.twocursor = self.conn.cursor ()
  12. def doit(self):
  13. self.cursor.execute("SELECT artist from Scrobbles;");
  14. while (1):
  15. row = self.cursor.fetchone()
  16. if row == None:
  17. break
  18. oldname = row[0]
  19. newname = oldname
  20. if "\'" in row[0]:
  21. newname = row[0].replace("\\'","'")
  22. if "&" in oldname:
  23. newname = newname.replace("&","&")
  24. if """ in oldname:
  25. newname = newname.replace(""","\"")
  26. if "<" in oldname:
  27. newname = newname.replace("&lt;","<")
  28. if "&gt;" in oldname:
  29. newname = newname.replace("&gt;",">")
  30. if " (PREVIEW: buy it at www.magnatune.com)" in oldname:
  31. newname = newname.replace(" (PREVIEW: buy it at www.magnatune.com)","")
  32. if oldname <> newname:
  33. print "%s >>>INTO>>> %s" % (oldname, newname)
  34. try:
  35. self.twocursor.execute("UPDATE Scrobbles SET artist = %s WHERE artist = %s", (newname, oldname))
  36. self.twocursor.execute("DELETE FROM Artist WHERE name = %s", (oldname))
  37. except Exception, e:
  38. print e
  39. def close(self):
  40. self.cursor.close()
  41. self.conn.commit()
  42. self.conn.close()
  43. def artist_exists(self, artist):
  44. try:
  45. self.cursor.execute("SELECT name FROM Artist WHERE name = %s ", (artist,))
  46. return self.cursor.rowcount != 0
  47. except:
  48. return False
  49. if __name__ == "__main__":
  50. if len(sys.argv) != 5:
  51. print "Usage: scrobbles-revisionism <mysql hostname> <mysql username> <mysql password> <mysql database>"
  52. sys.exit(1)
  53. cleaner = CleanUp(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
  54. cleaner.doit()
  55. cleaner.close()