Commit | Line | Data |
---|---|---|
f3ad0817 FT |
1 | import urllib |
2 | import BeautifulSoup | |
3 | import lib, htcache | |
4 | soup = BeautifulSoup.BeautifulSoup | |
5 | ||
3bba3a7b | 6 | class imgstream(lib.imgstream): |
f3ad0817 FT |
7 | def __init__(self, url): |
8 | self.bk = urllib.urlopen(url) | |
9 | self.ctype = self.bk.info()["Content-Type"] | |
10 | ||
11 | def close(self): | |
12 | self.bk.close() | |
13 | ||
f3ad0817 FT |
14 | def read(self, sz = None): |
15 | if sz is None: | |
16 | return self.bk.read() | |
17 | else: | |
18 | return self.bk.read(sz) | |
19 | ||
20 | class page(lib.page): | |
3683ab38 FT |
21 | def __init__(self, chapter, stack, n, url): |
22 | self.stack = stack | |
f3ad0817 FT |
23 | self.chapter = chapter |
24 | self.volume = self.chapter.volume | |
25 | self.manga = self.volume.manga | |
26 | self.n = n | |
46b3b90e | 27 | self.id = str(n) |
f3ad0817 FT |
28 | self.url = url |
29 | self.ciurl = None | |
30 | ||
31 | def iurl(self): | |
32 | if self.ciurl is None: | |
33 | page = soup(htcache.fetch(self.url)) | |
34 | self.ciurl = page.find("div", id="viewer").find("img", id="image")["src"] | |
35 | return self.ciurl | |
36 | ||
37 | def open(self): | |
38 | return imgstream(self.iurl()) | |
39 | ||
40 | class chapter(lib.pagelist): | |
46b3b90e | 41 | def __init__(self, volume, stack, id, name, url): |
3683ab38 | 42 | self.stack = stack |
f3ad0817 FT |
43 | self.volume = volume |
44 | self.manga = volume.manga | |
46b3b90e | 45 | self.id = id |
f3ad0817 FT |
46 | self.name = name |
47 | self.url = url | |
48 | self.cpag = None | |
49 | ||
50 | def __getitem__(self, i): | |
51 | return self.pages()[i] | |
52 | ||
53 | def __len__(self): | |
54 | return len(self.pages()) | |
55 | ||
56 | def pages(self): | |
57 | if self.cpag is None: | |
58 | pg = soup(htcache.fetch(self.url + "1.html")) | |
59 | l = pg.find("form", id="top_bar").find("div", attrs={"class": "l"}) | |
60 | if len(l.contents) != 3: | |
61 | raise Exception("parse error: weird page list for %r" % self) | |
62 | m = l.contents[2].strip() | |
63 | if m[:3] != u"of ": | |
64 | raise Exception("parse error: weird page list for %r" % self) | |
3683ab38 | 65 | self.cpag = [page(self, self.stack + [(self, n)], n + 1, self.url + ("%i.html" % (n + 1))) for n in xrange(int(m[3:]))] |
f3ad0817 FT |
66 | return self.cpag |
67 | ||
68 | def __str__(self): | |
69 | return self.name | |
70 | ||
71 | def __repr__(self): | |
72 | return "<mangafox.chapter %r.%r.%r>" % (self.manga.name, self.volume.name, self.name) | |
73 | ||
74 | class volume(lib.pagelist): | |
46b3b90e | 75 | def __init__(self, manga, stack, id, name): |
3683ab38 | 76 | self.stack = stack |
f3ad0817 | 77 | self.manga = manga |
46b3b90e | 78 | self.id = id |
f3ad0817 FT |
79 | self.name = name |
80 | self.ch = [] | |
81 | ||
82 | def __getitem__(self, i): | |
83 | return self.ch[i] | |
84 | ||
85 | def __len__(self): | |
86 | return len(self.ch) | |
87 | ||
88 | def __str__(self): | |
89 | return self.name | |
90 | ||
91 | def __repr__(self): | |
92 | return "<mangafox.volume %r.%r>" % (self.manga.name, self.name) | |
93 | ||
94 | def nextel(el): | |
95 | while True: | |
96 | el = el.nextSibling | |
97 | if isinstance(el, BeautifulSoup.Tag): | |
98 | return el | |
99 | ||
100 | class manga(lib.manga): | |
46b3b90e | 101 | def __init__(self, lib, id, name, url): |
f3ad0817 | 102 | self.lib = lib |
46b3b90e | 103 | self.id = id |
f3ad0817 FT |
104 | self.name = name |
105 | self.url = url | |
106 | self.cvol = None | |
3683ab38 | 107 | self.stack = [] |
f3ad0817 FT |
108 | |
109 | def __getitem__(self, i): | |
110 | return self.vols()[i] | |
111 | ||
112 | def __len__(self): | |
113 | return len(self.vols()) | |
114 | ||
115 | def vols(self): | |
116 | if self.cvol is None: | |
117 | page = soup(htcache.fetch(self.url)) | |
118 | vls = page.find("div", id="chapters").findAll("div", attrs={"class": "slide"}) | |
119 | self.cvol = [] | |
3683ab38 | 120 | for i, vn in enumerate(reversed(vls)): |
46b3b90e FT |
121 | name = vn.find("h3", attrs={"class": "volume"}).contents[0].strip() |
122 | vid = name.encode("utf8") | |
123 | vol = volume(self, [(self, i)], vid, name) | |
3683ab38 | 124 | cls = nextel(vn) |
f3ad0817 FT |
125 | if cls.name != u"ul" or cls["class"] != u"chlist": |
126 | raise Exception("parse error: weird volume list for %r" % self) | |
3683ab38 | 127 | for o, ch in enumerate(reversed(cls.findAll("li"))): |
f3ad0817 FT |
128 | n = ch.div.h3 or ch.div.h4 |
129 | name = n.a.string | |
46b3b90e | 130 | chid = name.encode("utf8") |
f3ad0817 FT |
131 | for span in ch("span"): |
132 | try: | |
133 | if u" title " in (u" " + span["class"] + u" "): | |
134 | name += " " + span.string | |
135 | except KeyError: | |
136 | pass | |
137 | url = n.a["href"].encode("us-ascii") | |
138 | if url[-7:] != "/1.html": | |
139 | raise Exception("parse error: unexpected chapter URL for %r: %s" % (self, url)) | |
46b3b90e | 140 | vol.ch.append(chapter(vol, vol.stack + [(vol, o)], chid, name, url[:-6])) |
3683ab38 | 141 | self.cvol.append(vol) |
f3ad0817 FT |
142 | return self.cvol |
143 | ||
144 | def __str__(self): | |
145 | return self.name | |
146 | ||
147 | def __repr__(self): | |
148 | return "<mangafox.manga %r>" % self.name | |
149 | ||
150 | def libalphacmp(a, b): | |
151 | return cmp(a.upper(), b.upper()) | |
152 | ||
153 | class library(lib.library): | |
154 | def __init__(self): | |
6fab7b55 | 155 | self.base = "http://mangafox.me/" |
f3ad0817 FT |
156 | |
157 | def alphapage(self, pno): | |
158 | page = soup(htcache.fetch(self.base + ("directory/%i.htm?az" % pno))) | |
159 | ls = page.find("div", id="mangalist").find("ul", attrs={"class": "list"}).findAll("li") | |
160 | ret = [] | |
46b3b90e | 161 | ubase = self.base + "manga/" |
f3ad0817 FT |
162 | for m in ls: |
163 | t = m.find("div", attrs={"class": "manga_text"}).find("a", attrs={"class": "title"}) | |
164 | name = t.string | |
165 | url = t["href"].encode("us-ascii") | |
46b3b90e FT |
166 | if url[:len(ubase)] != ubase or url.find('/', len(ubase)) != (len(url) - 1): |
167 | raise Exception("parse error: unexpected manga URL for %r: %s" % (name, url)) | |
168 | ret.append(manga(self, url[len(ubase):-1], name, url)) | |
f3ad0817 FT |
169 | return ret |
170 | ||
171 | def alphapages(self): | |
172 | page = soup(htcache.fetch(self.base + "directory/?az")) | |
173 | ls = page.find("div", id="mangalist").find("div", id="nav").find("ul").findAll("li") | |
174 | return int(ls[-2].find("a").string) | |
175 | ||
176 | def byname(self, prefix): | |
177 | if not isinstance(prefix, unicode): | |
178 | prefix = prefix.decode("utf8") | |
179 | l = 1 | |
180 | r = self.alphapages() | |
181 | while True: | |
182 | if l > r: | |
183 | return | |
184 | c = l + ((r + 1 - l) // 2) | |
185 | ls = self.alphapage(c) | |
186 | if libalphacmp(ls[0].name, prefix) > 0: | |
187 | r = c - 1 | |
188 | elif libalphacmp(ls[-1].name, prefix) < 0: | |
189 | l = c + 1 | |
190 | else: | |
191 | pno = c | |
192 | break | |
193 | i = 0 | |
194 | while i < len(ls): | |
195 | m = ls[i] | |
196 | if libalphacmp(m.name, prefix) >= 0: | |
197 | break | |
198 | i += 1 | |
199 | while True: | |
200 | while i < len(ls): | |
201 | m = ls[i] | |
202 | if not m.name[:len(prefix)].upper() == prefix.upper(): | |
203 | return | |
204 | yield m | |
205 | i += 1 | |
206 | pno += 1 | |
207 | ls = self.alphapage(pno) | |
208 | i = 0 | |
943a9376 | 209 | |
46b3b90e FT |
210 | def byid(self, id): |
211 | url = self.base + ("manga/%s/" % id) | |
212 | page = soup(htcache.fetch(url)) | |
213 | if page.find("div", id="title") is None: | |
214 | # Assume we got the search page | |
215 | raise KeyError(id) | |
216 | name = page.find("div", id="series_info").find("div", attrs={"class": "cover"}).img["alt"] | |
217 | return manga(self, id, name, url) | |
218 | ||
943a9376 FT |
219 | def __iter__(self): |
220 | raise NotImplementedError("mangafox iterator") |