Fix - Use all categories

This commit is contained in:
CAJNA Jarod 2023-02-07 21:55:14 +01:00
parent bc96776ec0
commit e95987c590
8 changed files with 52 additions and 39 deletions

View File

@ -1,7 +1,10 @@
./data/logs/*.logs
./data/data.db
./data/profiles/prof_*
.vscode
.gitignore
.idea/
./__pycache__/
./GlobalExambBot/__pycache__/
.git
*.md

View File

@ -1,5 +1,6 @@
import logging
import os
import sys
from GlobalExamBot.helpers import TypeInField, element_exists, wait_between
from GlobalExamBot.Sheets import Sheets
@ -11,6 +12,7 @@ from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
class Bot:
def __init__(self, driver, action, configuration):
self.driver = driver
self.actions = action
@ -18,7 +20,7 @@ class Bot:
self.email_xpath = '//input[@name="email"]'
self.password_xpath = '//input[@name="password"]'
self.index = 0
self.scrollcount = 0
self.catindex = 0
self.categories = ['https://exam.global-exam.com/library/study-sheets/categories/grammar',
'https://exam.global-exam.com/library/study-sheets/categories/language-functions',
'https://exam.global-exam.com/library/study-sheets/categories/vocabulary']
@ -49,20 +51,17 @@ class Bot:
Sheets_action = Sheets(self.driver, self.actions, self.configuration)
while True:
self.driver.get('https://exam.global-exam.com/library/study-sheets/categories/grammar')
self.driver.get(self.categories[self.catindex])
Sheets_list = Sheets_action.search()
if Sheets_list :
logging.info(f'Sheets n°{ self.index }')
Sheets_action.watch(Sheets_list[0])
self.index +=1
self.scrollcount = 0
wait_between(3,10)
else:
logging.info('All visible Sheets have already been read. Need to scroll down ...')
self.driver.execute_script(f"window.scrollTo(0, document.body.scrollHeight)")
self.scrollcount += 1
wait_between(5,15)
if self.scrollcount > 10:
logging.info('End of page or network error.')
self.scrollcount = 0
logging.info(self.driver.get_log('browser'))
if self.catindex != len(self.categories) - 1 :
logging.info('All visible Sheets have already been read. Use of the next category.')
self.catindex += 1
else:
logging.info('No category available.')
sys.exit(1)

View File

@ -1,6 +1,9 @@
import sqlite3
class Database:
"""
Database management
"""
def __init__(self, database_link='./data/data.db'):
"""
Database constructor

View File

@ -3,6 +3,9 @@ from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
class Driver:
"""
Browser management
"""
def __init__(self, profile):
self.profile = profile
self.chrome_options = None
@ -10,6 +13,9 @@ class Driver:
self.action = None
def setup(self, log_path='./data/logs/', headless=True):
"""
Browser configuration
"""
self.chrome_options = Options()
# Anti bot detection
@ -45,7 +51,6 @@ class Driver:
self.chrome_options.add_argument("--log-level=3")
self.chrome_options.add_argument("--silent")
# Disable save password
prefs = {'credentials_enable_service': False,
'profile.password_manager_enabled': False}
@ -54,7 +59,7 @@ class Driver:
# Set profile
self.chrome_options.add_argument(f'user-data-dir=./data/profiles/{self.profile}')
self.driver = webdriver.Chrome(f'./ChromeDriver/chromedriver',options=self.chrome_options, service_args=[f'--log-path={log_path}ChromeDriver.log'])
self.driver = webdriver.Chrome('./ChromeDriver/chromedriver',options=self.chrome_options, service_args=[f'--log-path={log_path}ChromeDriver.log'])
self.action = ActionChains(self.driver)
return self.driver, self.action

View File

@ -50,6 +50,9 @@ class Helpers:
return args
def logging_configuration(self, logging_level=logging.INFO, filename='data/logs/bot_globalexam.log'):
"""
Log configuration
"""
logging.basicConfig(filename=filename,
level=logging_level,
format='%(asctime)s - %(levelname)s - %(message)s')
@ -74,19 +77,19 @@ def header():
logging.info('==\t version : ' + const.VERSION + ' \t==')
logging.info('==\t=============================================================\t==')
def wait_between( min, max):
def wait_between( minimum, maximum):
"""
Wait random time in second beetween min and max seconds, to have an not linear behavior and be more human.
Wait random time in second beetween min and max seconds,
to have an not linear behavior and be more human.
"""
rand=uniform(min, max)
rand=uniform(minimum, maximum)
sleep(rand)
def TypeInField(element, xpath, myValue):
def TypeInField(element, xpath, value):
"""Type in a field"""
val = myValue
elem = element.find_element(by=By.XPATH, value=xpath)
for i in range(len(val)):
elem.send_keys(val[i])
for ele in enumerate(value):
elem.send_keys(ele[1])
wait_between(0.2, 0.4)
wait_between(0.4, 0.7)