-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgae_tools.py
32 lines (28 loc) · 993 Bytes
/
gae_tools.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
import urllib2
import logging
from google.appengine.api.urlfetch import DownloadError
def gae_fetch(url, header = None, maxreload = 3):
# construct custom opener IF custom header is given
if header is not None:
opener = urllib2.build_opener()
## header example:
## 'Crawler, contact owner at egor.ryabkov(at)gmail.com'
opener.addheaders = [('User-agent', header)]
gae_opener = opener.open # custom opener version
else:
gae_opener = urllib2.urlopen
count = 0
page = None
while count < maxreload:
try:
## raise DownloadError('testing') # testing
page = gae_opener(url)
break
except DownloadError, de:
logging.error("A DownloadError (%s) has occurred while fetching, "
"repeating" % de)
errorMsg = de
count += 1
if count >= maxreload:
raise DownloadError(errorMsg)
return page