Compare commits
2
Commits
e83741286c
...
3d585d14e0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d585d14e0 | ||
|
|
8b6fc7fdcd |
No files matched your search
@@ -43,7 +43,10 @@ def ffmpeg_escape(path: Path) -> str:
|
||||
return str(path.resolve()).replace("'", r"'\''")
|
||||
|
||||
def create_m4b(root_path: Path, book_dir: Path) -> None:
|
||||
media_files = sorted(book_dir.glob("*.m[p,4][3,b,a]"))
|
||||
media_files = sorted(list(book_dir.glob("*.mp3"))
|
||||
+ list(book_dir.glob("*.m4a"))
|
||||
+ list(book_dir.glob("*.m4b"))
|
||||
+ list(book_dir.glob("*.opus")))
|
||||
if not media_files:
|
||||
return
|
||||
|
||||
@@ -152,6 +155,7 @@ def find_book_dirs(root: Path) -> tuple[list[Any], int]:
|
||||
list(root.rglob("*.mp3"))
|
||||
+ list(root.rglob("*.m4a"))
|
||||
+ list(root.rglob("*.m4b"))
|
||||
+ list(root.rglob("*.opus"))
|
||||
)
|
||||
|
||||
counts: dict[Path, int] = {}
|
||||
@@ -166,7 +170,7 @@ def find_book_dirs(root: Path) -> tuple[list[Any], int]:
|
||||
for directory, count in counts.items():
|
||||
if count > 1:
|
||||
directories.append(directory)
|
||||
size += sum(file.stat().st_size for pattern in ("*.mp3", "*.m4a", "*.m4b") for file in directory.glob(pattern))
|
||||
size += sum(file.stat().st_size for pattern in ("*.mp3", "*.m4a", "*.m4b", "*.opus") for file in directory.glob(pattern))
|
||||
|
||||
|
||||
return sorted(directories), size
|
||||
@@ -179,7 +183,6 @@ def process_book(args):
|
||||
except Exception as exc:
|
||||
return book_dir, str(exc)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
@@ -196,7 +199,10 @@ def main() -> None:
|
||||
raise SystemExit(f"Path does not exist: {target}")
|
||||
|
||||
# Direct book folder
|
||||
if any(target.glob("*.m[p,4][3,b]")):
|
||||
if any(list(target.glob("*.mp3"))
|
||||
+ list(target.glob("*.m4a"))
|
||||
+ list(target.glob("*.m4b"))
|
||||
+ list(target.glob("*.opus"))):
|
||||
create_m4b(target.parent,target)
|
||||
return
|
||||
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
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(
|
||||
"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)
|
||||
|
||||
for genre in list_genres:
|
||||
print(genre)
|
||||
|
||||
for book_id, genres_json in cur.execute("SELECT id, genres FROM books"):
|
||||
if not genres_json:
|
||||
continue
|
||||
|
||||
genres = json.loads(genres_json)
|
||||
new_genres = []
|
||||
|
||||
for genre in genres:
|
||||
genre = re.sub(r'(?i)^audio\s*books?$', '', genre)
|
||||
genre = re.sub(r'(?i)^audiobooks?$', '', genre)
|
||||
genre = re.sub(r'(?i)^hörb(uch|ücher)$', '', genre)
|
||||
genre = re.sub(r'(?i)^hörspiele?$', '', genre)
|
||||
genre = re.sub(r'(?i)^spoken\s*word$', '', genre)
|
||||
genre = re.sub(r'(?i)^speech$', '', genre)
|
||||
|
||||
genre = re.sub(r'(?i)^(sci[- ]?fi|sf)$', 'Science Fiction', genre)
|
||||
genre = re.sub(r'(?i)^nonfiction$', 'NonFiction', genre)
|
||||
|
||||
genre = genre.strip()
|
||||
|
||||
if genre:
|
||||
new_genres.append(genre)
|
||||
|
||||
new_genres = sorted(set(new_genres))
|
||||
|
||||
cur.execute(
|
||||
"UPDATE books SET genres=? WHERE id=?",
|
||||
(json.dumps(new_genres), book_id),
|
||||
)
|
||||
|
||||
db.commit()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -295,52 +295,6 @@ def rename_media(media_data,episodes, series_path,file_title):
|
||||
print("Cancelled.")
|
||||
return
|
||||
|
||||
def process_series(series_path: Path):
|
||||
series = series_path.name
|
||||
|
||||
m = re.match(r"^(.*?)\s*\((\d{4})\)\s*\[(\d+)\]$", series)
|
||||
|
||||
if not m:
|
||||
print(f"Skipping invalid folder: {series}")
|
||||
return
|
||||
|
||||
file_title = m.group(1).strip()
|
||||
file_year = int(m.group(2))
|
||||
file_tmdb_id = int(m.group(3))
|
||||
|
||||
print()
|
||||
print("=" * 80)
|
||||
print(f"TITLE={file_title}")
|
||||
print(f"YEAR={file_year}")
|
||||
print(f"TMDB_ID={file_tmdb_id}")
|
||||
|
||||
if file_tmdb_id == 0:
|
||||
print("TMDB ID required")
|
||||
return
|
||||
|
||||
try:
|
||||
data = find_by_tmdb_id(file_tmdb_id)
|
||||
|
||||
if not data:
|
||||
print("TMDB lookup failed")
|
||||
return
|
||||
|
||||
media_type, media_data = data
|
||||
|
||||
print_results(data)
|
||||
|
||||
episodes = get_episode_list(media_data)
|
||||
|
||||
rename_media(
|
||||
media_data,
|
||||
episodes,
|
||||
series_path,
|
||||
file_title
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error processing {series}: {e}")
|
||||
|
||||
def process_series(series_path: Path):
|
||||
series = series_path.name
|
||||
|
||||
|
||||
Reference in new issue
Block a user