Merge pull request #3 from j4rj4r/fix-use-all-categories

Fix - Use all categories
This commit is contained in:
Jarjar 2023-02-07 22:21:18 +01:00 committed by GitHub
commit f8192e0011
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 52 additions and 39 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -50,6 +50,9 @@ class Helpers:
return args return args
def logging_configuration(self, logging_level=logging.INFO, filename='data/logs/bot_globalexam.log'): def logging_configuration(self, logging_level=logging.INFO, filename='data/logs/bot_globalexam.log'):
"""
Log configuration
"""
logging.basicConfig(filename=filename, logging.basicConfig(filename=filename,
level=logging_level, level=logging_level,
format='%(asctime)s - %(levelname)s - %(message)s') format='%(asctime)s - %(levelname)s - %(message)s')
@ -74,19 +77,19 @@ def header():
logging.info('==\t version : ' + const.VERSION + ' \t==') logging.info('==\t version : ' + const.VERSION + ' \t==')
logging.info('==\t=============================================================\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) sleep(rand)
def TypeInField(element, xpath, myValue): def TypeInField(element, xpath, value):
"""Type in a field""" """Type in a field"""
val = myValue
elem = element.find_element(by=By.XPATH, value=xpath) elem = element.find_element(by=By.XPATH, value=xpath)
for i in range(len(val)): for ele in enumerate(value):
elem.send_keys(val[i]) elem.send_keys(ele[1])
wait_between(0.2, 0.4) wait_between(0.2, 0.4)
wait_between(0.4, 0.7) wait_between(0.4, 0.7)