1 import urllib, re, BeautifulSoup
3 soup = BeautifulSoup.BeautifulSoup
5 def byclass(el, name, cl):
6 for ch in el.findAll(name):
7 if not isinstance(ch, BeautifulSoup.Tag): continue
8 cll = ch.get("class", "")
16 if isinstance(el, BeautifulSoup.Tag):
20 def __init__(self, chapter, stack, n, url):
22 self.chapter = chapter
25 self.name = u"Page %s" % n
30 if self.ciurl is None:
31 page = soup(htcache.fetch(self.url))
32 img = nextel(page.find("div", id="full_image")).img
33 self.ciurl = img["src"].encode("us-ascii")
37 return lib.stdimgstream(self.iurl())
43 return "<batoto.page %r.%r.%r>" % (self.chapter.manga.name, self.chapter.name, self.name)
45 class chapter(lib.pagelist):
46 def __init__(self, manga, stack, id, name, url):
54 def __getitem__(self, i):
55 return self.pages()[i]
58 return len(self.pages())
60 pnre = re.compile(r"page (\d+)")
63 pg = soup(htcache.fetch(self.url))
65 for opt in pg.find("select", id="page_select").findAll("option"):
66 url = opt["value"].encode("us-ascii")
67 n = int(self.pnre.match(opt.string).group(1))
68 cpag.append(page(self, self.stack + [(self, len(cpag))], n, url))
76 return "<batoto.chapter %r.%r>" % (self.manga.name, self.name)
78 class manga(lib.manga):
79 def __init__(self, lib, id, name, url):
87 def __getitem__(self, i):
93 cure = re.compile(r"/read/_/(\d+)/[^/]*")
96 page = soup(htcache.fetch(self.url))
97 cls = byclass(page, u"table", u"chapters_list")
98 if cls.tbody is not None:
100 scl = u"lang_" + self.lib.lang
102 for ch in cls.childGenerator():
103 if isinstance(ch, BeautifulSoup.Tag) and ch.name == u"tr":
104 cll = ch.get("class", "").split()
105 if u"row" in cll and scl in cll:
106 url = ch.td.a["href"].encode("us-ascii")
107 m = self.cure.search(url)
108 if m is None: raise Exception("Got weird chapter URL: %r" % url)
110 url = self.lib.base + "read/_/" + cid
112 cch.append((cid, name, url))
115 for n, (cid, name, url) in enumerate(cch):
116 rch.append(chapter(self, [(self, n)], cid, name, url))
124 return "<batoto.manga %r>" % self.name
126 class library(lib.library):
128 self.base = "http://www.batoto.net/"
129 self.lang = u"English"
132 url = self.base + "comic/_/comics/" + id
133 page = soup(htcache.fetch(url))
134 title = page.find("h1", attrs={"class": "ipsType_pagetitle"})
137 return manga(self, id, title.string.strip(), url)
139 mure = re.compile(r"/comic/_/comics/([^/]*)$")
140 def search(self, expr):
141 resp = urllib.urlopen(self.base + "forums/index.php?app=core&module=search&do=search&fromMainBar=1",
142 urllib.urlencode({"search_term": expr, "search_app": "ccs:database:3"}))
144 page = soup(resp.read())
147 none = page.find("p", attrs={"class": "no_messages"})
148 if none is not None and u"No results" in none.text:
151 for child in page.find("div", id="search_results").ol.childGenerator():
152 if isinstance(child, BeautifulSoup.Tag) and child.name == u"li":
153 info = child.find("div", attrs={"class": "result_info"})
154 url = info.h3.a["href"].encode("us-ascii")
155 m = self.mure.search(url)
156 if m is None: raise Exception("Got weird manga URL: %r" % url)
158 name = info.h3.a.string.strip()
159 ret.append(manga(self, id, name, url))