-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
161 lines (126 loc) · 4.85 KB
/
main.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
160
161
import datetime
import os
import time
import json
import pytz
from exaroton import Exaroton
from croniter import croniter
from weebhook_send import WebhookSend
def check_and_install_requirements():
"""
Checks if the required libraries are installed and installs them if they are not.
Returns:
- None
"""
with open('requirements.txt') as f:
requirements = f.read().splitlines()
for lib in requirements:
try:
__import__(lib)
print(f'{lib} is already installed')
except ImportError:
os.system(f'pip install {lib}')
print(f'{lib} has been installed')
check_and_install_requirements()
# Load environment variables
exa = Exaroton(os.getenv("TOKEN"))
id_server = os.getenv("ID_SERVER")
timezone = pytz.timezone(os.getenv("TIMEZONE"))
cron_schedule_start = os.getenv("CRON_SCHEDULE_START")
cron_schedule_stop = os.getenv("CRON_SCHEDULE_STOP")
language = os.getenv("LANGUAGE")
def load_translations(lang: str) -> dict:
"""
Loads the translations from the language file.
Args:
- language (str): The language code to load the translations from.
Returns:
- dict: The translations are loaded from the language file.
"""
if not os.path.exists(f'lang/{lang}.json'):
raise FileNotFoundError(f'Language file not found: {lang}')
with open(f'lang/{lang}.json', 'r', encoding='utf-8') as file:
return json.load(file)
translations = load_translations(language)
def start_server():
"""
Starts the server using the Exaroton client and sends a notification via webhook.
Returns:
- None
"""
try:
print(exa.start(id_server))
WebhookSend.send_message(translations['server_started'])
print(translations['server_started'])
except Exception as er:
WebhookSend.send_message(translations['server_start_error'].format(error=er))
print(translations['server_start_error'].format(error=er))
def stop_server():
"""
Stops the server using the Exaroton client and sends a notification via webhook.
Returns:
- None
"""
try:
print(exa.stop(id_server))
WebhookSend.send_message(translations['server_stopped'])
print(translations['server_stopped'])
except Exception as er:
WebhookSend.send_message(translations['server_stop_error'].format(error=er))
print(translations['server_stop_error'].format(error=er))
def wait_until_next_cron(cron_schedule: str, timezone: pytz.timezone, action: str):
"""
Calculates the next scheduled time for the given cron and waits until that time.
Args:
- cron_schedule (str): The cron schedule string.
- timezone (pytz.timezone): The timezone to calculate the time in.
- action (str): Describes the action to be performed (start or stop) for logging.
Returns:
- None
"""
now_utc = datetime.datetime.now(pytz.utc)
cron = croniter(cron_schedule, now_utc)
next_run_time = cron.get_next(datetime.datetime)
next_run_time = next_run_time.astimezone(timezone)
delay_seconds = (next_run_time - now_utc).total_seconds()
delay_hours = delay_seconds / 3600
WebhookSend.send_message(translations[f'scheduled_{action}'].format(time=next_run_time.strftime("%H:%M"),
timezone=timezone, hours=f"{delay_hours:.2f}"))
time.sleep(delay_seconds)
def schedule_cron_task():
"""
Schedules tasks to start and stop the server based on the cron schedules.
It calculates the next run time for both start and stop, waits until those times,
and then executes the corresponding action.
This process repeats indefinitely.
Returns:
- None
"""
try:
while True:
# Wait until the next start time and start the server
wait_until_next_cron(cron_schedule_start, timezone, 'start')
start_server()
# Wait until the next stop time and stop the server
wait_until_next_cron(cron_schedule_stop, timezone, 'stop')
stop_server()
except Exception as er:
WebhookSend.send_message(translations['cron_task_error'].format(error=er))
print(translations['cron_task_error'].format(error=er))
if __name__ == '__main__':
"""
Main entry point of the script. Prints account and server information, then schedules the cron task.
Read -> readme.md for more information.
"""
try:
print("==" * 50)
print(translations['account_info'])
print(exa.get_account())
print("==" * 50)
print(translations['server_info'])
print(exa.get_server(id_server))
print("==" * 50)
schedule_cron_task()
except Exception as e:
WebhookSend.send_message(translations['main_script_error'].format(error=e))
print(translations['main_script_error'].format(error=e))