1 import urllib, re, BeautifulSoup
3 soup = BeautifulSoup.BeautifulSoup
4 soupify = lambda cont: soup(cont, convertEntities=soup.HTML_ENTITIES)
6 def byclass(el, name, cl):
7 for ch in el.findAll(name):
8 if not isinstance(ch, BeautifulSoup.Tag): continue
9 cll = ch.get("class", "")
17 if isinstance(el, BeautifulSoup.Tag):
21 def __init__(self, chapter, stack, n, url):
23 self.chapter = chapter
26 self.name = u"Page %s" % n
31 if self.ciurl is None:
32 page = soupify(htcache.fetch(self.url))
33 img = nextel(page.find("div", id="full_image")).img
34 self.ciurl = img["src"].encode("us-ascii")
38 return lib.stdimgstream(self.iurl())
44 return "<batoto.page %r.%r.%r>" % (self.chapter.manga.name, self.chapter.name, self.name)
46 class chapter(lib.pagelist):
47 def __init__(self, manga, stack, id, name, url):
55 def __getitem__(self, i):
56 return self.pages()[i]
59 return len(self.pages())
61 pnre = re.compile(r"page (\d+)")
64 pg = soupify(htcache.fetch(self.url))
66 for opt in pg.find("select", id="page_select").findAll("option"):
67 url = opt["value"].encode("us-ascii")
68 n = int(self.pnre.match(opt.string).group(1))
69 cpag.append(page(self, self.stack + [(self, len(cpag))], n, url))
77 return "<batoto.chapter %r.%r>" % (self.manga.name, self.name)
79 class manga(lib.manga):
80 def __init__(self, lib, id, name, url):
89 def __getitem__(self, i):
95 cure = re.compile(r"/read/_/(\d+)/[^/]*")
98 page = soupify(htcache.fetch(self.url))
99 cls = byclass(page, u"table", u"chapters_list")
100 if cls.tbody is not None:
102 scl = u"lang_" + self.lib.lang
104 for ch in cls.childGenerator():
105 if isinstance(ch, BeautifulSoup.Tag) and ch.name == u"tr":
106 cll = ch.get("class", "").split()
107 if u"row" in cll and scl in cll:
108 url = ch.td.a["href"].encode("us-ascii")
109 m = self.cure.search(url)
110 if m is None: raise Exception("Got weird chapter URL: %r" % url)
112 url = self.lib.base + "read/_/" + cid
114 cch.append((cid, name, url))
117 for n, (cid, name, url) in enumerate(cch):
118 rch.append(chapter(self, [(self, n)], cid, name, url))
123 if self.cnames is None:
124 page = soupify(htcache.fetch(self.url))
126 for tbl in page.findAll("table", attrs={"class": "ipb_table"}):
127 if tbl.tbody is not None: tbl = tbl.tbody
128 for tr in tbl.findAll("tr"):
129 if u"Alt Names:" in tr.td.text:
131 if nls.name != u"td" or nls.span is None:
132 raise Exception("Weird altnames table in " + self.id)
133 cnames = [nm.text.strip() for nm in nls.findAll("span")]
135 if cnames is not None:
138 raise Exception("Could not find altnames for " + self.id)
146 return "<batoto.manga %r>" % self.name
148 class library(lib.library):
150 self.base = "http://www.batoto.net/"
151 self.lang = u"English"
154 url = self.base + "comic/_/comics/" + id
155 page = soupify(htcache.fetch(url))
156 title = page.find("h1", attrs={"class": "ipsType_pagetitle"})
159 return manga(self, id, title.string.strip(), url)
161 def _search(self, pars):
166 resp = urllib.urlopen(self.base + "search?" + urllib.urlencode(_pars))
168 page = soupify(resp.read())
171 rls = page.find("div", id="comic_search_results").table
172 if rls.tbody is not None:
175 for child in rls.findAll("tr"):
176 if child.th is not None: continue
177 if child.get("id", u"")[:11] == u"comic_rowo_": continue
178 if child.get("id") == u"show_more_row":
181 link = child.td.strong.a
182 url = link["href"].encode("us-ascii")
183 m = self.rure.search(url)
184 if m is None: raise Exception("Got weird manga URL: %r" % url)
186 name = link.text.strip()
187 yield manga(self, id, name, url)
192 rure = re.compile(r"/comic/_/([^/]*)$")
193 def search(self, expr):
194 if not isinstance(expr, unicode):
195 expr = expr.decode("utf8")
196 return self._search({"name": expr.encode("utf8"), "name_cond": "c"})
198 def byname(self, prefix):
199 if not isinstance(prefix, unicode):
200 prefix = prefix.decode("utf8")
201 for res in self._search({"name": prefix.encode("utf8"), "name_cond": "s"}):
202 if res.name[:len(prefix)].lower() == prefix.lower():
205 for aname in res.altnames():
206 if aname[:len(prefix)].lower() == prefix.lower():
207 yield manga(self, res.id, aname, res.url)
211 print "eliding " + res.name