99import odml .tools .xmlparser
1010from hashlib import md5
1111py3 = True
12+
1213try :
13- import urllib .request as urllib2
14+ from urllib .request import urlopen
1415except ImportError :
15- import urllib2
16- py3 = False
16+ from urllib import urlopen
17+
1718import threading
1819
1920CACHE_AGE = datetime .timedelta (days = 1 )
@@ -24,7 +25,7 @@ def cache_load(url):
2425 load the url and store it in a temporary cache directory
2526 subsequent requests for this url will use the cached version
2627 """
27- filename = '.' . join ([ md5 (url .encode ()).hexdigest (), os .path .basename (url )] )
28+ filename = md5 (url .encode ()).hexdigest () + os .path .basename (url )
2829 cache_dir = os .path .join (tempfile .gettempdir (), "odml.cache" )
2930 if not os .path .exists (cache_dir ):
3031 try :
@@ -37,16 +38,15 @@ def cache_load(url):
3738 or datetime .datetime .fromtimestamp (os .path .getmtime (cache_file )) < \
3839 datetime .datetime .now () - CACHE_AGE :
3940 try :
40- data = urllib2 . urlopen (url ).read ().decode ("latin1 " )
41+ data = urlopen (url ).read ().decode ("utf-8 " )
4142 except Exception as e :
42- print ("failed loading '%s': %s" % (url , e ))
43+ print ("Failed loading '%s': %s" % (url , e ))
4344 return
45+
4446 fp = open (cache_file , "w" )
45- if py3 :
46- fp .write (data )
47- else :
48- fp .write (data .encode ('latin1' ))
47+ fp .write (data )
4948 fp .close ()
49+
5050 return open (cache_file )
5151
5252
@@ -74,7 +74,7 @@ def _load(self, url):
7474 # if url.startswith("http"): return None
7575 fp = cache_load (url )
7676 if fp is None :
77- print ("did not successfully load '%s'" % url )
77+ print ("Did not successfully load '%s'" % url )
7878 return
7979 try :
8080 term = odml .tools .xmlparser .XMLReader (filename = url , ignore_errors = True ).fromFile (fp )
0 commit comments