X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=manga%2Fmangafox.py;h=9831a8106bbb3ee3f61be3a13f8bed9f0e9f81f1;hb=b9e558ac507f4e6c11c8c9837b5bf22b5da90fce;hp=cb289441932610a34338ebce02a4fae9e065f34f;hpb=3683ab38c8cb8b221c2ec20c898fa47a884d3842;p=automanga.git diff --git a/manga/mangafox.py b/manga/mangafox.py index cb28944..9831a81 100644 --- a/manga/mangafox.py +++ b/manga/mangafox.py @@ -1,22 +1,8 @@ -import urllib -import BeautifulSoup +import urllib, re +import BeautifulSoup, json import lib, htcache soup = BeautifulSoup.BeautifulSoup -class imgstream(lib.imgstream): - def __init__(self, url): - self.bk = urllib.urlopen(url) - self.ctype = self.bk.info()["Content-Type"] - - def close(self): - self.bk.close() - - def read(self, sz = None): - if sz is None: - return self.bk.read() - else: - return self.bk.read(sz) - class page(lib.page): def __init__(self, chapter, stack, n, url): self.stack = stack @@ -24,6 +10,8 @@ class page(lib.page): self.volume = self.chapter.volume self.manga = self.volume.manga self.n = n + self.id = str(n) + self.name = u"Page %s" % n self.url = url self.ciurl = None @@ -34,13 +22,20 @@ class page(lib.page): return self.ciurl def open(self): - return imgstream(self.iurl()) + return lib.stdimgstream(self.iurl()) + + def __str__(self): + return self.name + + def __repr__(self): + return "" % (self.manga.name, self.volume.name, self.chapter.name, self.name) class chapter(lib.pagelist): - def __init__(self, volume, stack, name, url): + def __init__(self, volume, stack, id, name, url): self.stack = stack self.volume = volume self.manga = volume.manga + self.id = id self.name = name self.url = url self.cpag = None @@ -70,9 +65,10 @@ class chapter(lib.pagelist): return "" % (self.manga.name, self.volume.name, self.name) class volume(lib.pagelist): - def __init__(self, manga, stack, name): + def __init__(self, manga, stack, id, name): self.stack = stack self.manga = manga + self.id = id self.name = name self.ch = [] @@ -95,8 +91,11 @@ def nextel(el): return el class manga(lib.manga): - def __init__(self, lib, name, url): + cure = re.compile(r"/c[\d.]+/$") + + def __init__(self, lib, id, name, url): self.lib = lib + self.id = id self.name = name self.url = url self.cvol = None @@ -112,15 +111,18 @@ class manga(lib.manga): if self.cvol is None: page = soup(htcache.fetch(self.url)) vls = page.find("div", id="chapters").findAll("div", attrs={"class": "slide"}) - self.cvol = [] + cvol = [] for i, vn in enumerate(reversed(vls)): - vol = volume(self, [(self, i)], vn.find("h3", attrs={"class": "volume"}).contents[0].strip()) + name = vn.find("h3", attrs={"class": "volume"}).contents[0].strip() + vid = name.encode("utf8") + vol = volume(self, [(self, i)], vid, name) cls = nextel(vn) if cls.name != u"ul" or cls["class"] != u"chlist": raise Exception("parse error: weird volume list for %r" % self) for o, ch in enumerate(reversed(cls.findAll("li"))): n = ch.div.h3 or ch.div.h4 name = n.a.string + chid = name.encode("utf8") for span in ch("span"): try: if u" title " in (u" " + span["class"] + u" "): @@ -128,10 +130,15 @@ class manga(lib.manga): except KeyError: pass url = n.a["href"].encode("us-ascii") - if url[-7:] != "/1.html": + if url[-7:] == "/1.html": + url = url[:-6] + elif self.cure.search(url) is not None: + pass + else: raise Exception("parse error: unexpected chapter URL for %r: %s" % (self, url)) - vol.ch.append(chapter(vol, vol.stack + [(vol, o)], name, url[:-6])) - self.cvol.append(vol) + vol.ch.append(chapter(vol, vol.stack + [(vol, o)], chid, name, url)) + cvol.append(vol) + self.cvol = cvol return self.cvol def __str__(self): @@ -145,17 +152,20 @@ def libalphacmp(a, b): class library(lib.library): def __init__(self): - self.base = "http://www.mangafox.com/" + self.base = "http://mangafox.me/" def alphapage(self, pno): page = soup(htcache.fetch(self.base + ("directory/%i.htm?az" % pno))) ls = page.find("div", id="mangalist").find("ul", attrs={"class": "list"}).findAll("li") ret = [] + ubase = self.base + "manga/" for m in ls: t = m.find("div", attrs={"class": "manga_text"}).find("a", attrs={"class": "title"}) name = t.string url = t["href"].encode("us-ascii") - ret.append(manga(self, name, url)) + if url[:len(ubase)] != ubase or url.find('/', len(ubase)) != (len(url) - 1): + raise Exception("parse error: unexpected manga URL for %r: %s" % (name, url)) + ret.append(manga(self, url[len(ubase):-1], name, url)) return ret def alphapages(self): @@ -197,5 +207,22 @@ class library(lib.library): ls = self.alphapage(pno) i = 0 + def search(self, expr): + resp = urllib.urlopen(self.base + ("ajax/search.php?term=%s" % urllib.quote(expr))) + try: + rc = json.load(resp) + finally: + resp.close() + return [manga(self, id.encode("utf8"), name, self.base + ("manga/%s/" % id.encode("utf8"))) for num, name, id, genres, author in rc] + + def byid(self, id): + url = self.base + ("manga/%s/" % id) + page = soup(htcache.fetch(url)) + if page.find("div", id="title") is None: + # Assume we got the search page + raise KeyError(id) + name = page.find("div", id="series_info").find("div", attrs={"class": "cover"}).img["alt"] + return manga(self, id, name, url) + def __iter__(self): raise NotImplementedError("mangafox iterator")