Improved mangafox, probably.
[automanga.git] / manga / mangafox.py
index ed40efb..c254a9d 100644 (file)
@@ -1,13 +1,21 @@
-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"]
-        self.clen = int(self.bk.info()["Content-Length"])
+        ok = False
+        try:
+            if self.bk.getcode() != 200:
+                raise IOError("Server error: " + str(self.bk.getcode()))
+            self.ctype = self.bk.info()["Content-Type"]
+            self.clen = int(self.bk.info()["Content-Length"])
+            ok = True
+        finally:
+            if not ok:
+                self.bk.close()
 
     def fileno(self):
         return self.bk.fileno()
@@ -109,6 +117,8 @@ def nextel(el):
             return el
 
 class manga(lib.manga):
+    cure = re.compile(r"/c[\d.]+/$")
+    
     def __init__(self, lib, id, name, url):
         self.lib = lib
         self.id = id
@@ -146,9 +156,13 @@ 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)], chid, name, url[:-6]))
+                    vol.ch.append(chapter(vol, vol.stack + [(vol, o)], chid, name, url))
                 cvol.append(vol)
             self.cvol = cvol
         return self.cvol
@@ -219,6 +233,14 @@ 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))