diff --git a/audiobook_metadata/metadata_edit_database.py b/audiobook_metadata/metadata_genre_edit_database.py similarity index 55% rename from audiobook_metadata/metadata_edit_database.py rename to audiobook_metadata/metadata_genre_edit_database.py index c07a1f8..1ff4754 100755 --- a/audiobook_metadata/metadata_edit_database.py +++ b/audiobook_metadata/metadata_genre_edit_database.py @@ -39,6 +39,11 @@ def main(): 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, @@ -56,9 +61,11 @@ def main(): cur = db.cursor() list_genres = load_genre(cur,True) + if args.print_all: + for genre in list_genres: + print(genre) + return - #for genre in list_genres: - # print(genre) rows = cur.execute( "SELECT id, genres FROM books" ).fetchall() @@ -67,34 +74,71 @@ def main(): 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)\bhörb(uch|ücher)\b', '', 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)^nonfiction$', 'NonFiction', 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() - new_genres.append(genre) + 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}") + 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("Write to Database") print(f"After sed: {new_genres}") - print("=================================") 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 @@ -109,6 +153,9 @@ def main(): continue print("Write to Database") + + + print("=================================") cur.execute( "UPDATE books SET genres=? WHERE id=?", (json.dumps(new_genres), book_id),