X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=manga%2Fmangafox.py;h=cb5e6550008b3b0c1f69c7fe1f00182e43c99856;hb=c6ee84b17df6fcb4e8821856b885eff26d795885;hp=e06acfa2a2ee4b54055f2ed2bc430e58cb7f4805;hpb=f3ad0817587482b5a726db4c2f82072e191355e1;p=automanga.git diff --git a/manga/mangafox.py b/manga/mangafox.py index e06acfa..cb5e655 100644 --- a/manga/mangafox.py +++ b/manga/mangafox.py @@ -1,50 +1,42 @@ -import urllib -import BeautifulSoup -import lib, htcache -soup = BeautifulSoup.BeautifulSoup - -class imgstream(object): - def __init__(self, url): - self.bk = urllib.urlopen(url) - self.ctype = self.bk.info()["Content-Type"] - - def close(self): - self.bk.close() - - def __enter__(self): - return self - - def __exit__(self, *exc_info): - self.close() - - def read(self, sz = None): - if sz is None: - return self.bk.read() - else: - return self.bk.read(sz) +import urllib.request, re +import bs4, json +from . import lib, htcache +soup = bs4.BeautifulSoup +soupify = lambda cont: soup(cont) class page(lib.page): - def __init__(self, chapter, n, url): + def __init__(self, chapter, stack, n, url): + self.stack = stack self.chapter = chapter self.volume = self.chapter.volume self.manga = self.volume.manga self.n = n + self.id = str(n) + self.name = "Page %s" % n self.url = url self.ciurl = None def iurl(self): if self.ciurl is None: - page = soup(htcache.fetch(self.url)) + page = soupify(htcache.fetch(self.url)) self.ciurl = page.find("div", id="viewer").find("img", id="image")["src"] 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, 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 @@ -57,14 +49,14 @@ class chapter(lib.pagelist): def pages(self): if self.cpag is None: - pg = soup(htcache.fetch(self.url + "1.html")) + pg = soupify(htcache.fetch(self.url + "1.html")) l = pg.find("form", id="top_bar").find("div", attrs={"class": "l"}) if len(l.contents) != 3: raise Exception("parse error: weird page list for %r" % self) m = l.contents[2].strip() - if m[:3] != u"of ": + if m[:3] != "of ": raise Exception("parse error: weird page list for %r" % self) - self.cpag = [page(self, n + 1, self.url + ("%i.html" % (n + 1))) for n in xrange(int(m[3:]))] + self.cpag = [page(self, self.stack + [(self, n)], n + 1, self.url + ("%i.html" % (n + 1))) for n in range(int(m[3:]))] return self.cpag def __str__(self): @@ -74,8 +66,10 @@ class chapter(lib.pagelist): return "" % (self.manga.name, self.volume.name, self.name) class volume(lib.pagelist): - def __init__(self, manga, name): + def __init__(self, manga, stack, id, name): + self.stack = stack self.manga = manga + self.id = id self.name = name self.ch = [] @@ -94,15 +88,19 @@ class volume(lib.pagelist): def nextel(el): while True: el = el.nextSibling - if isinstance(el, BeautifulSoup.Tag): + if isinstance(el, bs4.Tag): 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 + self.stack = [] def __getitem__(self, i): return self.vols()[i] @@ -112,28 +110,34 @@ class manga(lib.manga): def vols(self): if self.cvol is None: - page = soup(htcache.fetch(self.url)) + page = soupify(htcache.fetch(self.url)) vls = page.find("div", id="chapters").findAll("div", attrs={"class": "slide"}) - self.cvol = [] - for i in xrange(len(vls)): - vol = volume(self, vls[i].find("h3", attrs={"class": "volume"}).contents[0].strip()) - cls = nextel(vls[i]) - if cls.name != u"ul" or cls["class"] != u"chlist": + cvol = [] + for i, vn in enumerate(reversed(vls)): + name = vn.find("h3", attrs={"class": "volume"}).contents[0].strip() + vol = volume(self, [(self, i)], name, name) + cls = nextel(vn) + if cls.name != "ul" or "chlist" not in cls["class"]: raise Exception("parse error: weird volume list for %r" % self) - for ch in cls.findAll("li"): + for o, ch in enumerate(reversed(cls.findAll("li"))): n = ch.div.h3 or ch.div.h4 - name = n.a.string + chid = name = n.a.string for span in ch("span"): try: - if u" title " in (u" " + span["class"] + u" "): + if "title" in span["class"]: name += " " + span.string except KeyError: pass - url = n.a["href"].encode("us-ascii") - if url[-7:] != "/1.html": + url = n.a["href"] + 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.insert(0, chapter(vol, name, url[:-6])) - self.cvol.insert(0, 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): @@ -143,31 +147,36 @@ class manga(lib.manga): return "" % self.name def libalphacmp(a, b): - return cmp(a.upper(), b.upper()) + if a.upper() < b.upper(): + return -1 + elif a.upper() > b.upper(): + return 1 + return 0 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))) + page = soupify(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)) + url = t["href"] + 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): - page = soup(htcache.fetch(self.base + "directory/?az")) + page = soupify(htcache.fetch(self.base + "directory/?az")) ls = page.find("div", id="mangalist").find("div", id="nav").find("ul").findAll("li") return int(ls[-2].find("a").string) def byname(self, prefix): - if not isinstance(prefix, unicode): - prefix = prefix.decode("utf8") l = 1 r = self.alphapages() while True: @@ -198,3 +207,22 @@ class library(lib.library): pno += 1 ls = self.alphapage(pno) i = 0 + + def search(self, expr): + req = urllib.request.Request(self.base + ("ajax/search.php?term=%s" % urllib.parse.quote(expr)), + headers={"User-Agent": "automanga/1"}) + with urllib.request.urlopen(req) as resp: + rc = json.loads(resp.read().decode("utf-8")) + return [manga(self, id, name, self.base + ("manga/%s/" % id)) for num, name, id, genres, author in rc] + + def byid(self, id): + url = self.base + ("manga/%s/" % id) + page = soupify(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")