Dolda2000 GitWeb
/
automanga.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Fixed some profile bugs in getmanga.
[automanga.git]
/
manga
/
htcache.py
diff --git
a/manga/htcache.py
b/manga/htcache.py
index
2f71f9a
..
a53aa45
100644
(file)
--- a/
manga/htcache.py
+++ b/
manga/htcache.py
@@
-1,39
+1,50
@@
-import os, md5, urllib, time
+import os, hashlib, urllib.request, time
+from . import profile
pj = os.path.join
pj = os.path.join
+class notfound(Exception):
+ pass
+
class cache(object):
def __init__(self, dir):
self.dir = dir
def mangle(self, url):
class cache(object):
def __init__(self, dir):
self.dir = dir
def mangle(self, url):
- n =
md5.new
()
- n.update(url)
+ n =
hashlib.md5
()
+ n.update(url
.encode("ascii")
)
return n.hexdigest()
return n.hexdigest()
+ def open(self, url):
+ req = urllib.request.Request(url, headers={"User-Agent": "automanga/1"})
+ return urllib.request.urlopen(req)
+
def miss(self, url):
def miss(self, url):
- s = urllib.urlopen(url)
try:
try:
+ s = self.open(url)
+ except urllib.error.HTTPError as exc:
+ if exc.code == 404:
+ raise notfound(url)
+ raise
+ with s:
+ if s.headers.get("content-encoding") == "gzip":
+ import gzip, io
+ return gzip.GzipFile(fileobj=io.BytesIO(s.read()), mode="r").read()
return s.read()
return s.read()
- finally:
- s.close()
- def fetch(self, url, expire
=
3600):
+ def fetch(self, url, expire
=
3600):
path = pj(self.dir, self.mangle(url))
if os.path.exists(path):
if time.time() - os.stat(path).st_mtime < expire:
path = pj(self.dir, self.mangle(url))
if os.path.exists(path):
if time.time() - os.stat(path).st_mtime < expire:
- with open(path) as f:
+ with open(path
, "rb"
) as f:
return f.read()
data = self.miss(url)
if not os.path.isdir(self.dir):
os.makedirs(self.dir)
return f.read()
data = self.miss(url)
if not os.path.isdir(self.dir):
os.makedirs(self.dir)
- with open(path, "w") as f:
+ with open(path, "w
b
") as f:
f.write(data)
return data
f.write(data)
return data
-home = os.getenv("HOME")
-if home is None or not os.path.isdir(home):
- raise Exception("Could not find home directory for HTTP caching")
-default = cache(pj(home, ".manga", "htcache"))
+default = cache(pj(profile.confdir, "htcache"))
-def fetch(url, expire
=
3600):
+def fetch(url, expire
=
3600):
return default.fetch(url, expire)
return default.fetch(url, expire)