Last active
January 6, 2025 19:23
-
-
Save jab416171/ab6f8d573ce299af1dc52a0147d856f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# This script will get the watched items from the mpv history and generate a playlist file with the unwatched items | |
# Depends on https://git.smrk.net/mpv-scripts/file/history.lua.html with a slight modification (add percent_pos to the table): | |
# | |
# --- history.lua.orig 2025-01-06 01:46:18.487447992 -0700 | |
# +++ history.lua 2025-01-06 10:07:52.193541868 -0700 | |
# @@ -25,12 +25,13 @@ | |
# local sql = [=[ | |
# CREATE TABLE IF NOT EXISTS loaded_items( | |
# - id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, | |
# - path TEXT NOT NULL, | |
# - filename TEXT NOT NULL, | |
# - title TEXT NOT NULL, | |
# - time_pos REAL, | |
# - date DATE NOT NULL | |
# + id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, | |
# + path TEXT NOT NULL, | |
# + filename TEXT NOT NULL, | |
# + title TEXT NOT NULL, | |
# + time_pos REAL, | |
# + percent_pos REAL, | |
# + date DATE NOT NULL | |
# ); | |
# ]=] | |
# @@ -83,12 +84,14 @@ | |
# mp.add_hook("on_unload", 50, function(_hook) | |
# local timepos = mp.get_property("audio-pts") or mp.get_property("time-pos") | |
# + local timepercent = mp.get_property("percent-pos") | |
# if timepos then | |
# dbexec(([=[ | |
# UPDATE loaded_items | |
# - SET time_pos = %g | |
# + SET time_pos = %g, | |
# + percent_pos = %g | |
# WHERE id = %d; | |
# - ]=]):format(timepos, last_insert_id)) | |
# + ]=]):format(timepos, timepercent, last_insert_id)) | |
# end | |
# end) | |
import sqlite3 | |
import os | |
import argparse | |
def get_watched(sqlite_path): | |
with sqlite3.connect(sqlite_path) as conn: | |
c = conn.cursor() | |
c.execute('SELECT path, percent_pos FROM loaded_items') | |
return [row[0] for row in c.fetchall() if row[1] > 90] | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Get watched items from mpv history') | |
parser.add_argument('--watch-path', type=str, nargs='+', action="append", help='Path to file or directory to check') | |
parser.add_argument("--shuffle", action="store_true", help="Shuffle the playlist") | |
args = parser.parse_args() | |
watch_path_tmp = args.watch_path | |
watch_paths = [] | |
for w in watch_path_tmp: | |
new_w = " ".join(w) | |
watch_paths.append(new_w) | |
watch_list = [] | |
# for p in watch_path | |
# if watch_path is a file, assume the file is a playlist, read the file in, and remove any watched items | |
# if watch_path is a directory, get all files in the directory and remove any watched items | |
for watch_path in watch_paths: | |
if os.path.isfile(watch_path): | |
with open(watch_path, 'r') as f: | |
for line in f: | |
if line.startswith('#'): | |
continue | |
watch_list = [line.strip() for line in f] | |
elif os.path.isdir(watch_path): | |
watch_list += [os.path.join(watch_path, f) for f in os.listdir(watch_path)] | |
else: | |
print("Invalid path") | |
exit(1) | |
user = os.getlogin() | |
sqlite_path = f"/home/{user}/.config/mpv/mpv-history.sqlite" | |
watched = get_watched(sqlite_path) | |
for w in watched: | |
if w in watch_list: | |
watch_list.remove(w) | |
path = f"/home/{user}/tmp/playlists" | |
playlist_path = os.path.dirname(watch_list[0]) | |
path += f"/{playlist_path}.m3u" | |
if not os.path.exists(os.path.dirname(path)): | |
os.makedirs(os.path.dirname(path)) | |
with open(path, 'w') as f: | |
f.write('#EXTM3U\n') | |
for w in watch_list: | |
f.write(w + '\n') | |
# invoke mpv with the playlist | |
mpv_flags = "" | |
if args.shuffle: | |
mpv_flags += " --shuffle" | |
mpv_flags += " --no-resume-playback" | |
mpv_flags += " --demuxer-max-bytes=1500M" | |
mpv_flags += " --screenshot-format=png" | |
os.system(f"mpv {mpv_flags} \"{path}\"") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
arg="" | |
for var in "$@"; do | |
if [[ "$var" == "--shuffle" ]]; then | |
arg+=" --shuffle" | |
else | |
arg+=" --watch-path $var" | |
fi | |
done | |
~/src/mpv_get_watched.py $arg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment