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"]
14 def read(self, sz = None):
18 return self.bk.read(sz)
21 def __init__(self, chapter, stack, n, url):
23 self.chapter = chapter
24 self.volume = self.chapter.volume
25 self.manga = self.volume.manga
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"]
38 return imgstream(self.iurl())
40 class chapter(lib.pagelist):
41 def __init__(self, volume, stack, id, name, url):
44 self.manga = volume.manga
50 def __getitem__(self, i):
51 return self.pages()[i]
54 return len(self.pages())
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()
64 raise Exception("parse error: weird page list for %r" % self)
65 self.cpag = [page(self, self.stack + [(self, n)], n + 1, self.url + ("%i.html" % (n + 1))) for n in xrange(int(m[3:]))]
72 return "<mangafox.chapter %r.%r.%r>" % (self.manga.name, self.volume.name, self.name)
74 class volume(lib.pagelist):
75 def __init__(self, manga, stack, id, name):
82 def __getitem__(self, i):
92 return "<mangafox.volume %r.%r>" % (self.manga.name, self.name)
97 if isinstance(el, BeautifulSoup.Tag):
100 class manga(lib.manga):
101 def __init__(self, lib, id, name, url):
109 def __getitem__(self, i):
110 return self.vols()[i]
113 return len(self.vols())
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"})
120 for i, vn in enumerate(reversed(vls)):
121 name = vn.find("h3", attrs={"class": "volume"}).contents[0].strip()
122 vid = name.encode("utf8")
123 vol = volume(self, [(self, i)], vid, name)
125 if cls.name != u"ul" or cls["class"] != u"chlist":
126 raise Exception("parse error: weird volume list for %r" % self)
127 for o, ch in enumerate(reversed(cls.findAll("li"))):
128 n = ch.div.h3 or ch.div.h4
130 chid = name.encode("utf8")
131 for span in ch("span"):
133 if u" title " in (u" " + span["class"] + u" "):
134 name += " " + span.string
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))
140 vol.ch.append(chapter(vol, vol.stack + [(vol, o)], chid, name, url[:-6]))
141 self.cvol.append(vol)
148 return "<mangafox.manga %r>" % self.name
150 def libalphacmp(a, b):
151 return cmp(a.upper(), b.upper())
153 class library(lib.library):
155 self.base = "http://www.mangafox.com/"
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")
161 ubase = self.base + "manga/"
163 t = m.find("div", attrs={"class": "manga_text"}).find("a", attrs={"class": "title"})
165 url = t["href"].encode("us-ascii")
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))
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)
176 def byname(self, prefix):
177 if not isinstance(prefix, unicode):
178 prefix = prefix.decode("utf8")
180 r = self.alphapages()
184 c = l + ((r + 1 - l) // 2)
185 ls = self.alphapage(c)
186 if libalphacmp(ls[0].name, prefix) > 0:
188 elif libalphacmp(ls[-1].name, prefix) < 0:
196 if libalphacmp(m.name, prefix) >= 0:
202 if not m.name[:len(prefix)].upper() == prefix.upper():
207 ls = self.alphapage(pno)
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
216 name = page.find("div", id="series_info").find("div", attrs={"class": "cover"}).img["alt"]
217 return manga(self, id, name, url)
220 raise NotImplementedError("mangafox iterator")