Skip to content

Commit

Permalink
re-use pre-dl media rows when no error
Browse files Browse the repository at this point in the history
  • Loading branch information
chapmanjacobd committed Jul 23, 2024
1 parent a23ae34 commit 40a0097
Show file tree
Hide file tree
Showing 22 changed files with 116 additions and 100 deletions.
8 changes: 2 additions & 6 deletions tests/utils/test_printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ def test_col_naturaldate():


def test_col_naturalsize():
assert printing.col_naturalsize([{"t": 0, "t1": 1}], "t") == [{"t": None, "t1": 1}]
assert printing.col_naturalsize([{"t": 946684800, "t1": 1}], "t") == [{"t": "902.8 MiB", "t1": 1}]
assert printing.col_filesize([{"t": 0, "t1": 1}], "t") == [{"t": None, "t1": 1}]
assert printing.col_filesize([{"t": 946684800, "t1": 1}], "t") == [{"t": "902.8 MiB", "t1": 1}]


def test_human_time():
assert printing.human_duration(0) == ""
assert printing.human_duration(946684800) == "30 years and 7 days"


def test_col_duration():
assert printing.col_duration([{"t": 0, "t1": 1}], "t") == [{"t": "", "t1": 1}]
Expand Down
4 changes: 4 additions & 0 deletions tests/utils/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,7 @@ def test_no_matching_string(self):

with pytest.raises(ValueError):
strings.partial_startswith(my_string, my_list)

def test_human_time():
assert strings.duration(0) == ""
assert strings.duration(946684800) == "30 years and 7 days"
2 changes: 1 addition & 1 deletion xklb/createdb/links_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def add_playlist(args, path):
"time_modified": 0,
"time_deleted": 0,
}
return db_playlists.add(args, str(path), info)
return db_playlists.add(args, str(path), info, extractor_key='LinksDB')


def consolidate_media(args, path: str) -> dict:
Expand Down
5 changes: 2 additions & 3 deletions xklb/editdb/dedupe_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from copy import deepcopy
from pathlib import Path

import humanize

from xklb import usage
from xklb.files import sample_compare, sample_hash
from xklb.mediadb import db_media
Expand All @@ -20,6 +18,7 @@
path_utils,
processes,
sql_utils,
strings,
)
from xklb.utils.consts import DBType
from xklb.utils.log_utils import log
Expand Down Expand Up @@ -491,7 +490,7 @@ def dedupe_media() -> None:
log.info("Skipping CSV export because pandas is not installed")

duplicates_size = sum(filter(None, [d["duplicate_size"] for d in duplicates]))
print(f"Approx. space savings: {humanize.naturalsize(duplicates_size // 2, binary=True)}")
print(f"Approx. space savings: {strings.file_size(duplicates_size // 2)}")

if duplicates and (args.force or devices.confirm("Delete duplicates?")): # type: ignore
log.info("Deleting...")
Expand Down
7 changes: 4 additions & 3 deletions xklb/files/similar_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

from xklb import usage
from xklb.folders.similar_folders import cluster_folders, map_and_name
from xklb.utils import arg_utils, arggroups, argparse_utils, file_utils, nums, printing
from xklb.utils import arg_utils, arggroups, argparse_utils, file_utils, nums, printing, strings
from xklb.utils.log_utils import log



def parse_args():
parser = argparse_utils.ArgumentParser(usage=usage.similar_files)
arggroups.cluster(parser)
Expand Down Expand Up @@ -144,8 +145,8 @@ def similar_files():
[
{
f'group {group["common_path"]}': d["path"],
"size": humanize.naturalsize(d["size"], binary=True),
"duration": printing.human_duration(d.get("duration")),
"size": strings.file_size(d["size"]),
"duration": strings.duration(d.get("duration")),
}
for d in media
]
Expand Down
10 changes: 5 additions & 5 deletions xklb/folders/move_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import humanize

