-
Notifications
You must be signed in to change notification settings - Fork 4
/
modules.py
106 lines (95 loc) · 3.94 KB
/
modules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import undetected_chromedriver as uc
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import logging
import logging.config
import time
import os
import glob
import requests
import re
class Modules:
def __init__(self, driver=False) -> None:
self.driver = driver
logging.config.dictConfig({
'version': 1,
'disable_existing_loggers': True,
})
logging.basicConfig(level=logging.DEBUG)
self.logger = logging.getLogger(__name__)
self.Wait = WebDriverWait(driver, 30)
def dataMatchColourPercentage(self, element) -> str:
match element.value_of_css_property("Color"):
case "rgb(0, 207, 131)":
return "Human"
case "rgb(250, 181, 21)":
return "Unsure"
case "rgb(255, 0, 0)":
return "AI"
def dataPercentageToWord(self, percentage) -> str:
try:
percentage = int(float(percentage))
except:
percentage = int(float(percentage.strip("%")))
if percentage > 70:
return "AI"
elif percentage < 30:
return "Human"
else:
return "Unsure"
def dataHumanToAI(self, human_percentage) -> int:
try:
human_percentage = int(float(human_percentage))
except:
human_percentage = int(float(human_percentage.strip("%")))
ai_percentage = 100 - int(float(human_percentage))
return ai_percentage
def doGetPath(self) -> list[str]:
check_path = input("Please provide the path of the .txt file, or a folder containing txt files that you would like to check\n")
if os.path.exists(check_path) == False:
print("The path you provided does not exist")
exit()
if os.path.isfile(check_path):
files_to_check = [check_path]
print(f"The file that is going to be checked is: {files_to_check}")
elif os.path.isdir(check_path):
files_to_check = glob.glob(f"{check_path}\\*.txt")
print("The files that are going to be checked are:")
for fileCur in files_to_check:
print(fileCur)
return files_to_check
def doDeleteElement(self, element: uc.webelement.WebElement):
self.driver.execute_script("""
var element = arguments[0];
element.parentNode.removeChild(element);
""", element)
def doWaitUntilText(self, xpath: str) -> uc.webelement.WebElement:
self.Wait.until(EC.presence_of_element_located((By.XPATH, xpath)))
element = self.driver.find_element(By.XPATH, xpath)
self.Wait.until(EC.visibility_of(element))
while element.text == "":
time.sleep(0.5)
return element
def doScrollToElement(self, element: uc.webelement.WebElement):
self.driver.execute_script("arguments[0].scrollIntoView();", element)
scroll_element_into_middle = """
var viewPortHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
var elementTop = arguments[0].getBoundingClientRect().top;
window.scrollBy(0, elementTop-(viewPortHeight/2));
"""
self.driver.execute_script(scroll_element_into_middle, element)
time.sleep(0.5)
def doGetLatestVersion(self):
url = f"https://api.github.com/repos/Nygosaki/AI-Writing-Detection/commits"
response = requests.get(url)
response.raise_for_status() # Raise exception if invalid response
commits = response.json()
if commits:
latest_commit = commits[0]
message = latest_commit['commit']['message']
# Search for version string in commit message
match = re.search(r"Version: (\d+\.\d+\.\d+)", message)
if match:
return match.group(1)
return None