#!/usr/bin/python3 import json import re import sqlite3 import argparse from pathlib import Path def load_genre(conn, unique=False): """ Return all genres from the books table. Args: conn: sqlite3 connection unique: if True, return unique genres only Returns: list[str] """ genres = [] for (genres_json,) in conn.execute("SELECT genres FROM books"): if not genres_json: continue try: genres.extend(json.loads(genres_json)) except Exception: pass if unique: return sorted(set(genres)) return genres def main(): parser = argparse.ArgumentParser() parser.add_argument( "-p", "--print-only", action="store_true", help="Print data only", ) parser.add_argument( "--print-all", action="store_true", help="Print all data only", ) parser.add_argument( "database", type=Path, help="SQL Database Path", ) args = parser.parse_args() database = args.database.resolve() if not database.is_file(): parser.error(f"Database is not a file: {database}") db = sqlite3.connect(database) cur = db.cursor() list_genres = load_genre(cur,True) if args.print_all: for genre in list_genres: print(genre) return rows = cur.execute( "SELECT id, genres FROM books" ).fetchall() for book_id, genres_json in rows: if not genres_json: continue genres = json.loads(genres_json) genres_new = [] new_genres = [] print("=================================") print(f"Before sed: {genres}") for genre in genres: if "Drachen" in genre: pass if "Fantastic" in genre: pass genre = re.sub(r'(?i)^audio\s*books?$', '', genre) genre = re.sub(r'(?i)^audiobooks?$', '', genre) genre = re.sub(r'(?i)\b(hörb(uch|ücher)|hoerb(uch|uecher))\b', '', genre) genre = re.sub(r'(?i)^(hörspiele?|hoerspiele?)$', '', genre) genre = re.sub(r'(?i)^spoken\s*word$', '', genre) genre = re.sub(r'(?i)gesprochene\s*inhalte$', '', genre) genre = re.sub(r'(?i)^speech$', '', genre) genre = re.sub(r'Fanasie', 'Fantasie', genre) genre = re.sub(r'Mit Videospiel', 'Videospielbezug', genre) genre = re.sub(r'(?i)nonfiction', 'NonFiction', genre) genre = re.sub(r'(?i)^speech$', '', genre) genre = re.sub(r'(?i)^vocal$', '', genre) genre = re.sub(r'(?i)^retail$', '', genre) genre = re.sub(r'(?i)^other$', '', genre) genre = re.sub(r'(?i)^misc$', '', genre) genre = re.sub(r'(?i)^(sci[- ]?fi|sf)$', 'Science Fiction', genre) genre = re.sub(r'(?i)^\(?science fiction\)?$', 'Science Fiction', genre) genre = re.sub(r'Thrillers', 'Thriller', genre) genre = re.sub(r"(?i)\bchildren'?s audiobooks?\b", "",genre) genre = re.sub(r'^-\s*|\s*-$', '', genre) genre = re.sub(r'(?i)Engelske\s*\sLydbånd','',genre) genre = re.sub(r'^FIC028000.*$', '', genre) genre = re.sub(r'^800.*$', '', genre) genre = re.sub(r'^Fantastic Beasts.*$', '', genre) genre = re.sub(r'^and thei.*$', '', genre) genre = re.sub(r'^those you.*$', '', genre) genre = genre.strip() genre = genre.lstrip() if "," in genre: genre = genre.split(",") elif " - " in genre: genre = genre.split(" - ") elif "Historical Adventure Thriller Mystery" in genre: genre = genre.split(" ") if isinstance(genre, str): if len(genre) > 50 or (5 > len(genre) > 0): genre = "" print(f"Too long or too short: {genre}") genre = genre.strip() genre = genre.lstrip() new_genres.append(genre) elif isinstance(genre, list): if len(genres) > 1: genres = [g for g in genres if g] new_genres.extend(genre) new_genres = sorted(set(new_genres)) print(f"After sed: {new_genres}") list_difference = False print("Write to Database") if len(new_genres) < len(genres): list_difference = True for old,new in zip(genres,new_genres): if old != new: list_difference = True break if not list_difference: print("No difference: Dont Commit to Database") continue if args.print_only: print("Print only: Dont Commit to database") continue print("Write to Database") print("=================================") cur.execute( "UPDATE books SET genres=? WHERE id=?", (json.dumps(new_genres), book_id), ) print("Database Updated") db.commit() if __name__ == "__main__": main()