2005-01-19 14:14:38 +01:00
|
|
|
###
|
2005-01-19 14:33:05 +01:00
|
|
|
# Copyright (c) 2002-2005, Jeremiah Fincher
|
2009-09-16 07:17:33 +02:00
|
|
|
# Copyright (c) 2009, James Vega
|
2005-01-19 14:14:38 +01:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
import re
|
|
|
|
import socket
|
|
|
|
import urllib
|
|
|
|
import urllib2
|
|
|
|
import httplib
|
2005-01-27 07:59:08 +01:00
|
|
|
import sgmllib
|
2005-01-19 14:14:38 +01:00
|
|
|
import urlparse
|
2005-01-27 07:59:08 +01:00
|
|
|
import htmlentitydefs
|
2005-01-19 14:14:38 +01:00
|
|
|
|
2010-05-24 21:44:25 +02:00
|
|
|
sockerrors = (socket.error,)
|
|
|
|
try:
|
|
|
|
sockerrors += (socket.sslerror,)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
2005-01-27 07:59:08 +01:00
|
|
|
from str import normalizeWhitespace
|
2005-01-19 14:14:38 +01:00
|
|
|
|
|
|
|
Request = urllib2.Request
|
|
|
|
urlquote = urllib.quote
|
|
|
|
urlunquote = urllib.unquote
|
2009-09-30 01:54:07 +02:00
|
|
|
urlencode = urllib.urlencode
|
2005-01-19 14:14:38 +01:00
|
|
|
|
2005-01-27 07:59:08 +01:00
|
|
|
class Error(Exception):
|
2005-01-19 14:14:38 +01:00
|
|
|
pass
|
|
|
|
|
2009-10-16 03:56:26 +02:00
|
|
|
_octet = r'(?:2(?:[0-4]\d|5[0-5])|1\d\d|\d{1,2})'
|
|
|
|
_ipAddr = r'%s(?:\.%s){3}' % (_octet, _octet)
|
2008-12-09 07:28:37 +01:00
|
|
|
# Base domain regex off RFC 1034 and 1738
|
2009-10-16 03:56:26 +02:00
|
|
|
_label = r'[0-9a-z][-0-9a-z]*[0-9a-z]?'
|
2011-01-02 13:22:54 +01:00
|
|
|
_domain = r'%s(?:\.%s)*\.[0-9a-z][-0-9a-z]+' % (_label, _label)
|
2009-10-16 03:56:26 +02:00
|
|
|
_urlRe = r'(\w+://(?:%s|%s)(?::\d+)?(?:/[^\])>\s]*)?)' % (_domain, _ipAddr)
|
|
|
|
urlRe = re.compile(_urlRe, re.I)
|
|
|
|
_httpUrlRe = r'(https?://(?:%s|%s)(?::\d+)?(?:/[^\])>\s]*)?)' % (_domain,
|
|
|
|
_ipAddr)
|
|
|
|
httpUrlRe = re.compile(_httpUrlRe, re.I)
|
2005-01-19 14:14:38 +01:00
|
|
|
|
|
|
|
REFUSED = 'Connection refused.'
|
|
|
|
TIMED_OUT = 'Connection timed out.'
|
|
|
|
UNKNOWN_HOST = 'Unknown host.'
|
|
|
|
RESET_BY_PEER = 'Connection reset by peer.'
|
|
|
|
FORBIDDEN = 'Client forbidden from accessing URL.'
|
|
|
|
|
|
|
|
def strError(e):
|
|
|
|
try:
|
|
|
|
n = e.args[0]
|
|
|
|
except Exception:
|
|
|
|
return str(e)
|
|
|
|
if n == 111:
|
|
|
|
return REFUSED
|
|
|
|
elif n in (110, 10060):
|
|
|
|
return TIMED_OUT
|
|
|
|
elif n == 104:
|
|
|
|
return RESET_BY_PEER
|
2005-10-14 14:59:47 +02:00
|
|
|
elif n in (8, 7, 3, 2, -2, -3):
|
2005-01-19 14:14:38 +01:00
|
|
|
return UNKNOWN_HOST
|
|
|
|
elif n == 403:
|
|
|
|
return FORBIDDEN
|
|
|
|
else:
|
|
|
|
return str(e)
|
|
|
|
|
2005-01-27 07:59:08 +01:00
|
|
|
defaultHeaders = {
|
|
|
|
'User-agent': 'Mozilla/5.0 (compatible; utils.web python module)'
|
2005-01-19 14:14:38 +01:00
|
|
|
}
|
|
|
|
|
2005-01-27 07:59:08 +01:00
|
|
|
# Other modules should feel free to replace this with an appropriate
|
|
|
|
# application-specific function. Feel free to use a callable here.
|
|
|
|
proxy = None
|
|
|
|
|
2009-09-16 07:17:33 +02:00
|
|
|
def getUrlFd(url, headers=None, data=None):
|
|
|
|
"""getUrlFd(url, headers=None, data=None)
|
|
|
|
|
|
|
|
Opens the given url and returns a file object. Headers and data are
|
2009-09-30 01:54:07 +02:00
|
|
|
a dict and string, respectively, as per urllib2.Request's arguments."""
|
2005-01-19 14:14:38 +01:00
|
|
|
if headers is None:
|
2005-01-27 07:59:08 +01:00
|
|
|
headers = defaultHeaders
|
2005-01-19 14:14:38 +01:00
|
|
|
try:
|
|
|
|
if not isinstance(url, urllib2.Request):
|
|
|
|
if '#' in url:
|
|
|
|
url = url[:url.index('#')]
|
2009-09-16 07:17:33 +02:00
|
|
|
request = urllib2.Request(url, headers=headers, data=data)
|
2005-01-19 14:14:38 +01:00
|
|
|
else:
|
|
|
|
request = url
|
2009-09-16 07:17:33 +02:00
|
|
|
request.add_data(data)
|
2005-01-27 07:59:08 +01:00
|
|
|
httpProxy = force(proxy)
|
2005-01-19 14:14:38 +01:00
|
|
|
if httpProxy:
|
|
|
|
request.set_proxy(httpProxy, 'http')
|
|
|
|
fd = urllib2.urlopen(request)
|
|
|
|
return fd
|
|
|
|
except socket.timeout, e:
|
2005-02-02 15:07:20 +01:00
|
|
|
raise Error, TIMED_OUT
|
2010-05-24 21:44:25 +02:00
|
|
|
except sockerrors, e:
|
2005-02-02 15:07:20 +01:00
|
|
|
raise Error, strError(e)
|
2005-01-19 14:14:38 +01:00
|
|
|
except httplib.InvalidURL, e:
|
2005-02-02 15:07:20 +01:00
|
|
|
raise Error, 'Invalid URL: %s' % e
|
2005-01-19 14:14:38 +01:00
|
|
|
except urllib2.HTTPError, e:
|
2005-02-02 15:07:20 +01:00
|
|
|
raise Error, strError(e)
|
2005-01-19 14:14:38 +01:00
|
|
|
except urllib2.URLError, e:
|
2005-02-02 15:07:20 +01:00
|
|
|
raise Error, strError(e.reason)
|
2005-01-19 14:14:38 +01:00
|
|
|
# Raised when urllib doesn't recognize the url type
|
|
|
|
except ValueError, e:
|
2005-02-02 15:07:20 +01:00
|
|
|
raise Error, strError(e)
|
2005-01-19 14:14:38 +01:00
|
|
|
|
2009-09-16 07:17:33 +02:00
|
|
|
def getUrl(url, size=None, headers=None, data=None):
|
|
|
|
"""getUrl(url, size=None, headers=None, data=None)
|
|
|
|
|
|
|
|
Gets a page. Returns a string that is the page gotten. Size is an integer
|
|
|
|
number of bytes to read from the URL. Headers and data are dicts as per
|
|
|
|
urllib2.Request's arguments."""
|
|
|
|
fd = getUrlFd(url, headers=headers, data=data)
|
2005-01-19 14:14:38 +01:00
|
|
|
try:
|
|
|
|
if size is None:
|
|
|
|
text = fd.read()
|
|
|
|
else:
|
|
|
|
text = fd.read(size)
|
|
|
|
except socket.timeout, e:
|
2005-02-02 15:07:20 +01:00
|
|
|
raise Error, TIMED_OUT
|
2005-01-19 14:14:38 +01:00
|
|
|
fd.close()
|
|
|
|
return text
|
|
|
|
|
|
|
|
def getDomain(url):
|
|
|
|
return urlparse.urlparse(url)[1]
|
|
|
|
|
2005-01-27 07:59:08 +01:00
|
|
|
class HtmlToText(sgmllib.SGMLParser):
|
|
|
|
"""Taken from some eff-bot code on c.l.p."""
|
|
|
|
entitydefs = htmlentitydefs.entitydefs.copy()
|
|
|
|
entitydefs['nbsp'] = ' '
|
|
|
|
def __init__(self, tagReplace=' '):
|
|
|
|
self.data = []
|
|
|
|
self.tagReplace = tagReplace
|
|
|
|
sgmllib.SGMLParser.__init__(self)
|
|
|
|
|
|
|
|
def unknown_starttag(self, tag, attr):
|
|
|
|
self.data.append(self.tagReplace)
|
|
|
|
|
|
|
|
def unknown_endtag(self, tag):
|
|
|
|
self.data.append(self.tagReplace)
|
|
|
|
|
|
|
|
def handle_data(self, data):
|
|
|
|
self.data.append(data)
|
|
|
|
|
|
|
|
def getText(self):
|
|
|
|
text = ''.join(self.data).strip()
|
|
|
|
return normalizeWhitespace(text)
|
|
|
|
|
|
|
|
def htmlToText(s, tagReplace=' '):
|
|
|
|
"""Turns HTML into text. tagReplace is a string to replace HTML tags with.
|
|
|
|
"""
|
|
|
|
x = HtmlToText(tagReplace)
|
|
|
|
x.feed(s)
|
|
|
|
return x.getText()
|
|
|
|
|
|
|
|
def mungeEmail(s):
|
|
|
|
s = s.replace('@', ' AT ')
|
|
|
|
s = s.replace('.', ' DOT ')
|
|
|
|
return s
|
|
|
|
|
2006-02-11 16:52:51 +01:00
|
|
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|
2005-01-19 14:14:38 +01:00
|
|
|
|