diff options
author | cos <cos> | 2024-07-03 01:01:06 +0200 |
---|---|---|
committer | cos <cos> | 2024-07-03 11:31:23 +0200 |
commit | 9d85f652318e9af393bb2389553c657e425ae0e9 (patch) | |
tree | 8a1a6f7b33e46f845b527c5c98c9087d6eb3c0c1 | |
parent | 262585c5dfb87170d2c136cf1c15d007e13eba52 (diff) | |
download | AntennaPodDbFixer-9d85f652318e9af393bb2389553c657e425ae0e9.zip |
Improve readability of both source code and output
-rwxr-xr-x | AntennaPodDbFixer.py | 51 |
1 files changed, 42 insertions, 9 deletions
diff --git a/AntennaPodDbFixer.py b/AntennaPodDbFixer.py index 56d77a4..dfc83ad 100755 --- a/AntennaPodDbFixer.py +++ b/AntennaPodDbFixer.py @@ -5,8 +5,15 @@ import shutil import os import sys +def usage(): + return sys.argv[0] + " [--help] [corrupt.db]" + if len(sys.argv) > 1: - inputPath = sys.argv[1] + if sys.argv[1] == "--help": + print(usage()) + exit(0) + else: + inputPath = sys.argv[1] else: inputPath = input("Enter file path to your corrupted AntennaPod database: ") @@ -21,7 +28,11 @@ if dbpage_ext != DBPAGE_SYM: print("https://sqlite.org/dbpage.html (required by .recover)", file=sys.stderr) exit(1) -corruptedVersion = subprocess.run(["sqlite3", inputPath, "PRAGMA user_version;"], capture_output=True, text=True).stdout.strip() +corruptedVersion = subprocess.run( + ["sqlite3", inputPath, "PRAGMA user_version;"], + capture_output=True, + text=True + ).stdout.strip() if corruptedVersion == "0": print("Error: File not found, not a database, or too corrupted for this script.") exit() @@ -29,8 +40,13 @@ print("Corrupted file version: " + corruptedVersion) emptyPath = "empty/" + corruptedVersion + ".db" if not os.path.isfile(emptyPath): - emptyPath = input("Enter file path to an EMPTY AntennaPod database with the same version. If needed, you can download old app versions on F-Droid and export an empty database: ") - emptyVersion = subprocess.run(["sqlite3", emptyPath, "PRAGMA user_version;"], capture_output=True, text=True).stdout.strip() + print("If needed, you can download old app versions on F-Droid and export an empty database.") + emptyPath = input("Enter file path to an EMPTY AntennaPod database with the same version: ") + emptyVersion = subprocess.run( + ["sqlite3", emptyPath, "PRAGMA user_version;"], + capture_output=True, + text=True + ).stdout.strip() print("Empty file version: " + emptyVersion) if corruptedVersion != emptyVersion: print("Error: Version does not match") @@ -48,12 +64,17 @@ if os.path.exists(corruptedPath): os.remove(corruptedPath) # Recover to SQL commands and insert back into a database print("Recovering database.") -subprocess.run(["sqlite3", inputPath, ".recover --ignore-freelist"], check=True, stdout=open(sqlPath, 'w')) +subprocess.run( + ["sqlite3", inputPath, ".recover --ignore-freelist"], + check=True, + stdout=open(sqlPath, 'w') + ) f = open(sqlPath,'r') filedata = f.read() f.close() f = open(sqlPath,'w') -f.write(filedata.replace("CREATE TABLE sqlite_sequence(name,seq);","")) # Avoid a warning that could be confusing to users +# Avoid a warning that could be confusing to users +f.write(filedata.replace("CREATE TABLE sqlite_sequence(name,seq);","")) f.close() subprocess.run(["sqlite3", corruptedPath], stdin=open(sqlPath, 'r')) print() @@ -63,7 +84,12 @@ print() def query(db, query): result = "" try: - result = subprocess.run(["sqlite3", "-json", db, query], capture_output=True, check=True, text=True).stdout + result = subprocess.run( + ["sqlite3", "-json", db, query], + capture_output=True, + check=True, + text=True + ).stdout return json.loads(result) except subprocess.CalledProcessError as err: print(err.stderr) @@ -75,10 +101,17 @@ tables = query(emptyPath, "SELECT name FROM sqlite_schema WHERE type='table';") for table in tables: table = table["name"] print("Copying " + table + "...") - columns = query(emptyPath, "SELECT GROUP_CONCAT(NAME,',') AS columns FROM PRAGMA_TABLE_INFO('" + table + "')")[0]["columns"] + columns = query( + emptyPath, + "SELECT GROUP_CONCAT(NAME,',') AS columns FROM PRAGMA_TABLE_INFO('" + table + "')" + )[0]["columns"] query(outputPath, "DELETE FROM " + table) - query(outputPath, "attach '" + corruptedPath + "' AS old; INSERT INTO main." + table + " SELECT " + columns + " from old." + table + ";") + query( + outputPath, + "attach '" + corruptedPath + "' AS old; INSERT INTO main." + table + " SELECT " + + columns + " from old." + table + ";" + ) print() # Cleanup |