2 import BeautifulSoup, urlparse
4 soup = BeautifulSoup.BeautifulSoup
6 class imgstream(lib.imgstream):
7 def __init__(self, url):
8 self.bk = urllib.urlopen(url)
11 if self.bk.getcode() != 200:
12 raise IOError("Server error: " + str(self.bk.getcode()))
13 self.ctype = self.bk.info()["Content-Type"]
14 self.clen = int(self.bk.info()["Content-Length"])
21 return self.bk.fileno()
26 def read(self, sz = None):
30 return self.bk.read(sz)
33 def __init__(self, chapter, stack, n, url):
35 self.chapter = chapter
36 self.manga = chapter.manga
39 self.name = u"Page %s" % n
44 if self.ciurl is None:
45 page = soup(htcache.fetch(self.url))
46 self.ciurl = page.find("div", id="imgholder").find("img", id="img")["src"].encode("us-ascii")
50 return imgstream(self.iurl())
56 return "<mrnet.page %r.%r.%r>" % (self.manga.name, self.chapter.name, self.name)
58 class chapter(lib.pagelist):
59 def __init__(self, manga, stack, id, name, url):
67 def __getitem__(self, i):
68 return self.pages()[i]
71 return len(self.pages())
75 pg = soup(htcache.fetch(self.url))
77 for opt in pg.find("div", id="selectpage").find("select", id="pageMenu").findAll("option"):
78 url = urlparse.urljoin(self.url, opt["value"].encode("us-ascii"))
80 pag.append(page(self, self.stack + [(self, len(pag))], n, url))
88 return "<mrnet.chapter %r.%r>" % (self.manga.name, self.name)
90 class manga(lib.manga):
91 def __init__(self, lib, id, name, url):
99 def __getitem__(self, i):
103 return len(self.ch())
107 page = soup(htcache.fetch(self.url))
108 cls = page.find("div", id="chapterlist").find("table", id="listing")
111 for tr in cls.findAll("tr"):
113 if td is None: continue
115 url = urlparse.urljoin(self.url, cla["href"].encode("us-ascii"))
117 cid = name.encode("utf8")
118 if isinstance(cla.nextSibling, unicode):
119 ncont = unicode(cla.nextSibling)
120 if len(ncont) > 3 and ncont[:3] == u" : ":
121 name += u": " + ncont[3:]
122 cch.append(chapter(self, [(self, len(cch))], cid, name, url))
130 return "<mrnet.manga %r>" % self.name
132 class library(lib.library):
134 self.base = "http://www.mangareader.net/"
138 page = soup(htcache.fetch(url))
139 if page.find("h2", attrs={"class": "aname"}) is None:
141 name = page.find("h2", attrs={"class": "aname"}).string
142 return manga(self, id, name, url)
145 page = soup(htcache.fetch(self.base + "alphabetical"))
146 for sec in page.findAll("div", attrs={"class": "series_alpha"}):
147 for li in sec.find("ul", attrs={"class": "series_alpha"}).findAll("li"):
148 url = li.a["href"].encode("us-ascii")
150 if url[:1] != "/": continue
153 # Does this distinction mean something?
154 id = id[id.rindex('/') + 1:]
155 if id[-5:] != ".html":
158 yield manga(self, id, name, urlparse.urljoin(self.base, url))
160 def byname(self, prefix):
161 if not isinstance(prefix, unicode):
162 prefix = prefix.decode("utf8")
163 prefix = prefix.lower()
165 if manga.name.lower()[:len(prefix)] == prefix: