Commit | Line | Data |
---|---|---|
3cc7937c | 1 | #!/usr/bin/python3 |
0d5fcfbb FT |
2 | |
3 | import sys, getopt | |
4 | import manga.lib, manga.profile | |
5 | ||
6 | def usage(out): | |
7 | out.write("usage: checkmanga [-hSi] [-p PROFILE] TAG\n") | |
8 | ||
9 | prnm = "" | |
10 | save = iact = False | |
11 | opts, args = getopt.getopt(sys.argv[1:], "hSip:") | |
12 | for o, a in opts: | |
13 | if o == "-h": | |
14 | usage(sys.stdout) | |
15 | sys.exit(0) | |
16 | elif o == "-S": | |
17 | save = True | |
18 | elif o == "-p": | |
19 | prnm = a | |
20 | elif o == "-i": | |
21 | iact = True | |
22 | if prnm == "": | |
23 | profile = manga.profile.profile.last() | |
24 | else: | |
25 | profile = manga.profile.profile.byname(prnm) | |
26 | if len(args) < 1: | |
27 | usage(sys.stderr) | |
28 | sys.exit(1) | |
29 | tag = args[0] | |
30 | ||
31 | def getpages(pr): | |
32 | try: | |
33 | fp = pr.file("lastpages") | |
34 | except IOError: | |
35 | return {} | |
36 | ret = {} | |
37 | with fp: | |
38 | for ln in fp: | |
39 | words = manga.profile.splitline(ln) | |
40 | ret[tuple(words[:2])] = [int(x) for x in words[2:]] | |
41 | return ret | |
42 | ||
43 | def savepages(pr, pages): | |
44 | with pr.file("lastpages", "w") as fp: | |
45 | for (libnm, id), pl in pages.iteritems(): | |
46 | fp.write(manga.profile.consline(libnm, id, *[str(x) for x in pl]) + "\n") | |
47 | ||
48 | def prompt(q): | |
49 | with open("/dev/tty", "r+") as tp: | |
50 | tp.write(q); tp.flush() | |
51 | return tp.readline().strip() | |
52 | ||
53 | def lastpage(m): | |
54 | ret = [] | |
55 | while isinstance(m, manga.lib.pagelist): | |
56 | ret.append(len(m) - 1) | |
57 | m = m[len(m) - 1] | |
58 | return ret | |
59 | ||
60 | lastpages = getpages(profile) | |
61 | ||
62 | changed = False | |
63 | for mp in profile.bytag(tag): | |
64 | try: | |
65 | try: | |
66 | m = mp.open() | |
67 | except: | |
68 | sys.stderr.write("checkmanga: could not open %s/%s\n" % (libnm, id)) | |
69 | continue | |
70 | nlp = lastpage(m) | |
71 | ch = False | |
72 | if (mp.libnm, mp.id) in lastpages: | |
73 | clp = lastpages[mp.libnm, mp.id] | |
74 | if clp != nlp: | |
75 | try: | |
76 | clpn = u"" | |
77 | p = m | |
78 | for pn in clp: | |
79 | p = p[pn] | |
80 | if clpn != "": | |
81 | clpn += ", " | |
82 | clpn += p.name | |
83 | sys.stdout.write("%s %s: %s\n" % (mp.libnm, mp.id, str(clpn))) | |
84 | ch = True | |
85 | except IndexError: | |
86 | sys.stdout.write("%s %s: structure changed\n" % (mp.libnm, mp.id)) | |
87 | ch = True | |
88 | else: | |
89 | sys.stdout.write("%s %s: not previously checked\n" % (mp.libnm, mp.id)) | |
90 | ch = True | |
91 | if ch: | |
92 | savecur = False | |
93 | if save: | |
94 | savecur = True | |
95 | elif iact: | |
96 | if prompt("Save? ")[:1].lower() == "y": | |
97 | savecur = True | |
98 | if savecur: | |
99 | lastpages[mp.libnm, mp.id] = nlp | |
100 | changed = True | |
101 | except: | |
102 | sys.stderr.write("checkmanga: unexpected error when checking %s/%s:\n" % (mp.libnm, mp.id)) | |
103 | raise | |
104 | if changed: | |
105 | savepages(profile, lastpages) |