diff options
author | Wolfgang Müller | 2024-10-26 21:21:54 +0200 |
---|---|---|
committer | Wolfgang Müller | 2024-11-06 21:30:12 +0100 |
commit | ef2ba0943d941b080946f85993e4383cd5e0b1a3 (patch) | |
tree | e0bedfa0cbdfc560aaf28f7bccfb8446259a3edf | |
parent | ec4750b5e2f801fec7e1100299114483d4a04f4e (diff) | |
download | later-ef2ba0943d941b080946f85993e4383cd5e0b1a3.tar.gz |
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.
-rwxr-xr-x | later | 27 | ||||
-rw-r--r-- | later.1 | 14 |
2 files changed, 38 insertions, 3 deletions
@@ -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) @@ -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 |