X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=manga%2Flib.py;h=662ecc37ce87c2bebd475487847532358ab578af;hb=531e4473ccfc214cb30582905b7dea23d011d501;hp=353d7ada48a9b53c40f2dd1e04fe2115caa30b79;hpb=ffd12e718bf1915abde0455fdf05a34d0632ed4d;p=automanga.git diff --git a/manga/lib.py b/manga/lib.py index 353d7ad..662ecc3 100644 --- a/manga/lib.py +++ b/manga/lib.py @@ -149,11 +149,43 @@ class imgstream(object): """Close this stream.""" raise NotImplementedError() - def read(self, sz = None): + def read(self, sz=None): """Read SZ bytes from the stream, or the entire rest of the stream of SZ is not given.""" raise NotImplementedError() +class stdimgstream(imgstream): + """A standard implementation of imgstream, for libraries which + have no particular implementation requirements.""" + + def __init__(self, url): + import urllib.request + req = urllib.request.Request(url, headers={"User-Agent": "automanga/1"}) + print(req) + self.bk = urllib.request.urlopen(req) + 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() + + 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 cursor(object): def __init__(self, ob): if isinstance(ob, cursor): @@ -188,7 +220,8 @@ class cursor(object): loaded = {} def findlib(name): def load(name): - mod = __import__(name, fromlist=["dummy"]) + import importlib + mod = importlib.import_module(name) if not hasattr(mod, "library"): raise ImportError("module " + name + " is not a manga library") return mod.library()