-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcache.py
149 lines (126 loc) · 3.46 KB
/
cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
import pickle
import re
import urllib2
#import urllib
import codecs
#verbose = False
verbose = True
def doload(x,f):
filename = "data/%s.pkl" % x
output = open(filename, 'wb')
data = f()
if data is None :
raise Exception (" ".join(["for",x,"for", filename,"got None"]))
s= str(data)
l =len(s)
if (l > 80 ):
l=80
print "for",x,"for", filename,"got",s[:l]
pickle.dump(data, output)
return data
def delcache(x) :
filename = "data/%s.pkl" % x
if (os.path.exists(filename)):
os.remove(filename)
def cache (x,f) :
filename = "data/%s.pkl" % x
if not os.path.exists("data"):
os.makedirs("data")
if (os.path.exists(filename)):
pkl_file = open(filename, 'rb')
try :
data = pickle.load(pkl_file)
return data
except :
# just load it
return doload(x,f)
else:
return doload(x,f)
def cachewp (url) :
data = cacheweb(url)
if (re.search("Redirected from",data)):
raise Exception( " redirect %s" % url)
#print "redirect %s" % url
return data
def cachewebfile (url) :
url2=url
url2=url2.replace("/","_")
filename = "data/" + url2
filename = filename.replace('action=purge&','')
if not os.path.exists("data"):
os.makedirs("data")
if (os.path.exists(filename)):
if verbose :
#print "get url" , url, " from file ", filename
return filename
###
# if verbose :
# print "get", url
hdr = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Cache-Control' : 'no-cache, no-store, max-age=0, must-revalidate',
'Pragma' : 'no-cache',
# 'Accept-Charset': 'utf-8',
# 'Accept-Language': 'en-US',
'Connection': 'keep-alive'
}
r = urllib2.Request(url=url, headers=hdr )
d=None
try:
d = urllib2.urlopen(r)
except urllib2.HTTPError, e:
print "error http",e
raise e
except Exception, e:
print "other",e
raise e
except:
print "could not load "
exit()
data= d.read()
try :
data2 = data.decode("utf-8")
f = codecs.open(filename,'wb','utf-8')
f.write(data2)
f.close()
print "write", filename
return filename
except Exception, e :
print "error decoding", e
try :
f = open(filename,'wb')
f.write(data)
f.close()
return filename
except Exception, e :
print "decoding2", e
return None
def cacheweb (url) :
filename =cachewebfile (url)
if (os.path.exists(filename)):
if verbose :
#print "get url" , url, " from file ", filename
pass
try:
f = codecs.open(filename, "rb", "utf-8")
data= f.read()
return data
except Exception,e :
print "failed to open, ", e, "try again without utf8"
f = open(filename, "rb")
data= f.read()
return data
return None
import pycurl
import StringIO
def curlget(url):
c = pycurl.Curl()
c.setopt(pycurl.URL, url)
print " pycurl", url
b = StringIO.StringIO()
c.perform()
c.close()
data = b.getvalue()
return data