Compare commits

..

No commits in common. "master" and "2.0.1" have entirely different histories.

6 changed files with 46 additions and 78 deletions

View File

@ -165,13 +165,11 @@ Several command line options are available:
- `--reset-db`: Reset the database (remove all stored file hashes)
- `--list-patterns`: List all advertisement patterns being used
- `--version`: Show version information and exit
- `-v`, `--verbose`: Increase output verbosity (show analyzing/skipping messages)
Example usage:
```sh
find /your/media/location -name "*.srt" | subscleaner --force
find /your/media/location -name "*.srt" | subscleaner --db-location /path/to/custom/database.db
find /your/media/location -name "*.srt" | subscleaner --verbose
```
This feature makes Subscleaner more efficient, especially when running regularly via cron jobs or other scheduled tasks, as it will only process new or modified subtitle files.

View File

@ -1,6 +1,6 @@
[project]
name = "subscleaner"
version = "2.1.1"
version = "2.0.1"
description = "Remove advertisements from subtitle files"
authors = [
{name = "Roger Gonzalez", email = "roger@rogs.me"}

View File

@ -1,3 +1,3 @@
"""Subscleaner package."""
__version__ = "2.1.1"
__version__ = "1.3.0"

View File

@ -270,7 +270,7 @@ def remove_ad_lines(subtitle_data: pysrt.SubRipFile) -> bool:
return modified
def is_already_processed(subtitle_file, db_path, file_hash, force=False, verbose=False):
def is_already_processed(subtitle_file, db_path, file_hash, force=False):
"""
Check if the subtitle file has already been processed.
@ -290,14 +290,13 @@ def is_already_processed(subtitle_file, db_path, file_hash, force=False, verbose
# Check if the file is in the database with the same hash
if is_file_processed(db_path, str(subtitle_file), file_hash):
if verbose:
print(f"Already processed {subtitle_file} (hash match)")
return True
return False
def process_subtitle_file(subtitle_file_path: str, db_path, force=False, verbose=False) -> bool:
def process_subtitle_file(subtitle_file_path: str, db_path, force=False) -> bool:
"""
Process a subtitle file to remove ad lines.
@ -305,14 +304,12 @@ def process_subtitle_file(subtitle_file_path: str, db_path, force=False, verbose
subtitle_file_path (str): The path to the subtitle file.
db_path (pathlib.Path): The path to the database file.
force (bool): If True, process the file even if it has been processed before.
verbose (bool): If True, print detailed processing information.
Returns:
bool: True if the subtitle file was modified, False otherwise.
"""
try:
subtitle_file = pathlib.Path(subtitle_file_path)
if verbose:
print(f"Analyzing: {subtitle_file}")
# Early validation checks
@ -362,7 +359,7 @@ def process_subtitle_file(subtitle_file_path: str, db_path, force=False, verbose
return False
def process_subtitle_files(subtitle_files: list[str], db_path, force=False, verbose=False) -> list[str]:
def process_subtitle_files(subtitle_files: list[str], db_path, force=False) -> list[str]:
"""
Process multiple subtitle files to remove ad lines.
@ -370,20 +367,25 @@ def process_subtitle_files(subtitle_files: list[str], db_path, force=False, verb
subtitle_files (list[str]): A list of subtitle file paths.
db_path (pathlib.Path): The path to the database file.
force (bool): If True, process files even if they have been processed before.
verbose (bool): If True, print detailed processing information.
Returns:
list[str]: A list of modified subtitle file paths.
"""
modified_files = []
for subtitle_file in subtitle_files:
if process_subtitle_file(subtitle_file, db_path, force, verbose):
if process_subtitle_file(subtitle_file, db_path, force):
modified_files.append(subtitle_file)
return modified_files
def _parse_args():
"""Parse command line arguments."""
def main():
"""
Process subtitle files to remove ad lines.
Read subtitle file paths from standard input, process each file to remove ad lines,
and print the result. Keep track of the modified files and print
a summary at the end.
"""
parser = argparse.ArgumentParser(description="Remove advertisements from subtitle files.")
parser.add_argument(
"--db-location",
@ -394,17 +396,10 @@ def _parse_args():
parser.add_argument("--version", action="store_true", help="Show version information and exit")
parser.add_argument("--reset-db", action="store_true", help="Reset the database (remove all stored file hashes)")
parser.add_argument("--list-patterns", action="store_true", help="List all advertisement patterns being used")
parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="Increase output verbosity (show analyzing/skipping messages)",
)
return parser.parse_args()
args = parser.parse_args()
def _print_version():
"""Print the application version."""
# Handle version request
if args.version:
try:
from subscleaner import __version__
@ -414,10 +409,13 @@ def _print_version():
version = importlib.metadata.version("subscleaner")
print(f"Subscleaner version {version}")
return
# Get database path
db_path = get_db_path(args.db_location)
def _reset_database(db_path):
"""Reset the database file."""
# Handle reset database request
if args.reset_db:
if db_path.exists():
try:
db_path.unlink()
@ -426,43 +424,16 @@ def _reset_database(db_path):
print(f"Error resetting database: {e}")
else:
print(f"No database found at {db_path}")
def _list_patterns():
"""List the configured ad patterns."""
print("Advertisement patterns being used:")
for i, pattern in enumerate(AD_PATTERNS, 1):
print(f"{i}. {pattern.pattern}")
def main():
"""
Run the main entry point for the Subscleaner script.
Parse arguments, handle special commands like version or reset-db,
and processes subtitle files provided via stdin.
"""
args = _parse_args()
# Handle version request
if args.version:
_print_version()
return
# Get database path
db_path = get_db_path(args.db_location)
# Handle reset database request
if args.reset_db:
_reset_database(db_path)
return
# Handle list patterns request
if args.list_patterns:
_list_patterns()
print("Advertisement patterns being used:")
for i, pattern in enumerate(AD_PATTERNS, 1):
print(f"{i}. {pattern.pattern}")
return
# Initialize database if not resetting
# Initialize database
init_db(db_path)
# Process subtitle files
@ -471,9 +442,8 @@ def main():
print("No subtitle files provided. Pipe filenames to subscleaner or use --help for more information.")
return
if args.verbose:
print("Starting script")
modified_files = process_subtitle_files(subtitle_files, db_path, args.force, args.verbose)
modified_files = process_subtitle_files(subtitle_files, db_path, args.force)
if modified_files:
print(f"Modified {len(modified_files)} files")
print("Done")

View File

@ -191,8 +191,8 @@ def test_process_subtitle_files(tmpdir, sample_srt_content, mock_db_path):
assert modified_subtitle_files == [subtitle_file1]
assert mock_process.call_count == 2 # noqa PLR2004
# Check that db_path was passed to process_subtitle_file
mock_process.assert_any_call(subtitle_file1, mock_db_path, False, False)
mock_process.assert_any_call(subtitle_file2, mock_db_path, False, False)
mock_process.assert_any_call(subtitle_file1, mock_db_path, False)
mock_process.assert_any_call(subtitle_file2, mock_db_path, False)
def test_main_no_modification(tmpdir, sample_srt_content):
@ -213,7 +213,7 @@ def test_main_no_modification(tmpdir, sample_srt_content):
patch("src.subscleaner.subscleaner.process_subtitle_files", return_value=[]) as mock_process_subtitle_files,
):
main()
mock_process_subtitle_files.assert_called_once_with([subtitle_file], Path("/tmp/test_db.db"), False, False)
mock_process_subtitle_files.assert_called_once_with([subtitle_file], Path("/tmp/test_db.db"), False)
def test_main_with_modification(tmpdir, sample_srt_content):
@ -237,7 +237,7 @@ def test_main_with_modification(tmpdir, sample_srt_content):
) as mock_process_subtitle_files,
):
main()
mock_process_subtitle_files.assert_called_once_with([subtitle_file], Path("/tmp/test_db.db"), False, False)
mock_process_subtitle_files.assert_called_once_with([subtitle_file], Path("/tmp/test_db.db"), False)
def test_process_files_with_special_chars(special_chars_temp_dir, sample_srt_content, mock_db_path):
@ -366,4 +366,4 @@ def test_main_with_special_chars(special_chars_temp_dir, sample_srt_content):
) as mock_process_subtitle_files,
):
main()
mock_process_subtitle_files.assert_called_once_with([str(file_path)], Path("/tmp/test_db.db"), False, False)
mock_process_subtitle_files.assert_called_once_with([str(file_path)], Path("/tmp/test_db.db"), False)

2
uv.lock generated
View File

@ -456,7 +456,7 @@ wheels = [
[[package]]
name = "subscleaner"
version = "2.1.1"
version = "2.0.0"
source = { editable = "." }
dependencies = [
{ name = "appdirs" },