-
Notifications
You must be signed in to change notification settings - Fork 91
/
connect_and_launch.py
159 lines (124 loc) · 5.21 KB
/
connect_and_launch.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import asyncio
import time
import os
import logging
from selenium import webdriver
from selenium.common.exceptions import ElementNotInteractableException, \
NoSuchElementException
from dotenv import load_dotenv
from chromedriver_py import binary_path
load_dotenv()
USER = os.getenv('USERNAME_C')
PASSWORD = os.getenv('PASSWORD_C')
URL = "https://aternos.org/go/"
# chrome variables
adblock = False # for those with network wide ad blockers
headless = True # if you want a headless window
options = webdriver.ChromeOptions()
if headless:
options.add_argument('--headless')
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/87.0.4280.88 Safari/537.36")
driver = webdriver.Chrome(options=options, executable_path=binary_path)
async def start_server():
""" Starts the server by clicking on the start button.
The try except part tries to find the confirmation button, and if it
doesn't, it continues to loop until the confirm button is clicked."""
element = driver.find_element_by_xpath("//*[@id=\"start\"]")
element.click()
await asyncio.sleep(3)
# hides the notification question
driver.execute_script('hideAlert();')
# server state span
while get_status() == "Waiting in queue":
# while in queue, check for the confirm button and try click it
await asyncio.sleep(3)
try:
element = driver.find_element_by_xpath('//*[@id="confirm"]')
element.click()
except ElementNotInteractableException:
pass
def get_status():
""" Returns the status of the server as a string."""
return driver.find_element_by_xpath('//*[@id="nope"]/main/section/div['
'3]/div[3]/div[1]/div/span['
'2]/span').text
def get_number_of_players():
""" Returns the number of players as a string.
Works: When server is online--Returns 0 if offline"""
try:
return driver.find_element_by_xpath('//*[@id="nope"]/main/section'
'/div[3]/div[5]/div[2]/div['
'1]/div[1]/div[2]/div[2]').text
except NoSuchElementException:
# Can't be 0/20 because max isn't always the same,
# could maybe pull max players from options page
return '0'
def get_ip():
""" Returns the severs IP address.
Works: Always works"""
return driver.find_element_by_xpath('//*[@id="nope"]/main/section/div['
'3]/div[1]').text[:-8]
def get_software():
""" Returns the server software.
Works: Always works"""
return driver.find_element_by_xpath('//*[@id="software"]').text
def get_version():
""" Returns the server version.
Works: Always works"""
return driver.find_element_by_xpath('//*[@id="version"]').text
def get_tps():
""" Returns the server TPS
Works; When the server is online--Returns '0' if offline"""
try:
return driver.find_element_by_xpath('//*[@id="nope"]/main/section'
'/div[3]/div[5]/div[2]/div['
'1]/div[3]/div[2]/div[2]').text
except NoSuchElementException:
return '0'
def get_server_info():
""" Returns a string of information about the server
Returns: server_ip, server_status, number of players, software,
version"""
return get_ip(), get_status(), get_number_of_players(), \
get_software(), get_version(), get_tps()
def connect_account():
""" Connects to the accounts through a headless chrome tab so we don't
have to do it every time we want to start or stop the server."""
driver.get(URL)
# login to aternos
element = driver.find_element_by_xpath('//*[@id="user"]')
element.send_keys(USER)
element = driver.find_element_by_xpath('//*[@id="password"]')
element.send_keys(PASSWORD)
element = driver.find_element_by_xpath('//*[@id="login"]')
element.click()
time.sleep(2)
# selects server from server list
element = driver.find_element_by_css_selector('body > div > main > section'
'> div > div.servers.single '
'> div > div.server-body')
element.click()
# by passes the 3 second adblock
if adblock:
adblockBypass()
logging.info('Aternos Tab Loaded')
def adblockBypass():
time.sleep(1)
element = driver.find_element_by_xpath('//*[@id="sXMbkZHTzeemhBrPtXgBD'
'DwAboVOOFxHiMjcTsUwoIOJ"]/div/'
'div/div[3]/div[2]/div[3]/div'
'[1]')
element.click()
time.sleep(3)
logging.debug("Adblock Wall Bypassed")
async def stop_server():
""" Stops server from aternos panel."""
element = driver.find_element_by_xpath("//*[@id=\"stop\"]")
element.click()
def quitBrowser():
""" Quits the browser driver cleanly."""
driver.quit()
def refreshBrowser():
driver.refresh()