3 import sys, os, io, getopt, ann, subprocess, tempfile
8 return io.TextIOWrapper(io.FileIO(os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY), "r+"))
10 sys.stderr.write("anndl: terminal not available for interactive input: %s\n" % (exc.strerror))
14 tty.write("%d matches:\n" % (len(opts)))
15 for i, anime in enumerate(opts):
16 tty.write("%d. %s\n" % (i + 1, anime.name))
19 resp = tty.readline().strip()
22 if not resp.isdigit():
23 sys.stderr.write("anndl: %s: not a number\n" % resp)
26 if not 0 <= n < len(opts):
27 sys.stderr.write("anndl: %s: no such option\n" % resp)
31 def prompt(line, default=""):
35 tty.write(" [%s]" % (default))
38 resp = tty.readline().strip()
44 fd, path = tempfile.mkstemp()
46 with os.fdopen(fd, "w") as fp:
48 subprocess.run([os.getenv("EDITOR"), path])
49 with open(path, "r") as fp:
55 out.write("usage: anndl [-h] SERIES\n")
60 opts, args = getopt.getopt(argv[1:], "h")
71 match = ann.getlist(name)
73 sys.stderr.write("anndl: could not find %s\n" % (name))
80 sys.stderr.write("anndl: %s matches %i series\n" % (name, len(match)))
84 tty.write("could not find number of eps\n")
85 if prompt("accept? ")[:1].lower() != "y":
87 name = prompt("canonical name", get.name)
88 sexpr = "\"" + r"L~%s[^0-9]*0*%%i([^0-9]*(\[[^]]*\]|\([^\)]*\)|v2))*[^0-9]*\.(avi|ogm|mkv|mp4)$" + "\""
89 sexpr = sexpr % (name.lower().replace(" ", ".*"))
90 tty.write("sexpr: %s\n" % (sexpr))
91 if prompt("accept? ")[:1].lower() != "y":
94 root = pj(os.getenv("HOME"), "dc/autodl/cur", name)
95 dl = pj(root, ".autodl")
99 except OSError as exc:
100 sys.stderr.write("anndl: %s: %s\n" % (root, exc.strerror))
103 with open(pj(dl, "disabled"), "w") as fp:
105 with open(pj(root, "nextep"), "w") as fp:
107 with open(pj(dl, "uarg"), "w") as fp:
111 melen = len(str(get.eps))
112 fp.write("rename:auto:%s:%i:move:../autodl/cur/%s\n" % (name, melen, name))
113 with open(pj(dl, "sexpr"), "w") as fp:
114 fp.write("%s\n" % sexpr)
115 with open(pj(dl, "curep"), "w") as fp:
117 if get.eps is not None:
118 with open(pj(dl, "maxep"), "w") as fp:
119 fp.write("%i\n" % get.eps)
120 if prompt("begin? ")[:1].lower() == "y":
121 os.unlink(pj(dl, "disabled"))
123 if __name__ == "__main__":
126 except KeyboardInterrupt: