From ef2ba0943d941b080946f85993e4383cd5e0b1a3 Mon Sep 17 00:00:00 2001 From: Wolfgang Müller Date: Sat, 26 Oct 2024 21:21:54 +0200 Subject: Allow manually creating watch_later entries This commit introduces the "add" command which allows the user to create watch_later entries manually. Make sure to silently ignore entries that already exist - we'll print the entire list of entries subsequently anyway. Also, make explicitly specifying "add" optional if there's any remaining arguments in argv. --- later | 27 ++++++++++++++++++++++++--- later.1 | 14 ++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/later b/later index 3eb9ea0..3ffc645 100755 --- a/later +++ b/later @@ -2,6 +2,7 @@ import datetime import getopt +import hashlib import json import os import re @@ -33,6 +34,7 @@ def usage(message=""): lines.append("") lines.append("usage: later [options] [list]") + lines.append(" [options] [add] item ...") lines.append("options: [-u | --update-titles]") sys.exit("\n".join(lines)) @@ -171,7 +173,7 @@ class Arguments: rest: list[str] = field(default_factory=list) -def parse_args(argv, commands, default): +def parse_args(argv, commands, default, default_args): try: options, args = getopt.gnu_getopt(argv, "u", "update-titles") except getopt.GetoptError as e: @@ -187,6 +189,8 @@ def parse_args(argv, commands, default): if args: if args[0] in commands: parsed_args.command = commands[args.pop(0)] + else: + parsed_args.command = commands[default_args] parsed_args.rest = args @@ -229,9 +233,26 @@ def list_entries(args, title_map): print(entry.format(title_map)) -commands = {"list": Command("list", list_entries, implies_list=False, args=False)} +def add_entries(args, title_map): + for entry in args.rest: + filename = hashlib.md5(entry.encode("utf-8")).hexdigest().upper() + path = os.path.join(watch_later_dir, filename) -args = parse_args(sys.argv[1:], commands, "list") + try: + with open(path, "x") as handle: + handle.write(f"# {entry}\n") + except FileExistsError: + pass + except OSError as e: + exit(e) + + +commands = { + "add": Command("add", add_entries), + "list": Command("list", list_entries, implies_list=False, args=False), +} + +args = parse_args(sys.argv[1:], commands, "list", "add") title_map = TitleMap(title_map_file, update=args.update_titles) diff --git a/later.1 b/later.1 index a4be6b0..b9f38b4 100644 --- a/later.1 +++ b/later.1 @@ -8,6 +8,10 @@ .Nm .Op options .Op Ic list +.Nm +.Op options +.Op Ic add +.Ar item ... .Sh DESCRIPTION .Nm is a program to manage watch_later entries as created by @@ -26,10 +30,20 @@ update titles of videos using yt-dlp .Pp The commands are as follows: .Bl -tag -width Ds +.It Sy add Em item ... +Creates watch_later entries for the given items. +This command is implied if it wasn't explicitly given but there are +remaining arguments. .It Sy list Lists all watch_later entries. This command is implied if there are no remaining arguments. .El +.Pp +An item may be a URL or a path to a file on the file system. +Any command that modifies watch_later entries will subsequently print +all entries just as if +.Ic list +was invoked. .Sh FILES .Bl -tag -width "1" .It Pa $XDG_CACHE_HOME/later/titles.json -- cgit v1.2.3-2-gb3c3