| 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | import sys, os, getopt, time, random |
| 4 | import manga.lib, manga.profile |
| 5 | from PIL import Image |
| 6 | |
| 7 | verbose = 0 |
| 8 | wait = 10 |
| 9 | getnames = False |
| 10 | |
| 11 | def msg(vl, msg, *args): |
| 12 | if verbose >= vl: |
| 13 | sys.stderr.write("getmanga: " + (msg % args) + "\n") |
| 14 | |
| 15 | def getprop(nm, default=None): |
| 16 | if mprof and "dl-" + nm in mprof.props: |
| 17 | return mprof.props["dl-" + nm] |
| 18 | if nm in props: |
| 19 | return props[nm] |
| 20 | return default |
| 21 | |
| 22 | def digits(num): |
| 23 | n, i = 10, 1 |
| 24 | while True: |
| 25 | if num < n: |
| 26 | return i |
| 27 | n, i = n * 10, i + 1 |
| 28 | |
| 29 | def autoname(page): |
| 30 | ret = "" |
| 31 | inames = [] |
| 32 | for t, i in page.stack: |
| 33 | if ret: |
| 34 | inames.append((t, ret)) |
| 35 | if ret: |
| 36 | ret += "-" |
| 37 | ret += "%0*i" % (digits(len(t) + 1), i + 1) |
| 38 | return ret, inames |
| 39 | |
| 40 | def expand(pattern, page): |
| 41 | ret = "" |
| 42 | si = 0 |
| 43 | fp = 0 |
| 44 | inames = [] |
| 45 | stack = list(zip([t for t, i in page.stack], [t for t, i in page.stack[1:]] + [page], [i for t, i in page.stack])) |
| 46 | while True: |
| 47 | p = pattern.find('%', fp) |
| 48 | if p < 0: |
| 49 | if si < len(stack): |
| 50 | sys.stderr.write("getmanga: pattern %s did not match page %s\n" % |
| 51 | (pattern, "/".join(c.name for t, c, i in stack))) |
| 52 | sys.exit(1) |
| 53 | return ret + pattern[fp:], inames |
| 54 | ret += pattern[fp:p] |
| 55 | m = pattern[p + 1:p + 2] |
| 56 | fp = p + 2 |
| 57 | if m == "%": |
| 58 | ret += "%" |
| 59 | else: |
| 60 | if si >= len(stack): |
| 61 | sys.stderr.write("getmanga: pattern %s did not match page %s\n" % |
| 62 | (pattern, "/".join(c.name for t, c, i in stack))) |
| 63 | sys.exit(1) |
| 64 | t, ct, ti = stack[si] |
| 65 | si += 1 |
| 66 | if m == "i": |
| 67 | ret += "%0*i" % (digits(len(t) + 1), ti + 1) |
| 68 | elif m == "n": |
| 69 | ret += ct.name |
| 70 | elif m == "d": |
| 71 | ret += ct.id |
| 72 | else: |
| 73 | sys.stderr.write("getmanga: %s: unknown specifier `%s'\n" % (pattern, m)) |
| 74 | sys.exit(1) |
| 75 | if si < len(stack): |
| 76 | inames.append((ct, ret)) |
| 77 | |
| 78 | checkednames = None |
| 79 | def checknames(tdir, names): |
| 80 | global checkednames |
| 81 | nmpath = os.path.join(tdir, "names") |
| 82 | if checkednames is None: |
| 83 | checkednames = {} |
| 84 | if os.path.exists(nmpath): |
| 85 | with open(nmpath) as fp: |
| 86 | for line in fp: |
| 87 | line = line.strip() |
| 88 | p = line.find(' ') |
| 89 | if p < 0: continue |
| 90 | checkednames[line[:p]] = line[p + 1:] |
| 91 | for t, prefix in names: |
| 92 | if not prefix: continue |
| 93 | if ' ' not in prefix and prefix not in checkednames: |
| 94 | with manga.profile.txfile(nmpath, "w") as fp: |
| 95 | if '\n' not in t.name: |
| 96 | checkednames[prefix] = t.name |
| 97 | msg(1, "adding name %s for %s" % (t.name, prefix)) |
| 98 | else: |
| 99 | checkednames[prefix] = "" |
| 100 | sys.stderr.write("getmanga: warning: node names contains newlines: %r\n" % (t.name,)) |
| 101 | for prefix, name in checkednames.items(): |
| 102 | if not name: continue |
| 103 | fp.write("%s %s\n" % (prefix, name)) |
| 104 | |
| 105 | def download(mng, tdir, pattern): |
| 106 | exts = ["", ".jpg", ".jpeg", ".png", ".gif"] |
| 107 | fmts = {"PNG": "png", "JPEG": "jpeg", "GIF": "gif"} |
| 108 | for page in manga.lib.cursor(mng): |
| 109 | if pattern is None: |
| 110 | nm, inames = autoname(page) |
| 111 | else: |
| 112 | nm, inames = expand(pattern, page) |
| 113 | if getnames: |
| 114 | checknames(tdir, inames) |
| 115 | path = os.path.join(tdir, nm) |
| 116 | if any(os.path.exists(path + ext) for ext in exts): |
| 117 | msg(2, "%s exists, skipping", nm) |
| 118 | continue |
| 119 | msg(1, "getting %s...", nm) |
| 120 | with page.open() as fp: |
| 121 | with open(path, "wb") as out: |
| 122 | done = False |
| 123 | try: |
| 124 | while True: |
| 125 | data = fp.read(65536) |
| 126 | if data == b"": |
| 127 | done = True |
| 128 | break |
| 129 | out.write(data) |
| 130 | finally: |
| 131 | if not done: |
| 132 | os.unlink(path) |
| 133 | try: |
| 134 | img = Image.open(path) |
| 135 | except OSError: |
| 136 | fmt = None |
| 137 | else: |
| 138 | fmt = img.format |
| 139 | if fmt not in fmts: |
| 140 | sys.stderr.write("getmanga: warning: could not determine file format of %s, leaving as is\n" % nm) |
| 141 | else: |
| 142 | os.rename(path, path + "." + fmts[fmt]) |
| 143 | msg(3, "%s -> %s", nm, nm + "." + fmts[fmt]) |
| 144 | cwait = abs(random.gauss(0, 1) * wait) |
| 145 | msg(2, "waiting %.1f s...", cwait) |
| 146 | time.sleep(cwait) |
| 147 | |
| 148 | def usage(out): |
| 149 | out.write("usage: getmanga [-hvn] [-w WAIT] [-p PROFILE] [-P PATTERN] DIRECTORY [LIBRARY ID]\n") |
| 150 | out.write("\tpattern templates:\n") |
| 151 | out.write("\t %i\tSequence number\n") |
| 152 | out.write("\t %n\tName\n") |
| 153 | out.write("\t %d\tID\n") |
| 154 | |
| 155 | def main(): |
| 156 | global verbose, wait, mprof, props, getnames |
| 157 | |
| 158 | opts, args = getopt.getopt(sys.argv[1:], "hvnp:w:P:") |
| 159 | profnm = None |
| 160 | pattern = None |
| 161 | for o, a in opts: |
| 162 | if o == "-h": |
| 163 | usage(sys.stdout) |
| 164 | sys.exit(0) |
| 165 | elif o == "-p": |
| 166 | profnm = a |
| 167 | elif o == "-v": |
| 168 | verbose += 1 |
| 169 | elif o == "-n": |
| 170 | getnames = True |
| 171 | elif o == "-w": |
| 172 | wait = int(a) |
| 173 | elif o == "-P": |
| 174 | pattern = a |
| 175 | if len(args) < 1: |
| 176 | usage(sys.stderr) |
| 177 | sys.exit(1) |
| 178 | tdir = args[0] |
| 179 | |
| 180 | if not os.path.isdir(tdir): |
| 181 | sys.stderr.write("getmanga: %s: not a directory\n" % (tdir)) |
| 182 | sys.exit(1) |
| 183 | |
| 184 | pfile = os.path.join(tdir, ".props") |
| 185 | props = {} |
| 186 | if os.path.exists(pfile): |
| 187 | with open(pfile, "r") as fp: |
| 188 | for words in manga.profile.splitlines(fp): |
| 189 | if words[0] == "set" and len(words) > 2: |
| 190 | props[words[1]] = words[2] |
| 191 | elif words[0] == "lset" and len(words) > 1: |
| 192 | props[words[1]] = words[2:] |
| 193 | |
| 194 | if profnm is None: |
| 195 | profile = manga.profile.profile.last() |
| 196 | elif profnm == "": |
| 197 | profile = None |
| 198 | else: |
| 199 | profile = manga.profile.profile.byname(profnm) |
| 200 | |
| 201 | if props.get("getnames", "") == "yes": |
| 202 | getnames = True |
| 203 | |
| 204 | if len(args) == 2: |
| 205 | usage(sys.stderr) |
| 206 | sys.exit(1) |
| 207 | elif len(args) > 2: |
| 208 | libnm, mid = args[1:3] |
| 209 | elif isinstance(props.get("manga"), list): |
| 210 | libnm, mid = props["manga"] |
| 211 | else: |
| 212 | sys.stderr.write("getmanga: %s: id is neither saved nor given\n" % (tdir)) |
| 213 | sys.exit(1) |
| 214 | try: |
| 215 | lib = manga.lib.findlib(libnm) |
| 216 | except ImportError: |
| 217 | sys.stderr.write("getmanga: no such library: %s\n" % (libnm)) |
| 218 | sys.exit(1) |
| 219 | try: |
| 220 | mng = lib.byid(mid) |
| 221 | except KeyError: |
| 222 | sys.stderr.write("getmanga: no such manga: %s\n" % (mid)) |
| 223 | sys.exit(1) |
| 224 | if profile is not None: |
| 225 | mprof = profile.getmanga(libnm, mng.id) |
| 226 | else: |
| 227 | mprof = None |
| 228 | |
| 229 | download(mng, tdir, pattern or getprop("pattern")) |
| 230 | |
| 231 | if __name__ == "__main__": |
| 232 | try: |
| 233 | main() |
| 234 | except KeyboardInterrupt: |
| 235 | pass |