Skip to content

Commit

Permalink
2.2.044
Browse files Browse the repository at this point in the history
  • Loading branch information
chapmanjacobd committed Jul 26, 2023
1 parent 051b5e4 commit db94f2c
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 17 deletions.
11 changes: 9 additions & 2 deletions README.md
Expand Up @@ -143,7 +143,7 @@ Incremental surfing. 📈🏄 totally rad!
<details><summary>List all subcommands</summary>

$ library
xk media library subcommands (v2.2.043)
xk media library subcommands (v2.2.044)

local media:
lb fsadd Create a local media database; Add folders
Expand Down Expand Up @@ -530,7 +530,7 @@ Explore `library` databases in your browser

Adding one URL:

library tabsadd -f monthly -c travel tabs.db https://old.reddit.com/r/Colombia/top/?sort=top&t=month
library tabsadd -f daily tabs.db https://wiby.me/surprise/

Depending on your shell you may need to escape the URL (add quotes)

Expand Down Expand Up @@ -794,6 +794,13 @@ Explore `library` databases in your browser
library watch --sort 'ntile(10000) over (order by size/duration) desc'
library watch -u 'ntile(100) over (order by size) desc'

Sort by count of media with the same columnX (most common to least common value)
library watch -u same-duration
library watch -u same-title
library watch -u same-size
library watch -u same-width, same-height, same-fps
library watch -u same-time_uploaded same-view_count same-upvote_ratio

Post-actions -- choose what to do after playing:
library watch --post-action keep # do nothing after playing (default)
library watch -k delete # delete file after playing
Expand Down
2 changes: 1 addition & 1 deletion xklb/__init__.py
@@ -1 +1 @@
__version__ = "2.2.043"
__version__ = "2.2.044"
2 changes: 1 addition & 1 deletion xklb/lb.py
Expand Up @@ -238,7 +238,7 @@ def create_subcommands_parser() -> argparse.ArgumentParser:
subp_playback_next.set_defaults(func=playback_next)
subp_playback_stop = add_parser(subparsers, "stop")
subp_playback_stop.set_defaults(func=playback_stop)
subp_playback_pause = add_parser(subparsers, "pause")
subp_playback_pause = add_parser(subparsers, "pause", ["play"])
subp_playback_pause.set_defaults(func=playback_pause)

subp_tabsadd = add_parser(subparsers, "tabsadd")
Expand Down
26 changes: 23 additions & 3 deletions xklb/play_actions.py
Expand Up @@ -15,7 +15,27 @@

def parse_args_sort(args) -> None:
if args.sort:
args.sort = " ".join(args.sort)
combined_sort = []
for s in utils.flatten([s.split(",") for s in args.sort]):
if s.strip() in ["asc", "desc"] and combined_sort:
combined_sort[-1] += " " + s.strip()
else:
combined_sort.append(s.strip())

sort_list = []
select_list = []
for s in combined_sort:
if s.startswith("same-"):
var = s[len("same-") :]
select_list.append(
f"CASE WHEN {var} IS NULL THEN NULL ELSE COUNT(*) OVER (PARTITION BY {var}) END AS same_{var}_count"
)
sort_list.append(f"same_{var}_count DESC")
else:
sort_list.append(s)

args.sort = sort_list
args.select = select_list
elif not args.sort and hasattr(args, "defaults"):
args.defaults.append("sort")

Expand Down Expand Up @@ -46,7 +66,7 @@ def parse_args_sort(args) -> None:
],
)
else None,
args.sort,
*(args.sort or []),
"duration desc" if args.action in (SC.listen, SC.watch) and args.include else None,
"size desc" if args.action in (SC.listen, SC.watch) and args.include else None,
"play_count" if args.action in (SC.listen, SC.watch) else None,
Expand Down Expand Up @@ -403,7 +423,7 @@ def ii(string):
aggregate_filter_columns = ["time_first_played", "time_last_played", "play_count", "playhead"]

cols = args.cols or ["path", "title", "duration", "size", "subtitle_count", "is_dir", "rank"]
args.select = [c for c in cols if c in m_columns or c in ["*"]]
args.select = [c for c in cols if c in m_columns or c in ["*"]] + getattr(args, "select", [])
if args.action == SC.read and "tags" in m_columns:
args.select += "cast(length(tags) / 4.2 / 220 * 60 as INT) + 10 duration"
args.select_sql = "\n , ".join(args.select)
Expand Down
16 changes: 9 additions & 7 deletions xklb/player.py
Expand Up @@ -1053,9 +1053,11 @@ def media_printer(args, data, units=None, media_len=None) -> None:
if units is None:
units = "media"

cols = getattr(args, "cols", [])

media = deepcopy(data)

if args.verbose >= consts.LOG_DEBUG and args.cols and "*" in args.cols:
if args.verbose >= consts.LOG_DEBUG and cols and "*" in cols:
breakpoint()

if not media:
Expand Down Expand Up @@ -1088,8 +1090,8 @@ def media_printer(args, data, units=None, media_len=None) -> None:
D["size"] = sum((d["size"] or 0) for d in media)
D["avg_size"] = sum((d["size"] or 0) for d in media) / len(media)

if args.cols:
for c in args.cols:
if cols:
for c in cols:
if isinstance(media[0][c], Number):
D[f"sum_{c}"] = sum((d[c] or 0) for d in media)
D[f"avg_{c}"] = sum((d[c] or 0) for d in media) / len(media)
Expand Down Expand Up @@ -1146,13 +1148,13 @@ def should_align_right(k, v):
if len(media) == 0:
raise FileNotFoundError

if not args.cols:
args.cols = ["path"]
if not cols:
cols = ["path"]

selected_cols = [{k: d.get(k, None) for k in args.cols} for d in media]
selected_cols = [{k: d.get(k, None) for k in cols} for d in media]
virtual_csv = StringIO()
wr = csv.writer(virtual_csv, quoting=csv.QUOTE_NONE)
wr = csv.DictWriter(virtual_csv, fieldnames=args.cols)
wr = csv.DictWriter(virtual_csv, fieldnames=cols)
wr.writerows(selected_cols)

virtual_csv.seek(0)
Expand Down
49 changes: 49 additions & 0 deletions xklb/scripts/dedupe.py
Expand Up @@ -37,6 +37,13 @@ def parse_args() -> argparse.Namespace:
const="title",
help="Dedupe database by title",
)
profile.add_argument(
"--duration",
action="store_const",
dest="profile",
const="duration",
help="Dedupe database by duration (caution obviously)",
)
profile.add_argument(
"--fts",
action="store_const",
Expand Down Expand Up @@ -281,6 +288,46 @@ def get_title_duplicates(args) -> List[dict]:
return media


def get_duration_duplicates(args) -> List[dict]:
m_columns = db.columns(args, "media")
query = f"""
SELECT
m1.path keep_path
-- , length(m1.path)-length(REPLACE(m1.path, '/', '')) num_slash
-- , length(m1.path)-length(REPLACE(m1.path, '.', '')) num_dot
-- , length(m1.path) len_p
, m2.path duplicate_path
, m2.size duplicate_size
FROM
{args.table} m1
JOIN {args.table2} m2 on 1=1
and m2.path != m1.path
and m1.duration >= m2.duration - 4
and m1.duration <= m2.duration + 4
WHERE 1=1
and coalesce(m1.time_deleted,0) = 0 and coalesce(m2.time_deleted,0) = 0
and m1.duration = m2.duration
{" ".join(args.filter_sql)}
ORDER BY 1=1
, m1.video_count > 0 DESC
{', m1.subtitle_count > 0 DESC' if 'subtitle_count' in m_columns else ''}
, m1.audio_count DESC
, m1.uploader IS NOT NULL DESC
, length(m1.path)-length(REPLACE(m1.path, '/', '')) DESC
, length(m1.path)-length(REPLACE(m1.path, '.', ''))
, length(m1.path)
, m1.size DESC
, m1.time_modified DESC
, m1.time_created DESC
, m1.duration DESC
, m1.path DESC
"""

media = list(args.db.query(query, args.filter_bindings))

return media


def filter_split_files(paths):
pattern = r"\.\d{3,5}\."
return filter(lambda x: not re.search(pattern, x), paths)
Expand All @@ -295,6 +342,8 @@ def dedupe() -> None:
duplicates = get_id_duplicates(args)
elif args.profile == "title":
duplicates = get_title_duplicates(args)
elif args.profile == "duration":
duplicates = get_duration_duplicates(args)
elif args.profile == DBType.filesystem:
print(
"""
Expand Down
13 changes: 10 additions & 3 deletions xklb/usage.py
Expand Up @@ -395,8 +395,15 @@ def play(action) -> str:
library {action} -u 'subtitle_count > 0 desc' # play media that has at least one subtitle first
Prioritize large-sized media
library watch --sort 'ntile(10000) over (order by size/duration) desc'
library watch -u 'ntile(100) over (order by size) desc'
library {action} --sort 'ntile(10000) over (order by size/duration) desc'
library {action} -u 'ntile(100) over (order by size) desc'
Sort by count of media with the same columnX (most common to least common value)
library {action} -u same-duration
library {action} -u same-title
library {action} -u same-size
library {action} -u same-width, same-height, same-fps
library {action} -u same-time_uploaded same-view_count same-upvote_ratio
Post-actions -- choose what to do after playing:
library {action} --post-action keep # do nothing after playing (default)
Expand Down Expand Up @@ -739,7 +746,7 @@ def play(action) -> str:
Adding one URL:
library tabsadd -f monthly -c travel tabs.db https://old.reddit.com/r/Colombia/top/?sort=top&t=month
library tabsadd -f daily tabs.db https://wiby.me/surprise/
Depending on your shell you may need to escape the URL (add quotes)
Expand Down

0 comments on commit db94f2c

Please sign in to comment.