31 lines
800 B
Python
31 lines
800 B
Python
# flake8: noqa: E501
|
|
|
|
import re
|
|
from urllib.parse import parse_qs, quote
|
|
|
|
|
|
def get_user_agent():
|
|
# TODO: randomize it
|
|
return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
|
|
|
|
|
|
def regex_search_string(regexes, string, default=None):
|
|
if not isinstance(regexes, list):
|
|
regexes = [regexes]
|
|
for regex in regexes:
|
|
match = re.match(regex, string)
|
|
if match:
|
|
return match
|
|
return default
|
|
|
|
|
|
def dump_qs(obj):
|
|
qs = ""
|
|
for key in obj:
|
|
if isinstance(obj[key], (str, int, float)):
|
|
qs += quote(key) + "=" + quote(str(obj[key]))
|
|
else:
|
|
# TODO: support nested dicts and lists
|
|
raise TypeError("unknown types in dump_qs")
|
|
return qs
|