4 soup = BeautifulSoup.BeautifulSoup
6 class imgstream(lib.imgstream):
7 def __init__(self, url):
8 self.bk = urllib.urlopen(url)
9 self.ctype = self.bk.info()["Content-Type"]
10 self.clen = int(self.bk.info()["Content-Length"])
13 return self.bk.fileno()
18 def read(self, sz = None):
22 return self.bk.read(sz)
25 def __init__(self, chapter, stack, n, url):
27 self.chapter = chapter
28 self.volume = self.chapter.volume
29 self.manga = self.volume.manga
36 if self.ciurl is None:
37 page = soup(htcache.fetch(self.url))
38 self.ciurl = page.find("div", id="viewer").find("img", id="image")["src"]
42 return imgstream(self.iurl())
44 class chapter(lib.pagelist):
45 def __init__(self, volume, stack, id, name, url):
48 self.manga = volume.manga
54 def __getitem__(self, i):
55 return self.pages()[i]
58 return len(self.pages())
62 pg = soup(htcache.fetch(self.url + "1.html"))
63 l = pg.find("form", id="top_bar").find("div", attrs={"class": "l"})
64 if len(l.contents) != 3:
65 raise Exception("parse error: weird page list for %r" % self)
66 m = l.contents[2].strip()
68 raise Exception("parse error: weird page list for %r" % self)
69 self.cpag = [page(self, self.stack + [(self, n)], n + 1, self.url + ("%i.html" % (n + 1))) for n in xrange(int(m[3:]))]
76 return "<mangafox.chapter %r.%r.%r>" % (self.manga.name, self.volume.name, self.name)
78 class volume(lib.pagelist):
79 def __init__(self, manga, stack, id, name):
86 def __getitem__(self, i):
96 return "<mangafox.volume %r.%r>" % (self.manga.name, self.name)
101 if isinstance(el, BeautifulSoup.Tag):
104 class manga(lib.manga):
105 def __init__(self, lib, id, name, url):
113 def __getitem__(self, i):
114 return self.vols()[i]
117 return len(self.vols())
120 if self.cvol is None:
121 page = soup(htcache.fetch(self.url))
122 vls = page.find("div", id="chapters").findAll("div", attrs={"class": "slide"})
124 for i, vn in enumerate(reversed(vls)):
125 name = vn.find("h3", attrs={"class": "volume"}).contents[0].strip()
126 vid = name.encode("utf8")
127 vol = volume(self, [(self, i)], vid, name)
129 if cls.name != u"ul" or cls["class"] != u"chlist":
130 raise Exception("parse error: weird volume list for %r" % self)
131 for o, ch in enumerate(reversed(cls.findAll("li"))):
132 n = ch.div.h3 or ch.div.h4
134 chid = name.encode("utf8")
135 for span in ch("span"):
137 if u" title " in (u" " + span["class"] + u" "):
138 name += " " + span.string
141 url = n.a["href"].encode("us-ascii")
142 if url[-7:] != "/1.html":
143 raise Exception("parse error: unexpected chapter URL for %r: %s" % (self, url))
144 vol.ch.append(chapter(vol, vol.stack + [(vol, o)], chid, name, url[:-6]))
145 self.cvol.append(vol)
152 return "<mangafox.manga %r>" % self.name
154 def libalphacmp(a, b):
155 return cmp(a.upper(), b.upper())
157 class library(lib.library):
159 self.base = "http://mangafox.me/"
161 def alphapage(self, pno):
162 page = soup(htcache.fetch(self.base + ("directory/%i.htm?az" % pno)))
163 ls = page.find("div", id="mangalist").find("ul", attrs={"class": "list"}).findAll("li")
165 ubase = self.base + "manga/"
167 t = m.find("div", attrs={"class": "manga_text"}).find("a", attrs={"class": "title"})
169 url = t["href"].encode("us-ascii")
170 if url[:len(ubase)] != ubase or url.find('/', len(ubase)) != (len(url) - 1):
171 raise Exception("parse error: unexpected manga URL for %r: %s" % (name, url))
172 ret.append(manga(self, url[len(ubase):-1], name, url))
175 def alphapages(self):
176 page = soup(htcache.fetch(self.base + "directory/?az"))
177 ls = page.find("div", id="mangalist").find("div", id="nav").find("ul").findAll("li")
178 return int(ls[-2].find("a").string)
180 def byname(self, prefix):
181 if not isinstance(prefix, unicode):
182 prefix = prefix.decode("utf8")
184 r = self.alphapages()
188 c = l + ((r + 1 - l) // 2)
189 ls = self.alphapage(c)
190 if libalphacmp(ls[0].name, prefix) > 0:
192 elif libalphacmp(ls[-1].name, prefix) < 0:
200 if libalphacmp(m.name, prefix) >= 0:
206 if not m.name[:len(prefix)].upper() == prefix.upper():
211 ls = self.alphapage(pno)
215 url = self.base + ("manga/%s/" % id)
216 page = soup(htcache.fetch(url))
217 if page.find("div", id="title") is None:
218 # Assume we got the search page
220 name = page.find("div", id="series_info").find("div", attrs={"class": "cover"}).img["alt"]
221 return manga(self, id, name, url)
224 raise NotImplementedError("mangafox iterator")