From c848a4bf736eb8ac67e9225d0e72618a6da1011a Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Fri, 20 Dec 2019 03:20:48 +0100 Subject: [PATCH] anndl: Replaced with new Python version. --- anndl | 183 +++++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 121 insertions(+), 62 deletions(-) diff --git a/anndl b/anndl index c62c8c1..1290a0b 100755 --- a/anndl +++ b/anndl @@ -1,68 +1,127 @@ -#!/usr/bin/perl -w +#!/usr/bin/python3 -use Anime::ANN; -use Getopt::Long qw/:config gnu_getopt/; +import sys, os, io, getopt, ann, subprocess, tempfile +pj = os.path.join -if(!defined($ARGV[0])) { - print STDERR "usage: anndl SERIES\n"; - exit 1; -} +def opentty(): + try: + return io.TextIOWrapper(io.FileIO(os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY), "r+")) + except OSError as exc: + sys.stderr.write("anndl: terminal not available for interactive input: %s\n" % (exc.strerror)) + sys.exit(1) -unless($id = Anime::ANN::getid $ARGV[0]) { - print STDERR "anndl: could not find $ARGV[0]\n"; - exit 1; -} +def choose(opts): + tty.write("%d matches:\n" % (len(opts))) + for i, anime in enumerate(opts): + tty.write("%d. %s\n" % (i + 1, anime.name)) + tty.write("> ") + tty.flush() + resp = tty.readline().strip() + if resp == "": + return opts[0] + if not resp.isdigit(): + sys.stderr.write("anndl: %s: not a number\n" % resp) + sys.exit(1) + n = int(resp) - 1 + if not 0 <= n < len(opts): + sys.stderr.write("anndl: %s: no such option\n" % resp) + sys.exit(1) + return opts[n] -%info = %{Anime::ANN::getseries $id}; +def prompt(line, default=""): + tty.write(line) + if line[-1:] != " ": + if default != "": + tty.write(" [%s]" % (default)) + tty.write(": ") + tty.flush() + resp = tty.readline().strip() + if resp == "": + return default + return resp -if(!defined $info{"eps"}) { - print "could not find number of eps\n"; - print "accept? "; - if(!( =~ /^y/i)) { - exit 1; - } -} +def edit(string): + fd, path = tempfile.mkstemp() + try: + with os.fdopen(fd, "w") as fp: + fp.write(string) + subprocess.run([os.getenv("EDITOR"), path]) + with open(path, "r") as fp: + return fp.read() + finally: + os.unlink(path) -$name = $info{"name"}; -print "canonical name [$name]: "; -chomp($newname = ); -$name = $newname if $newname ne ""; -$dirname = $ENV{"HOME"} . "/dc/autodl/cur/" . $name; -mkdir $dirname || die "could not create $dirname: $!"; -mkdir "$dirname/.autodl" || die "could not create $dirname/.autodl: $!"; -open FH, ">$dirname/.autodl/disabled" || die "could not create $dirname/.autodl/disabled: $!"; -close FH; -open FH, ">$dirname/nextep" || die "could not create nextep: $!"; -print FH "1\n"; -close FH; -open FH, ">$dirname/.autodl/uarg" || die "could not create $dirname/.autodl/uarg: $!"; -if(defined $info{"eps"}) { - $melen = length $info{"eps"} -} else { - $melen = 2; -} -print FH ("rename:auto:$name:$melen:move:../autodl/cur/$name\n"); -close FH; -$sexpr = lc $name; -$sexpr =~ s/ /.*/g; -$sexpr = "\"L~$sexpr\[^0-9]*0*%i([^0-9]*(\\[[^]]*\\]|\\([^\\)]*\\)|v2))*[^0-9]*\\.(avi|ogm|mkv|mp4)\$\""; -open FH, ">$dirname/.autodl/sexpr" || die "could not create $dirname/.autodl/sexpr: $!"; -print FH "$sexpr\n"; -close FH; -print "sexpr: $sexpr\n"; -print "accept? "; -if(!( =~ /^y/i)) { - system $ENV{"EDITOR"}, "$dirname/.autodl/sexpr"; -} -open FH, ">$dirname/.autodl/curep" || die "could not create $dirname/.autodl/curep: $!"; -print FH "1\n"; -close FH; -if(defined $info{"eps"}) { - open FH, ">$dirname/.autodl/maxep" || die "could not create $dirname/.autodl/maxep: $!"; - print FH ($info{"eps"} . "\n"); - close FH; -} -print "begin? "; -if( =~ /^y/i) { - unlink "$dirname/.autodl/disabled"; -} +def usage(out): + out.write("usage: anndl [-h] SERIES\n") + +def main(argv): + global tty + + opts, args = getopt.getopt(argv[1:], "h") + for o, a in opts: + if o == "-h": + usage(sys.stdout) + sys.exit(0) + if len(args) < 1: + usage(sys.stderr) + sys.exit(1) + name = args[0] + tty = opentty() + + match = ann.getlist(name) + if len(match) == 0: + sys.stderr.write("anndl: could not find %s\n" % (name)) + sys.exit(1) + elif len(match) == 1: + get = match[0] + elif len(match) < 10: + get = choose(match) + else: + sys.stderr.write("anndl: %s matches %i series\n" % (name, len(match))) + sys.exit(1) + + if get.eps is None: + tty.write("could not find number of eps\n") + if prompt("accept? ")[:1].lower() != "y": + sys.exit(1) + name = prompt("canonical name", get.name) + sexpr = "\"" + r"L~%s[^0-9]*0*%%i([^0-9]*(\[^]]*\]|\([^\)]*\)|v2))*[^0-9]*\.(avi|ogm|mkv|mp4)$" + "\"" + sexpr = sexpr % (name.lower().replace(" ", ".*")) + tty.write("sexpr: %s\n" % (sexpr)) + if prompt("accept? ")[:1].lower() != "y": + sexpr = edit(sexpr) + + root = pj(os.getenv("HOME"), "dc/autodl/cur", name) + dl = pj(root, ".autodl") + + try: + os.mkdir(root) + except OSError as exc: + sys.stderr.write("anndl: %s: %s\n" % (root, exc.strerror)) + sys.exit(1) + os.mkdir(dl) + with open(pj(dl, "disabled"), "w") as fp: + pass + with open(pj(root, "nextep"), "w") as fp: + fp.write("1\n") + with open(pj(dl, "uarg"), "w") as fp: + if get.eps is None: + melen = 2 + else: + melen = len(str(get.eps)) + fp.write("rename:auto:%s:$i:move:../autodl/cur/%s\n" % (name, melen, name)) + with open(pj(dl, "sexpr")) as fp: + fp.write("%s\n" % sexpr) + with open(pj(dl, "curep")) as fp: + fp.write("1\n") + if get.eps is not None: + with open(pj(dl, "maxep")) as fp: + fp.write("%i\n" % get.eps) + if prompt("begin? ")[:1].lower() == "y": + os.unlink(pj(dl, "disabled")) + +if __name__ == "__main__": + try: + main(sys.argv) + except KeyboardInterrupt: + sys.exit(1) -- 2.11.0