Fixed batoto search bug.
[automanga.git] / automanga
... / ...
CommitLineData
1#!/usr/bin/python
2
3import sys, getopt
4import manga.lib, manga.reader, manga.profile, manga.local
5import glib, gobject, gtk
6
7gobject.threads_init()
8gtk.gdk.threads_init()
9
10def usage(out):
11 out.write("usage: automanga [-h] [-p PROFILE] {DIRECTORY|-l LIBRARY ID|-a ALIAS}\n")
12 out.write(" automanga -a ALIAS=LIBRARY:ID\n")
13 out.write(" automanga -t {DIRECTORY|-l LIBRARY ID|-a ALIAS} [-]TAG...\n")
14 out.write(" automanga -{s|S} LIBRARY NAME\n")
15 out.write(" automanga -L TAG\n")
16
17libname = None
18search = None
19profile = ""
20alias = None
21settags = False
22listtag = None
23opts, args = getopt.getopt(sys.argv[1:], "hl:sSp:a:tL:")
24for o, a in opts:
25 if o == "-h":
26 usage(sys.stdout)
27 sys.exit(0)
28 elif o == "-l":
29 libname = a
30 elif o == "-a":
31 alias = a
32 elif o == "-s":
33 search = "name"
34 elif o == "-S":
35 search = "match"
36 elif o == "-p":
37 if a == "":
38 profile = None
39 else:
40 profile = a
41 elif o == "-t":
42 settags = True
43 elif o == "-L":
44 listtag = a
45if profile == "":
46 try:
47 profile = manga.profile.profile.last()
48 except KeyError:
49 profile = manga.profile.profile.byname("default")
50elif profile is not None:
51 profile = manga.profile.profile.byname(profile)
52
53def main():
54 global libname
55
56 if search:
57 if len(args) < 2:
58 usage(sys.stderr)
59 sys.exit(1)
60 try:
61 lib = manga.lib.findlib(args[0])
62 except ImportError:
63 sys.stderr.write("automanga: no such library: %s\n" % args[0])
64 sys.exit(1)
65 try:
66 if search == "name":
67 results = lib.byname(args[1])
68 elif search == "match":
69 results = lib.search(args[1])
70 if hasattr(results, "__length_hint__"):
71 sys.stderr.write("%i results:\n" % results.__length_hint__())
72 except NotImplementedError:
73 sys.stderr.write("automanga: %s does not support searching\n" % args[0])
74 sys.exit(1)
75 for mng in results:
76 sys.stdout.write("\"%s\": %s\n" % (mng.name, mng.id))
77 return
78
79 if listtag is not None:
80 if profile is not None:
81 for mprof in profile.bytag(listtag):
82 mng = mprof.open()
83 sys.stdout.write("%s %s: \"%s\"\n" % (mprof.libnm, mprof.id, mng.name))
84 return
85
86 if alias and (alias.find('=') > 0):
87 tgt, nm = alias.split('=', 1)
88 if not ':' in nm:
89 usage(sys.stderr)
90 sys.exit(1)
91 libname, mid = nm.split(':', 1)
92 try:
93 lib = manga.lib.findlib(libname)
94 except ImportError:
95 sys.stderr.write("automanga: no such library: %s\n" % libname)
96 sys.exit(1)
97 try:
98 mng = lib.byid(mid)
99 except KeyError as e:
100 sys.stderr.write("automanga: no such manga: %s\n" % str(e))
101 sys.exit(1)
102 profile.setalias(tgt, libname, mng.id)
103 return
104
105 if alias:
106 try:
107 libname, mid = profile.getalias(alias)
108 except KeyError:
109 sys.stderr("automanga: no such alias: %s\n" % alias)
110 sys.exit(1)
111 try:
112 lib = manga.lib.findlib(libname)
113 except ImportError:
114 sys.stderr.write("automanga: no such library: %s\n" % libname)
115 sys.exit(1)
116 try:
117 mng = lib.byid(mid)
118 except KeyError:
119 sys.stderr.write("automanga: manga no longer found: %s\n" % mid)
120 sys.exit(1)
121 elif libname is not None:
122 if len(args) < 1:
123 usage(sys.stderr)
124 sys.exit(1)
125 try:
126 lib = manga.lib.findlib(libname)
127 except ImportError:
128 sys.stderr.write("automanga: no such library: %s\n" % libname)
129 sys.exit(1)
130 try:
131 mng = lib.byid(args.pop(0))
132 except KeyError as e:
133 sys.stderr.write("automanga: no such manga: %s\n" % str(e))
134 sys.exit(1)
135 else:
136 libname = "local"
137 try:
138 if len(args) > 0:
139 mdir = args.pop(0)
140 else:
141 mdir = "."
142 mng = manga.local.manga(mdir)
143 except TypeError:
144 sys.stderr.write("automanga: not a valid manga directory: %s\n" % mdir)
145 sys.exit(1)
146
147 mprof = None if profile is None else profile.getmanga(libname, mng.id, True)
148 if profile is not None:
149 profile.setlast()
150
151 if settags:
152 if mprof is not None:
153 ntags = [tag for tag in args if tag[:1] != "-"]
154 rtags = [tag[1:] for tag in args if tag[:1] == "-"]
155 if ntags: mprof.tags.add(*ntags)
156 if rtags: mprof.tags.remove(*rtags)
157 return
158
159 reader = manga.reader.reader(mng, mprof)
160 reader.show()
161 gtk.main()
162
163try:
164 main()
165except KeyboardInterrupt:
166 pass