from datetime import date from itertools import count from bs4 import BeautifulSoup from requests import Session from antiintuit.basic import get_session, get_publish_id_from_link from antiintuit.config import Config from antiintuit.database import Account, Course, Subscribe from antiintuit.jobs.accounts_manager import get_authorized_session from antiintuit.logger import exception, get_logger __all__ = ["run_job", "subscribe_to_course"] logger = get_logger("antiintuit", "courses_manager") @exception(logger) def run_job(): """Checks pages of the courses list and adds they in database.""" new_courses, founded_courses = 0, 0 for page_num in count(): logger.debug("Scanning %i page...", page_num) scanned_courses_stats = create_courses_from_page(page_num) new_courses += scanned_courses_stats["new"] founded_courses += scanned_courses_stats["found"] logger.debug("Found on page %i courses among them %i are new.", scanned_courses_stats["found"], scanned_courses_stats["new"]) if scanned_courses_stats[ "found"] == 0: # If the list is empty then pages have ended.
from flask import Flask, abort, request, send_from_directory from flask.logging import default_handler from peewee import BackrefAccessor, Database from antiintuit.api.functions import * from antiintuit.basic import get_publish_id_from_link from antiintuit.config import Config from antiintuit.database import * from antiintuit.logger import get_logger __all__ = ["app", "run_server"] logger = get_logger("antiintuit", "api") app = Flask(logger.name, static_folder=Config.STATIC_DIRECTORY) app.logger.removeHandler(default_handler) allow_models = {"courses": Course, "tests": Test, "questions": Question} @app.route("/<string:model_name>/<int:model_id>", defaults={'attr': None}, methods=["GET"]) @app.route("/<string:model_name>/<int:model_id>/<string:attr>", methods=["GET"]) def get_model_data_by_id(model_name, model_id, attr): if model_name not in allow_models: abort(404) model_class = allow_models[model_name] try: model = model_class.get_by_id(model_id) except model_class.DoesNotExist:
from datetime import datetime import ujson from peewee import MySQLDatabase, PostgresqlDatabase, SqliteDatabase, Model, DateTimeField, TextField from antiintuit.basic import truncate from antiintuit.config import Config from antiintuit.database.exceptions import DatabaseException from antiintuit.logger import get_logger __all__ = ["BaseModel", "VariantsModel"] logger = get_logger("antiintuit", "database") def get_system_database(): """Returns connection to database received from Config""" database_type = Config.DATABASE_TYPE.lower() logger.debug("Connection to %s database (%s)", database_type, Config.DATABASE_NAME) if database_type == "mysql": port = Config.DATABASE_PORT or 3306 return MySQLDatabase(Config.DATABASE_NAME, host=Config.DATABASE_HOST, user=Config.DATABASE_USER, password=Config.DATABASE_PASSWORD, port=port, charset="utf8mb4") elif database_type == "postgres": port = Config.DATABASE_PORT or 5432 return PostgresqlDatabase(Config.DATABASE_NAME,
from bs4 import BeautifulSoup from peewee import SQL from requests import Session from antiintuit.basic import get_image_extension, get_publish_id_from_link from antiintuit.config import Config from antiintuit.database import * from antiintuit.jobs.accounts_manager import get_authorized_session from antiintuit.jobs.courses_manager import subscribe_to_course from antiintuit.jobs.tests_solver.exceptions import * from antiintuit.jobs.tests_solver.queue_solution import * from antiintuit.logger import exception, get_logger __all__ = ["run_job", "run_endless_job_loop"] logger = get_logger("antiintuit", "tests_solver") def run_endless_job_loop(): iteration_count = count(1) while True: logger.info("It will be %i iteration without errors", next(iteration_count)) run_job() @exception(logger) def run_job(test: Test = None, account: Account = None): """Get a test and pass it.""" test_course_account, session = None, None try:
from datetime import datetime from random import randint from requests import Response from antiintuit.basic import get_session from antiintuit.jobs.accounts_manager.temp_mailbox.letter_interface import LetterInterface from antiintuit.jobs.accounts_manager.temp_mailbox.mailbox_interface import MailboxInterface from antiintuit.jobs.accounts_manager.temp_mailbox.services.mytemp_email.mytemp_email_exceptions \ import MyTempEmailException from antiintuit.logger import get_logger __all__ = ["MyTempEmailLetter", "MyTempEmailMailbox"] logger = get_logger("antiintuit", "temp_mailbox", "mytemp_email") class MyTempEmailMailbox(MailboxInterface): """The class for working with https://mytemp.email service""" def __init__(self): self._sid, self._session = randint(0, 10000000), get_session() self._start_timestamp = int(datetime.utcnow().timestamp() * 1000) self._task, self._email_hash, self._messages = 1, None, [] self._session.headers.clear() self._session.headers.update({ "Origin": "https://mytemp.email", "Connection": "keep-alive", "Accept": "application/json, text/plain, */*",
from datetime import timedelta from bs4 import BeautifulSoup from peewee import fn, JOIN, SQL from requests import Session from antiintuit.basic import get_publish_id_from_link from antiintuit.config import Config from antiintuit.database import Account, Course, Subscribe, Test from antiintuit.jobs.accounts_manager import get_authorized_session from antiintuit.jobs.courses_manager import subscribe_to_course from antiintuit.logger import exception, get_logger __all__ = ["run_job"] logger = get_logger("antiintuit", "tests_manager") @exception(logger) def run_job(course: Course = None): # Getting the tests of the first found course if course is None: course = (Course.select().order_by( Course.last_scan_at, Course.published_on.desc()).limit(1)).get() if course.last_scan_at > Config.get_course_scan_timeout_moment(): next_in = course.last_scan_at - Config.get_course_scan_timeout_moment( ) logger.info( "All courses in timeout. Timeout is %s. Next course is '%s' will be in %s.", str(timedelta( minutes=Config.COURSE_SCAN_INTERVAL)).split(".")[0],
from random import randint from requests import Session from antiintuit.jobs.accounts_manager.temp_mailbox.letter_interface import LetterInterface from antiintuit.jobs.accounts_manager.temp_mailbox.mailbox_interface import MailboxInterface from antiintuit.logger import get_logger __all__ = ["GetnadaMailbox", "GetnadaLetter"] logger = get_logger("antiintuit", "temp_mailbox", "getnada") class GetnadaMailbox(MailboxInterface): def __init__(self): self._session = Session() super().__init__(self._get_new_email()) logger.info("Getnada mailbox {} has been initialized".format( self.email)) self.refresh() def _request(self, *commands, method="GET", **kwargs) -> dict: command = "/".join(["https://getnada.com/api/v1", *map(str, commands)]) resp = self._session.request(method, command, **kwargs) resp = resp.json() return resp def _get_domains(self): return list(map(lambda d: d["name"], self._request("domains"))) def _get_new_email(self) -> str:
from bs4 import BeautifulSoup from antiintuit.basic import get_session from antiintuit.config import Config from antiintuit.database import Account, DeletedAccount from antiintuit.jobs.accounts_manager.exceptions import * from antiintuit.jobs.accounts_manager.temp_mailbox import get_random_mailbox, TempMailBoxException from antiintuit.logger import exception, get_logger __all__ = [ "get_authorized_session", "delete_account", "run_job" ] logger = get_logger("antiintuit", "accounts_manager") @exception(logger) def run_job(): """Create and delete one account by a condition.""" the_oldest_accounts = (Account .select() .where((Account.created_at < Config.get_account_aging_moment()) & (Account.reserved_until < Config.get_account_reserve_out_moment())) .order_by(Account.created_at) ) the_oldest_accounts_count = the_oldest_accounts.count() the_fresh_accounts = (Account
import socket from random import random from time import sleep import ujson from antiintuit.config import Config from antiintuit.logger import get_logger, get_host_and_port, is_open_connection __all__ = ["wait_in_the_queue", "get_out_of_the_queue"] logger = get_logger("antiintuit", "tests_solver", "queue_solution") def send_message(host, port, message): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: sock.connect((host, port)) sock.sendall(bytes(message + "\n", "utf-8")) received = ujson.loads(str(sock.recv(1024), "utf-8")) logger.debug("Sent: %s\nReceived: %s", message, repr(received)) return received def wait_in_the_queue(): if Config.TEST_SOLVER_SESSION_QUEUE_HOST is None: sleep_time = random.random() * Config.MAX_LATENCY_FOR_OUT_OF_SYNC logger.debug("Session '%s' is sleeping during %f", Config.SESSION_ID, sleep_time) sleep(sleep_time) return host, port = get_host_and_port(Config.TEST_SOLVER_SESSION_QUEUE_HOST,