from xklb import usage
from xklb.utils import arggroups, argparse_utils, consts, devices, iterables, printing, sqlgroups
from xklb.utils import arggroups, argparse_utils, consts, devices, iterables, printing, sqlgroups, strings


def parse_args() -> argparse.Namespace:
Expand Down Expand Up @@ -67,7 +67,7 @@ def iterate_and_show_options(args, tbl) -> tuple[list[dict], list[dict]]:

view = iterables.list_dict_filter_bool(view, keep_0=False)
view = printing.col_resize_percent(view, "path", 60)
view = printing.col_naturalsize(view, "size")
view = printing.col_filesize(view, "size")
printing.table(tbl)
print(len(tbl) - len(view), "other folders not shown")

Expand Down Expand Up @@ -99,7 +99,7 @@ def move_list() -> None:
args = parse_args()
_total, _used, free = shutil.disk_usage(args.target_mount_point)

print("Current free space in target:", humanize.naturalsize(free))
print("Current free space in target:", strings.file_size(free))

data = get_table(args)

Expand Down Expand Up @@ -168,9 +168,9 @@ def move_list() -> None:
print(
len(selected_paths),
"selected paths:",
humanize.naturalsize(selected_paths_size, binary=True),
strings.file_size(selected_paths_size),
"; future free space:",
humanize.naturalsize(selected_paths_size + free, binary=True),
strings.file_size(selected_paths_size + free)
)

if selected_paths:
Expand Down
4 changes: 2 additions & 2 deletions xklb/folders/scatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ def get_path_stats(args, data) -> list[dict]:

def print_path_stats(tbl) -> None:
tbl = iterables.list_dict_filter_bool(tbl, keep_0=False)
tbl = printing.col_naturalsize(tbl, "total_size")
tbl = printing.col_naturalsize(tbl, "median_size")
tbl = printing.col_filesize(tbl, "total_size")
tbl = printing.col_filesize(tbl, "median_size")
for t in consts.EPOCH_COLUMNS:
printing.col_naturaldate(tbl, t)

