Skip to content

Commit 43df00c

Browse files
committed
date_utils: make date specificity sorting safer
1 parent 29b14ed commit 43df00c

File tree

5 files changed

+40
-19
lines changed

5 files changed

+40
-19
lines changed

library/createdb/av.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -191,20 +191,25 @@ def munge_av_tags(args, m) -> dict:
191191
full_scan_if_corrupt=args.full_scan_if_corrupt,
192192
threads=1,
193193
)
194+
except (TimeoutError, subprocess.TimeoutExpired):
195+
log.error(f"FFProbe timed out. {path}")
196+
m["error"] = "FFProbe timed out"
194197
except Exception:
195198
print(path)
196199
raise
197-
198-
if media_check.corruption_threshold_exceeded(
199-
args.delete_corrupt, corruption, duration
200-
) and not file_utils.is_file_open(path):
201-
threshold_str = (
202-
strings.percent(args.delete_corrupt) if 0 < args.delete_corrupt < 1 else (args.delete_corrupt + "s")
203-
)
204-
log.warning("Deleting %s corruption %.1f%% exceeded threshold %s", path, corruption * 100, threshold_str)
205-
file_utils.trash(args, path, detach=False)
206-
m["time_deleted"] = consts.APPLICATION_START
207-
m["error"] = "Media check failed"
200+
else:
201+
if media_check.corruption_threshold_exceeded(
202+
args.delete_corrupt, corruption, duration
203+
) and not file_utils.is_file_open(path):
204+
threshold_str = (
205+
strings.percent(args.delete_corrupt) if 0 < args.delete_corrupt < 1 else (args.delete_corrupt + "s")
206+
)
207+
log.warning(
208+
"Deleting %s corruption %.1f%% exceeded threshold %s", path, corruption * 100, threshold_str
209+
)
210+
file_utils.trash(args, path, detach=False)
211+
m["time_deleted"] = consts.APPLICATION_START
212+
m["error"] = "Media check failed"
208213

209214
tags = format_.pop("tags", None)
210215
if tags:

library/createdb/tube_backend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import json, re, sys
1+
import json, re, subprocess, sys
22
from copy import deepcopy
33
from pathlib import Path
44
from subprocess import CalledProcessError
@@ -594,7 +594,7 @@ def blocklist_check(info, *pargs, incomplete):
594594
threads=args.same_file_threads,
595595
)
596596
info["corruption"] = int(corruption * 100)
597-
except (RuntimeError, CalledProcessError):
597+
except (RuntimeError, CalledProcessError, TimeoutError, subprocess.TimeoutExpired):
598598
info["corruption"] = 50
599599
if info["corruption"] > 7:
600600
media_check_failed = True

library/mediafiles/media_check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def media_check() -> None:
213213
corruption = future.result()
214214
print(strings.percent(corruption), shlex.quote(path), sep="\t")
215215
except Exception as excinfo:
216-
print(f"Error hashing {path}: {excinfo}")
216+
print(f"Error checking {path}: {excinfo}")
217217
if args.verbose >= consts.LOG_DEBUG:
218218
raise
219219
else:

library/utils/date_utils.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import dateutil.parser
55

6-
from library.utils import iterables, nums
6+
from library.utils import consts, iterables, nums
77

88

99
def is_tz_aware(dt: datetime.datetime) -> bool:
@@ -37,15 +37,26 @@ def super_parser(date_str, fallback=False):
3737
return None
3838

3939

40+
def date_specificity_sort_key(d):
41+
month_flag = bool(getattr(d, "month", None))
42+
day_flag = bool(getattr(d, "day", None))
43+
44+
try:
45+
ts = d.timestamp()
46+
ts_key = -ts
47+
except Exception: # fallback for invalid year/timestamp
48+
ts_key = consts.APPLICATION_START
49+
50+
return (month_flag, day_flag, ts_key)
51+
52+
4053
def specific_date(*dates):
4154
valid_dates = [super_parser(s) for s in dates if s]
4255
past_dates = [d for d in valid_dates if d and d < maybe_tz_now(d)]
4356
if not past_dates:
4457
return None
4558

46-
earliest_specific_date = sorted(
47-
past_dates, key=lambda d: (bool(d.month), bool(d.day), -d.timestamp()), reverse=True
48-
)[0]
59+
earliest_specific_date = sorted(past_dates, key=date_specificity_sort_key, reverse=True)[0]
4960
return nums.to_timestamp(earliest_specific_date)
5061

5162

library/utils/nums.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import math, re, statistics
22

3+
from library.utils import consts
4+
35

46
def percent(value, total):
57
if total == 0:
@@ -26,7 +28,10 @@ def percentage_difference(value1, value2):
2628

2729

2830
def to_timestamp(dt_object):
29-
return int(dt_object.timestamp())
31+
try:
32+
return int(dt_object.timestamp())
33+
except ValueError:
34+
return consts.APPLICATION_START
3035

3136

3237
def safe_int(s) -> int | None:

0 commit comments

Comments
 (0)