2008-04-27 16:54:37 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2008-06-19 09:21:21 +00:00
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
2008-07-06 13:00:06 +00:00
|
|
|
# GPL 2008
|
2014-09-30 19:04:46 +00:00
|
|
|
from __future__ import with_statement, print_function
|
2008-04-28 09:55:30 +00:00
|
|
|
import gzip
|
2014-10-05 11:24:14 +00:00
|
|
|
import json
|
|
|
|
import os
|
2013-06-01 11:29:24 +00:00
|
|
|
import re
|
2012-04-30 08:40:10 +00:00
|
|
|
import struct
|
2008-04-27 16:54:37 +00:00
|
|
|
|
2014-10-05 11:24:14 +00:00
|
|
|
from six import BytesIO, PY3
|
|
|
|
from six.moves import urllib
|
2008-06-17 10:53:29 +00:00
|
|
|
from chardet.universaldetector import UniversalDetector
|
2008-04-27 16:54:37 +00:00
|
|
|
|
|
|
|
|
2012-08-21 06:41:25 +00:00
|
|
|
DEBUG = False
|
2008-04-27 16:54:37 +00:00
|
|
|
# Default headers for HTTP requests.
|
2008-04-29 08:45:14 +00:00
|
|
|
DEFAULT_HEADERS = {
|
2014-03-19 09:47:15 +00:00
|
|
|
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:28.0) Gecko/20100101 Firefox/28.0',
|
2010-08-12 21:35:31 +00:00
|
|
|
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
|
|
|
|
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
|
|
'Accept-Language': 'en-us,en;q=0.5',
|
2008-06-19 09:21:21 +00:00
|
|
|
'Accept-Encoding': 'gzip'
|
2008-04-29 08:45:14 +00:00
|
|
|
}
|
2008-04-27 16:54:37 +00:00
|
|
|
|
2008-04-30 12:43:14 +00:00
|
|
|
def status(url, data=None, headers=DEFAULT_HEADERS):
|
2008-06-19 09:21:21 +00:00
|
|
|
try:
|
2012-08-14 13:58:05 +00:00
|
|
|
f = open_url(url, data, headers)
|
2008-06-19 09:21:21 +00:00
|
|
|
s = f.code
|
2014-09-30 19:04:46 +00:00
|
|
|
except urllib.error.HTTPError as e:
|
2008-06-19 09:21:21 +00:00
|
|
|
s = e.code
|
|
|
|
return s
|
2008-04-30 12:43:14 +00:00
|
|
|
|
|
|
|
def exists(url, data=None, headers=DEFAULT_HEADERS):
|
2008-06-19 09:21:21 +00:00
|
|
|
s = status(url, data, headers)
|
|
|
|
if s >= 200 and s < 400:
|
|
|
|
return True
|
|
|
|
return False
|
2008-04-30 12:43:14 +00:00
|
|
|
|
2012-09-09 17:28:11 +00:00
|
|
|
def get_headers(url, data=None, headers=DEFAULT_HEADERS):
|
2008-06-19 09:21:21 +00:00
|
|
|
try:
|
2012-08-14 13:58:05 +00:00
|
|
|
f = open_url(url, data, headers)
|
2008-06-19 09:21:21 +00:00
|
|
|
f.headers['Status'] = "%s" % f.code
|
|
|
|
headers = f.headers
|
|
|
|
f.close()
|
2014-09-30 19:04:46 +00:00
|
|
|
except urllib.error.HTTPError as e:
|
2008-06-19 09:21:21 +00:00
|
|
|
e.headers['Status'] = "%s" % e.code
|
|
|
|
headers = e.headers
|
|
|
|
return dict(headers)
|
2008-04-30 12:43:14 +00:00
|
|
|
|
2014-10-05 11:24:14 +00:00
|
|
|
def get_json(url, data=None, headers=DEFAULT_HEADERS):
|
|
|
|
return json.loads(read_url(url, data, headers).decode('utf-8'))
|
|
|
|
|
2012-08-14 13:58:05 +00:00
|
|
|
def open_url(url, data=None, headers=DEFAULT_HEADERS):
|
2014-09-30 19:04:46 +00:00
|
|
|
if isinstance(url, bytes):
|
|
|
|
url = url.decode('utf-8')
|
2008-06-19 09:21:21 +00:00
|
|
|
url = url.replace(' ', '%20')
|
2014-10-04 19:07:18 +00:00
|
|
|
if data and PY3 and not isinstance(data, bytes):
|
2014-10-04 19:04:55 +00:00
|
|
|
data = data.encode('utf-8')
|
2014-09-30 19:04:46 +00:00
|
|
|
req = urllib.request.Request(url, data, headers)
|
|
|
|
return urllib.request.urlopen(req)
|
2008-04-27 16:54:37 +00:00
|
|
|
|
2012-08-14 13:58:05 +00:00
|
|
|
def read_url(url, data=None, headers=DEFAULT_HEADERS, return_headers=False, unicode=False):
|
2012-08-21 06:41:25 +00:00
|
|
|
if DEBUG:
|
2014-09-30 19:04:46 +00:00
|
|
|
print('ox.net.read_url', url)
|
2012-08-14 13:58:05 +00:00
|
|
|
f = open_url(url, data, headers)
|
2012-08-17 20:20:35 +00:00
|
|
|
result = f.read()
|
2008-06-19 09:21:21 +00:00
|
|
|
f.close()
|
|
|
|
if f.headers.get('content-encoding', None) == 'gzip':
|
2014-09-30 19:04:46 +00:00
|
|
|
result = gzip.GzipFile(fileobj=BytesIO(result)).read()
|
2012-08-14 13:58:05 +00:00
|
|
|
if unicode:
|
2014-09-30 19:04:46 +00:00
|
|
|
ctype = f.headers.get('content-type', '').lower()
|
|
|
|
if 'charset' in ctype:
|
|
|
|
encoding = ctype.split('charset=')[-1]
|
|
|
|
else:
|
|
|
|
encoding = detect_encoding(result)
|
2012-08-14 13:58:05 +00:00
|
|
|
if not encoding:
|
|
|
|
encoding = 'latin-1'
|
2012-08-17 20:20:35 +00:00
|
|
|
result = result.decode(encoding)
|
2012-08-14 13:58:05 +00:00
|
|
|
if return_headers:
|
2008-06-19 09:21:21 +00:00
|
|
|
f.headers['Status'] = "%s" % f.code
|
2014-09-30 19:04:46 +00:00
|
|
|
headers = {}
|
|
|
|
for key in f.headers:
|
|
|
|
headers[key.lower()] = f.headers[key]
|
|
|
|
return headers, result
|
2012-08-17 20:20:35 +00:00
|
|
|
return result
|
2008-04-27 16:54:37 +00:00
|
|
|
|
2012-08-14 13:58:05 +00:00
|
|
|
def detect_encoding(data):
|
2014-09-30 19:04:46 +00:00
|
|
|
data_lower = data.lower().decode('utf-8', 'ignore')
|
|
|
|
charset = re.compile('content="text/html; charset=(.*?)"').findall(data_lower)
|
2013-06-01 11:29:24 +00:00
|
|
|
if not charset:
|
2014-09-30 19:04:46 +00:00
|
|
|
charset = re.compile('meta charset="(.*?)"').findall(data_lower)
|
2013-06-01 11:29:24 +00:00
|
|
|
if charset:
|
|
|
|
return charset[0].lower()
|
2008-06-19 09:21:21 +00:00
|
|
|
detector = UniversalDetector()
|
2014-09-30 19:04:46 +00:00
|
|
|
p = 0
|
|
|
|
l = len(data)
|
|
|
|
s = 1024
|
|
|
|
while p < l:
|
|
|
|
detector.feed(data[p:p+s])
|
2008-06-19 09:21:21 +00:00
|
|
|
if detector.done:
|
|
|
|
break
|
2014-09-30 19:04:46 +00:00
|
|
|
p += s
|
2008-06-19 09:21:21 +00:00
|
|
|
detector.close()
|
|
|
|
return detector.result['encoding']
|
2008-06-17 10:53:29 +00:00
|
|
|
|
2014-10-05 11:24:14 +00:00
|
|
|
get_url=read_url
|
|
|
|
|
2012-08-14 13:58:05 +00:00
|
|
|
def save_url(url, filename, overwrite=False):
|
2009-07-15 13:53:40 +00:00
|
|
|
if not os.path.exists(filename) or overwrite:
|
|
|
|
dirname = os.path.dirname(filename)
|
|
|
|
if not os.path.exists(dirname):
|
|
|
|
os.makedirs(dirname)
|
2012-09-06 11:25:57 +00:00
|
|
|
data = read_url(url)
|
2014-10-02 08:28:22 +00:00
|
|
|
with open(filename, 'wb') as f:
|
|
|
|
f.write(data)
|
2009-07-15 13:53:40 +00:00
|
|
|
|
2012-04-30 08:40:10 +00:00
|
|
|
def oshash(url):
|
|
|
|
def get_size(url):
|
2014-09-30 19:04:46 +00:00
|
|
|
req = urllib.request.Request(url, headers=DEFAULT_HEADERS.copy())
|
2012-04-30 08:40:10 +00:00
|
|
|
req.get_method = lambda : 'HEAD'
|
2014-09-30 19:04:46 +00:00
|
|
|
u = urllib.request.urlopen(req)
|
2012-04-30 08:40:10 +00:00
|
|
|
if u.code != 200 or not 'Content-Length' in u.headers:
|
|
|
|
raise IOError
|
|
|
|
return int(u.headers['Content-Length'])
|
|
|
|
|
|
|
|
def get_range(url, start, end):
|
|
|
|
headers = DEFAULT_HEADERS.copy()
|
|
|
|
headers['Range'] = 'bytes=%s-%s' % (start, end)
|
2014-09-30 19:04:46 +00:00
|
|
|
req = urllib.request.Request(url, headers=headers)
|
|
|
|
u = urllib.request.urlopen(req)
|
2012-04-30 08:40:10 +00:00
|
|
|
return u.read()
|
|
|
|
|
|
|
|
try:
|
|
|
|
longlongformat = 'q' # long long
|
|
|
|
bytesize = struct.calcsize(longlongformat)
|
|
|
|
|
|
|
|
filesize = get_size(url)
|
|
|
|
hash = filesize
|
|
|
|
head = get_range(url, 0, min(filesize, 65536))
|
|
|
|
if filesize > 65536:
|
|
|
|
tail = get_range(url, filesize-65536, filesize)
|
|
|
|
if filesize < 65536:
|
|
|
|
for offset in range(0, filesize, bytesize):
|
|
|
|
buffer = head[offset:offset+bytesize]
|
|
|
|
(l_value,)= struct.unpack(longlongformat, buffer)
|
|
|
|
hash += l_value
|
|
|
|
hash = hash & 0xFFFFFFFFFFFFFFFF #cut off 64bit overflow
|
|
|
|
else:
|
|
|
|
for offset in range(0, 65536, bytesize):
|
|
|
|
buffer = head[offset:offset+bytesize]
|
|
|
|
(l_value,)= struct.unpack(longlongformat, buffer)
|
|
|
|
hash += l_value
|
|
|
|
hash = hash & 0xFFFFFFFFFFFFFFFF #cut of 64bit overflow
|
|
|
|
for offset in range(0, 65536, bytesize):
|
|
|
|
buffer = tail[offset:offset+bytesize]
|
|
|
|
(l_value,)= struct.unpack(longlongformat, buffer)
|
|
|
|
hash += l_value
|
|
|
|
hash = hash & 0xFFFFFFFFFFFFFFFF
|
|
|
|
returnedhash = "%016x" % hash
|
|
|
|
return returnedhash
|
|
|
|
except(IOError):
|
|
|
|
return "IOError"
|
|
|
|
|