forked from CMSCompOps/WmAgentScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
64 lines (56 loc) · 2.1 KB
/
utils.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
#! -*- encoding: utf-8 -*-
import sys
import urllib, urllib2
import logging
FORMAT = "%(module)s.%(funcName)s(%(lineno)s) => %(message)s (%(asctime)s)"
DATEFMT = "%Y-%m-%d %H:%M:%S"
logging.basicConfig(format = FORMAT, datefmt = DATEFMT, level=logging.DEBUG)
def url_encode_params(params = {}):
"""
encodes given parameters dictionary. Dictionary values
can contain list, in that case, encoded params will look
like this: param=val1¶m=val2...
"""
params_list = []
for key, value in params.items():
if isinstance(value, list):
params_list.extend([(key, x) for x in value])
else:
params_list.append((key, value))
return urllib.urlencode(params_list)
def download_data(url = None, params = None, headers = None, logger = None):
"""
Returns data got from server.
params has to be a dictionary, which can contain
"key : [value1, value2,...], and that will be
converted to key=value1&key=value2&...
"""
if not logger:
logger = logging
try:
if params:
params = url_encode_params(params)
url = "{url}?{params}".format(url = url, params = params)
response = urllib2.urlopen(url)
data = response.read()
return data
except urllib2.HTTPError as err:
error = "{msg} (HTTP Error: {code})"
logger.error(error.format(code = err.code, msg = err.msg))
logger.error("URL called: {url}".format(url = url))
return None
def download_file(url, params, path = None, logger = None):
if not logger:
logger = logging
if params:
params = url_encode_params(params)
url = "{url}?{params}".format(url = url, params = params)
logger.debug(url)
try:
filename, message = urllib.urlretrieve(url)
return filename
except urllib2.HTTPError as err:
error = "{msg} (HTTP Error: {code})"
logger.error(error.format(code = err.code, msg = err.msg))
logger.error("URL called: {url}".format(url = url))
return None