librefi/librefi/utils.py

157 lines
5.2 KiB
Python

# flake8: noqa: E501
import re
from urllib.parse import parse_qs, quote as qs_quote, urlparse
from datetime import datetime
import random
def get_user_agent():
if bool(random.getrandbits(1)):
# Google Chrome
return ("Mozilla/5.0 ({platform}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{version} Safari/537.36"
).format(platform=random.choice([
"Windows NT 10.0; Win64; x64",
"Windows NT 10.0; WOW64",
"Windows NT 10.0",
]), version=random.choice([
# https://en.wikipedia.org/wiki/Google_Chrome_version_history
# https://bin.ptrcnull.me/uruyewogug.js
"64.0.3282",
"65.0.3325",
"66.0.3359",
"67.0.3396",
"68.0.3440",
"69.0.3497",
"70.0.3538",
"71.0.3578",
"72.0.3626",
"73.0.3683",
"74.0.3729",
"75.0.3770",
"76.0.3809",
"77.0.3865",
"78.0.3904",
"79.0.3945",
"80.0.3987",
"81.0.4044",
"83.0.4103",
"84.0.4147",
"85.0.4183",
"86.0.4240",
"87.0",
"87.0",
]))
else:
# Mozilla Firefox
return ("Mozilla/5.0 ({platform}; rv:{version}) Gecko/20100101 Firefox/{version}"
).format(platform=random.choice([
"Windows NT 10.0; Win64; x64",
"Windows NT 10.0",
]), version=random.choice([
# https://www.mozilla.org/en-US/firefox/releases/
# https://bin.ptrcnull.me/dijaboyewi.js
"80.0",
"79.0",
"78.0",
"77.0",
"76.0",
"75.0",
"74.0",
"73.0",
"72.0",
"71.0",
"70.0",
"69.0",
"68.0",
]))
def get_email_address():
time_now = datetime.now()
if time_now.month == 5 and time_now.day == 17:
# https://en.wikipedia.org/wiki/International_Day_Against_Homophobia,_Transphobia_and_Biphobia
return random.choice([
"biuro@ordoiuris.pl",
"kontakt@stronazycia.pl",
"kontakt@petycjaonline.pl",
"gejprzeciwkoswiatu@gmail.com",
"biuro.prasowe@konfederacja.net",
"biuro@pis.org.pl",
"Zbigniew.Ziobro@sejm.pl",
])
email = ""
for i in range(random.randint(6, 18)):
email += chr(97 + random.randint(0, 24))
return email + "@" + random.choice([
"gmail.com", "outlook.com", "live.com",
])
def regex_search_string(regexes, string, default=None):
if not isinstance(regexes, list):
regexes = [regexes]
for regex in regexes:
match = re.search(regex, string)
if match:
return match.group(1)
return default
def dump_qs(obj):
old_qs = []
qs = []
for key in obj:
old_qs.append((key, obj[key]))
not_flat = True
while not_flat:
not_flat = False
for old_qs_element in old_qs:
if isinstance(old_qs_element[1], (str, int, float)):
qs.append((old_qs_element[0], old_qs_element[1]))
elif isinstance(old_qs_element[1], (dict)):
for subkey in old_qs_element[1]:
if old_qs_element[1][subkey] is not None:
qs.append(
(old_qs_element[0] + "[" + subkey + "]", old_qs_element[1][subkey]))
if isinstance(old_qs_element[1][subkey], (dict, list)):
not_flat = True
elif isinstance(old_qs_element[1], (list)):
for index in range(len(old_qs_element[1])):
element = old_qs_element[1][index]
if element is not None:
qs.append(
(old_qs_element[0] + "[" + str(index) + "]", element))
if isinstance(element, (dict, list)):
not_flat = True
if not_flat:
old_qs = qs
qs = []
strng = ""
for el in qs:
strng += qs_quote(str(el[0]), encoding="utf8") + "=" + \
qs_quote(str(el[1]), encoding="utf8") + "&"
return strng[:-1]
def absolute_url(new_url: str, old_url: str):
if new_url.startswith("http:") or new_url.startswith("https:"):
return new_url
old = urlparse(old_url)
scheme = old.scheme if old.scheme is not None else "http"
if new_url.startswith("://"):
return scheme + new_url
if new_url.startswith("//"):
return scheme + ":" + new_url
hostname = old.netloc
if new_url.startswith("/"):
return scheme + "://" + hostname + new_url
# like "hostname/path?query", if other checks fail
return scheme + "://" + new_url