9
9
import odml .tools .xmlparser
10
10
from hashlib import md5
11
11
py3 = True
12
+
12
13
try :
13
- import urllib .request as urllib2
14
+ from urllib .request import urlopen
14
15
except ImportError :
15
- import urllib2
16
- py3 = False
16
+ from urllib import urlopen
17
+
17
18
import threading
18
19
19
20
CACHE_AGE = datetime .timedelta (days = 1 )
@@ -24,7 +25,7 @@ def cache_load(url):
24
25
load the url and store it in a temporary cache directory
25
26
subsequent requests for this url will use the cached version
26
27
"""
27
- filename = '.' . join ([ md5 (url .encode ()).hexdigest (), os .path .basename (url )] )
28
+ filename = md5 (url .encode ()).hexdigest () + os .path .basename (url )
28
29
cache_dir = os .path .join (tempfile .gettempdir (), "odml.cache" )
29
30
if not os .path .exists (cache_dir ):
30
31
try :
@@ -37,16 +38,15 @@ def cache_load(url):
37
38
or datetime .datetime .fromtimestamp (os .path .getmtime (cache_file )) < \
38
39
datetime .datetime .now () - CACHE_AGE :
39
40
try :
40
- data = urllib2 . urlopen (url ).read ().decode ("latin1 " )
41
+ data = urlopen (url ).read ().decode ("utf-8 " )
41
42
except Exception as e :
42
- print ("failed loading '%s': %s" % (url , e ))
43
+ print ("Failed loading '%s': %s" % (url , e ))
43
44
return
45
+
44
46
fp = open (cache_file , "w" )
45
- if py3 :
46
- fp .write (data )
47
- else :
48
- fp .write (data .encode ('latin1' ))
47
+ fp .write (data )
49
48
fp .close ()
49
+
50
50
return open (cache_file )
51
51
52
52
@@ -74,7 +74,7 @@ def _load(self, url):
74
74
# if url.startswith("http"): return None
75
75
fp = cache_load (url )
76
76
if fp is None :
77
- print ("did not successfully load '%s'" % url )
77
+ print ("Did not successfully load '%s'" % url )
78
78
return
79
79
try :
80
80
term = odml .tools .xmlparser .XMLReader (filename = url , ignore_errors = True ).fromFile (fp )
0 commit comments