You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

124 lines
3.9 KiB

import pickle
import random
from datetime import datetime
from time import sleep
from arangodb import db
def sleep_(t):
"""
Sover en tid nära den angivna (för att inte sökningarna ska bli för lika varandra)
"""
variation = 4 # Testa olika sovlängder för att inte få användaren blockerad
sleep(t * variation * random.randrange(85, 115, 1) / 100)
if random.randrange(0, 60, 1) == 1:
longsleep = random.randrange(200, 300)
print('')
for s in range(0, longsleep):
print(f"Sover {longsleep - s} sekunder till... ", end="\r")
sleep(1)
print()
sleep(random.randrange(0, 10, 1) / 4)
# TODO #1 spara cookies till db
def update_cookie(cookies, profile):
""" Uppdaterar cookie för browser """
with open("data/cookie_{}.pkl".format(profile.name), "wb") as f:
pickle.dump(cookies, f)
def write_error(nr, profile, e=" ", traceback="", soup="", user="", url="", url_name=""):
"""Skriver info efter error till arango
Args:
nr ([type]): error number
e (str, optional): error. Defaults to "".
traceback (str, optional): The traceback from traceback.format_exc(). Defaults to "".
soup (str, optional): Soup. Defaults to "".
user (class, optional): The user. Defaults to "".
url (str, optional): Url, if any. Defaults to "".
count (int, optional): Count, if any. Defaults to 0.
url_name (str, optional): The description of the url, if any. Defaults to "".
profile (user, optional): The profile.
"""
if url == "":
url = "ingen url"
url_name = "ingen url"
# try:
# # BARA VID FELSÖKNING
# print(profile, user, e)
# print(profile, user, traceback.format_exc())
# _print(profile, user, e)
# _print(profile, user, traceback.format_exc())
# except Exception as e:
# print('Kunde inte skriva error print till databasen.')
# print(e)
if "e" not in locals():
e = 'Unknown error'
doc = {
"_key": f"{now()}_{profile.container})",
"number": nr,
"error": nr,
"user": str(user.username),
"error": str(e),
"url": str(url),
"url_name": url_name,
"soup": str(soup),
"traceback": str(traceback).split('\n'),
}
try:
db.insert_document(
"errors",
doc,
overwrite_mode="update",
silent=True,
)
except Exception as e:
_print(profile, user, e)
def now():
""" Returns current date and time as string"""
return datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
def nowstamp():
""" Returns current date and time as timestamp"""
return int(datetime.now().timestamp())
def _print(profile, user, text, end='\n', silent=False, sleeptime=0):
""" Write a "print" to the database (and prints in in the terminal)
Args:
container (string): the value of profile.container, meaning the "original" user.
user (class): The user username currelntly beeing looked up.
text (string): What should be written.
end (str, optional): The end value for print. Defaults to '\n'.
silent (bool, optional): If a print should be done in the terminal. Defaults to False.
"""
from classes import User
if silent == False:
print(text, end=end)
if isinstance(user, User):
if isinstance(text, list):
to_print = {user.username: text}
else:
to_print = f"{user.username} - {text.strip()}"
else:
if isinstance(text, list):
to_print = {user: text}
else:
to_print = f"{text.strip()}"
db.insert_document(
"prints",
{'_key': str(profile.container), 'print':{now(): to_print}},
overwrite_mode="update",
silent=True,
)
sleep(sleeptime)