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
|
import re
from collections import Iterable
try:
# Python 2
str_type = unicode
except NameError:
# Python 3
str_type = str
STRING_LIKE_TYPES = (str_type, bytes, bytearray)
try:
# Python 2
from urlparse import urlparse, parse_qsl
except ImportError:
# Python 3
from urllib.parse import urlparse, parse_qsl
try:
import simplejson as json
except ImportError:
import json
def json_iter_parse(response_text):
decoder = json.JSONDecoder(strict=False)
idx = 0
while idx < len(response_text):
obj, idx = decoder.raw_decode(response_text, idx)
yield obj
def stringify_values(dictionary):
stringified_values_dict = {}
for key, value in dictionary.items():
if isinstance(value, Iterable) and not isinstance(value, STRING_LIKE_TYPES):
value = u','.join(map(str_type, value))
stringified_values_dict[key] = value
return stringified_values_dict
def get_url_query(url):
parsed_url = urlparse(url)
url_query = parse_qsl(parsed_url.fragment)
# login_response_url_query can have multiple key
url_query = dict(url_query)
return url_query
def get_form_action(html):
form_action = re.findall(r'<form(?= ).* action="(.+)"', html)
if form_action:
return form_action[0]
def censor_access_token(access_token):
if isinstance(access_token, str_type) and len(access_token) >= 12:
return '{}***{}'.format(access_token[:4], access_token[-4:])
elif access_token:
return '***'
else:
return access_token
|