Expand Down
10 changes: 5 additions & 5 deletions xklb/folders/similar_folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ def similar_folders():
[
{
f'group {group["common_path"]}': d["path"],
"total_size": humanize.naturalsize(d["size"], binary=True),
"median_size": humanize.naturalsize(d["median_size"], binary=True),
"total_duration": printing.human_duration(d.get("duration")),
"median_duration": printing.human_duration(d.get("median_duration")),
"median_size": humanize.naturalsize(d.get("median_size"), binary=True),
"total_size": strings.file_size(d["size"]),
"median_size": strings.file_size(d["median_size"]),
"total_duration": strings.duration(d.get("duration")),
"median_duration": strings.duration(d.get("median_duration")),
"median_size": strings.file_size(d.get("median_size")),
"files": d["exists"],
}
for d in media
Expand Down
6 changes: 3 additions & 3 deletions xklb/mediadb/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from xklb import usage
from xklb.createdb import tube_backend
from xklb.playback import media_printer, post_actions
from xklb.utils import arg_utils, arggroups, argparse_utils, consts, db_utils, devices, iterables
from xklb.utils import arg_utils, arggroups, argparse_utils, consts, db_utils, devices, iterables, strings
from xklb.utils.log_utils import log


Expand Down Expand Up @@ -181,7 +181,7 @@ def block(args=None) -> None:
if paths_to_delete:
print(paths_to_delete)
if devices.confirm(
f"Would you like to delete these {len(paths_to_delete)} local files ({humanize.naturalsize(total_size, binary=True)})?",
f"Would you like to delete these {len(paths_to_delete)} local files ({strings.file_size(total_size)})?",
):
post_actions.delete_media(args, paths_to_delete)
return
Expand Down Expand Up @@ -265,7 +265,7 @@ def block(args=None) -> None:
total_size = sum(d["size"] or 0 for d in matching_media if (d["time_deleted"] or 0) == 0)
print("\n".join(local_paths_to_delete))
if devices.confirm(
f"Would you like to delete these {len(local_paths_to_delete)} local files ({humanize.naturalsize(total_size, binary=True)})?",
f"Would you like to delete these {len(local_paths_to_delete)} local files ({strings.file_size(total_size)})?",
):
post_actions.delete_media(args, local_paths_to_delete)

Expand Down
3 changes: 3 additions & 0 deletions xklb/mediadb/db_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ def add(args, entry):
entry.pop("description", None)

media_id = args.db.pop("select id from media where path = ?", [entry["path"]])
if not media_id and 'webpath' in entry and not entry.get('errors'):
media_id = args.db.pop("select id from media where path = ?", [entry["webpath"]])

try:
if media_id:
entry["id"] = media_id
Expand Down
6 changes: 3 additions & 3 deletions xklb/mediadb/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
consts,
db_utils,
iterables,
printing,
strings,
processes,
sql_utils,
web,
Expand Down Expand Up @@ -196,15 +196,15 @@ def download(args=None) -> None:
log.info(
"[%s]: Download marked deleted (%s ago). Skipping!",
m["path"],
printing.human_duration(consts.now() - d["time_deleted"]),
strings.duration(consts.now() - d["time_deleted"]),
)
mark_download_attempt(args, [m["path"]])
continue
elif d["time_modified"]:
log.info(
"[%s]: Download already attempted recently (%s ago). Skipping!",
m["path"],
printing.human_duration(consts.now() - d["time_modified"]),
strings.duration(consts.now() - d["time_modified"]),
)
continue

Expand Down
2 changes: 1 addition & 1 deletion xklb/mediadb/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def playlists() -> None:
query = f"""
select
coalesce(p.path, "Playlist-less media") path
, p.extractor_key
{', p.extractor_key' if 'extractor_key' in pl_columns else ", 'Playlist-less media' as extractor_key"}
{', p.title' if 'title' in pl_columns else ''}
{', p.time_deleted' if 'time_deleted' in pl_columns else ''}
{', count(*) FILTER(WHERE play_count>0) play_count' if 'play_count' in m_columns else ''}
Expand Down
10 changes: 5 additions & 5 deletions xklb/misc/dedupe_czkawka.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from xklb import usage
from xklb.playback import media_player, post_actions
from xklb.utils import arggroups, argparse_utils, consts, devices, file_utils, iterables, mpv_utils, processes
from xklb.utils import arggroups, argparse_utils, consts, devices, file_utils, iterables, mpv_utils, processes, strings
from xklb.utils.log_utils import log

left_mpv_socket = str(Path(consts.TEMP_SCRIPT_DIR) / f"mpv_socket_{consts.random_string()}")
Expand Down Expand Up @@ -241,14 +241,14 @@ def mv_to_keep_folder(args, d) -> None:

src_size = d["size"]
dst_size = new_path.stat().st_size
diff_size = humanize.naturalsize(src_size - dst_size, binary=True)
diff_size = strings.file_size(src_size - dst_size)

if src_size > dst_size:
print("Source is larger than destination", diff_size)
elif src_size < dst_size:
print("Source is smaller than destination", diff_size)
else:
print("Source and destination are the same size", humanize.naturalsize(src_size, binary=True))
print("Source and destination are the same size", strings.file_size(src_size))
if devices.confirm("Replace destination file?"):
file_utils.trash(args, new_path, detach=False)
new_path = shutil.move(media_file, keep_path)
Expand Down Expand Up @@ -291,8 +291,8 @@ def delete_dupe(d, detach=is_interactive):
left = right
continue

print(left["path"], humanize.naturalsize(left["size"], binary=True))
print(right["path"], humanize.naturalsize(right["size"], binary=True))
print(left["path"], strings.file_size(left["size"]))
print(right["path"], strings.file_size(right["size"]))

if args.auto_select_min_ratio < 1.0:
similar_ratio = difflib.SequenceMatcher(
Expand Down
4 changes: 2 additions & 2 deletions xklb/playback/media_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def media_printer(args, data, units=None, media_len=None) -> None:
if not any([args.to_json, "f" in print_args]):
for k, v in list(media[0].items()):
if k.endswith("size"):
printing.col_naturalsize(media, k)
printing.col_filesize(media, k)
elif k.endswith("duration") or k in ("playhead",):
printing.col_duration(media, k)
elif k.startswith("time_") or "_time_" in k:
Expand Down Expand Up @@ -268,7 +268,7 @@ def media_printer(args, data, units=None, media_len=None) -> None:
)

if total_duration > 0:
total_duration = printing.human_duration(total_duration)
total_duration = strings.duration(total_duration)
if "a" not in print_args:
print("Total duration:", total_duration)

Expand Down
2 changes: 1 addition & 1 deletion xklb/text/extract_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get_text(args, url):
is_error = False
if not args.local_html and not url.startswith("http") and Path(url).is_file():
text = fs_add.munge_book_tags_fast(url)
if text:
if text and text.get("tags"):
yield text.get("tags").replace(";", "\n")
return None

Expand Down
4 changes: 4 additions & 0 deletions xklb/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def play(action) -> str:
echo 'playlist-next force' | socat - {consts.DEFAULT_MPV_LISTEN_SOCKET} # library listen default
echo 'playlist-next force' | socat - {consts.DEFAULT_MPV_WATCH_SOCKET} # library watch default
If you prefer you can also send mpv the playlist, but this is incompatible with post-actions
mpv --playlist=(lb wt videos.db --ext mp4 -l 50 -p fw | psub) # fish shell, mark 50 videos as watched
mpv --playlist=<(lb wt videos.db --ext mp4 -p f) # BASH, all videos
Print an aggregate report of deleted media
library fs -w time_deleted!=0 -pa
Expand Down
2 changes: 1 addition & 1 deletion xklb/utils/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def reddit_frequency(frequency) -> str:

SPEECH_RECOGNITION_EXTENSIONS = set("mp3|ogg|wav".split("|"))
OCR_EXTENSIONS = set("gif|jpg|jpeg|png|tif|tff|tiff".split("|"))
AUDIO_ONLY_EXTENSIONS = set("mka|opus|oga|ogg|mp3|mpga|m2a|m4a|m4b|flac|wav|wma|aac|aa3|ac3|ape|mid|midi".split("|"))
AUDIO_ONLY_EXTENSIONS = set("mka|opus|oga|ogg|mp3|mpga|m2a|m4a|m4b|flac|wav|aif|aiff|wma|aac|aa3|ac3|ape|mid|midi".split("|"))
VIDEO_EXTENSIONS = set(
(
"str|aa|aax|acm|adf|adp|dtk|ads|ss2|adx|aea|afc|aix|al|apl|avifs|gif|gifv"
Expand Down
8 changes: 4 additions & 4 deletions xklb/utils/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import humanize

from xklb.files import sample_compare
from xklb.utils import arggroups, consts, file_utils
from xklb.utils import arggroups, consts, file_utils, strings
from xklb.utils.log_utils import log


Expand Down Expand Up @@ -90,9 +90,9 @@ def rmtree(args, p):


def log_size_diff(src_size, dst_size):
src_size_str = humanize.naturalsize(src_size, binary=True)
dst_size_str = humanize.naturalsize(dst_size, binary=True)
diff_size_str = humanize.naturalsize(abs(src_size - dst_size), binary=True)
src_size_str = strings.file_size(src_size)
dst_size_str = strings.file_size(dst_size)
diff_size_str = strings.file_size(abs(src_size - dst_size))
if src_size == dst_size:
print(f"Source and destination are the same size: {src_size_str}")
elif src_size > dst_size:
Expand Down
Loading

0 comments on commit 40a0097

Please sign in to comment.