-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStartup.py
96 lines (70 loc) · 3.44 KB
/
Startup.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
from dotenv import load_dotenv
import Log
import DBStuffs
import json
import os
import sys
import getpass
import subprocess
#############################################################################################################
# Gets env, settings file & DB vars then determines & returns the correct value to save
def Get_Variable(VariableName):
Log.Verbose("Determining {}".format(VariableName))
return os.getenv(VariableName)
#############################################################################################################
# Checks if mandatory fields have been provided, Exits with appropriate message if not
def Evaluate_Validity(Field):
if DBStuffs.DB_FetchSingle(Field, "Instance") == "":
print("{} not specified, exiting...".format(Field))
sys.exit(0)
#############################################################################################################
# Evaluates Validity for each mandatory variable
def Evaluate_AllFields():
Evaluate_Validity("TG_Bot_ID")
Evaluate_Validity("CRON_SCHEDULE")
#############################################################################################################
# Populate DB vars with values from env / settings file
def PopulateInstanceVars():
DBStuffs.DB_CreateDatabase()
DBStuffs.DB_UpdateSettings("TG_Bot_ID", Get_Variable("TG_Bot_ID"))
DBStuffs.DB_UpdateSettings("SERVER_PASSWORD", Get_Variable("SERVER_PASSWORD"))
DBStuffs.DB_UpdateSettings("CRON_SCHEDULE", Get_Variable("CRON_SCHEDULE"))
Evaluate_AllFields()
#############################################################################################################
# Sets up the cron job
def SetUpCronSchedule():
CurrentDir = os.getcwd()
Schedule = DBStuffs.DB_FetchSingle("CRON_SCHEDULE", "Instance")
Log.Verbose("Directory: {}/Scraper.py".format(CurrentDir))
Log.Verbose("Schedule: {}".format(Schedule))
os.system('echo "{0} root cd /app && /usr/local/bin/python {1}/Scraper.py >> /dev/stdout\n" >> /etc/cron.d/EpicScraper'.format(Schedule, CurrentDir))
os.system('chmod 644 /etc/cron.d/EpicScraper')
os.system('/etc/init.d/cron reload')
os.system('service cron restart')
Log.Information("Cron schedule set")
#############################################################################################################
# Displays a server password if the password is enabled
def DisplayServerPassword():
TopSecret = DBStuffs.DB_FetchSingle("SERVER_PASSWORD", "Instance")
if TopSecret != "":
Output = ""
Output += "\n"
Output += "##################################\n"
Output += "### Server Password:\n"
Output += "### {}\n".format(TopSecret)
Output += "##################################"
Output += "\n"
print(Output)
#############################################################################################################
# Orchestrator method
def ControlMethod():
load_dotenv()
PopulateInstanceVars()
SetUpCronSchedule()
DisplayServerPassword()
Log.Information("Performing Initial Scrape")
subprocess.Popen("python Scraper.py", shell = True, stdout=subprocess.PIPE) # First Run of Scraper
Log.Information("Starting Bot")
subprocess.run("python EpicScraperV2.py", shell = True, stdout=subprocess.PIPE) # Run Telegram Bot
#############################################################################################################
ControlMethod()