Created
September 4, 2015 19:29
-
-
Save thebino/e6fdbac843c91d9ce281 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import sqlite3 as lite | |
import os | |
def _open_db(db): | |
try: | |
con = lite.connect(db) | |
cur = con.cursor() | |
return (cur,con) | |
except lite.Error as e: | |
print("Could not open database %s: %s" % (db,e)) | |
quit(1) | |
if __name__=="__main__": | |
parser = argparse.ArgumentParser(description='delete duplicates in XBMC/Kodi video database') | |
parser.add_argument('db', help='path to MyVideos**.db library') | |
args = parser.parse_args() | |
(cur,con) = _open_db(args.db) | |
# list all duplicates in table 'path' | |
cur.execute('SELECT idPath, strPath, strHash, count(*) FROM path GROUP BY strPath, strHash having count(*) > 1') | |
result = cur.fetchmany() | |
duplicates = [] | |
while result: | |
for idPath, strPath, strHash, count in result: | |
duplicates.append(idPath) | |
result = cur.fetchmany() | |
print '%d duplicated items found!' % len(duplicates) | |
# delete all duplicates from table 'path' | |
for i in range(len(duplicates)): | |
print 'delete item with idPath %s from table path' % duplicates[i] | |
cur.execute("DELETE FROM path WHERE idPath=?", (duplicates[i],)) | |
con.commit() | |
# clean progress line | |
print "\r" + 100*" " + "\r", | |
con.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment