-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatistician.py
230 lines (182 loc) · 7.33 KB
/
statistician.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
from git import Repo
from handballStats import handballStats
from datetime import datetime
import time
import sys
import os
import smtplib
import ssl
import json
def statistician():
"""
Runs handballStats on Wednesdays, Saturdays and Sundays between 10:00 and 23:00 pm
:return: does not return anything, saves output_csvs and pushes changes to github
"""
while True:
# check if it is wednesday, saturday or sunday and between 22:00 and 24:00
start = datetime.now()
today = datetime.now().weekday()
time = datetime.now().time().strftime("%H:%M:%S")
if (today in (2,5,6)) and (int(time.split(':')[0]) in range(10,24,1)):
interval = 45
print(f'\n\n\n{"#"*10}\nit is {datetime.now().strftime("%d.%m.%Y %H:%M:%S")}, and I shall get statistics...\n{"#"*10}')
# scrape data
try:
handballStats()
print('successfully scraped the stats')
except Exception as e:
print(e)
log(f'Error @ {datetime.now().strftime("%d.%m.%Y %H:%M:%S")}', str(e))
#mailAlert(str(e))
sleep_minutes(0.1)
# clean up the Gameprogressions Folder (kick out games, that have not been played yet"
try:
print('getting rid of games which are played in the future...')
cleanupGameProgressions()
except Exception as e:
print(e)
log(f'Error @ {datetime.now().strftime("%d.%m.%Y %H:%M:%S")}', str(e))
#emailAlert(str(e))
#sys.exit()
sleep_minutes(0.1)
# push updates to github
print('-' *15, '\npushing to origin...')
try:
git_push()
except Exception as e:
print(e)
log(f'Error @ {datetime.now().strftime("%d.%m.%Y %H:%M:%S")}', str(e))
#emailAlert(str(e))
else:
# if its a day for updates, set interval to 1h, otherwise 2h
if today in (2,5,6):
interval = 60
else:
interval = 240
print(f'i sleep @ {datetime.now().strftime("%d.%m.%Y %H:%M:%S")}')
try:
print('housekeeping...')
cleanupGameProgressions()
print('house kept :)')
except Exception as e:
print(e)
log(f'Error @ {datetime.now().strftime("%d.%m.%Y %H:%M:%S")}', str(e))
#emailAlert(str(e))
#sys.exit()
# wait for 45 minutes before checking again
end = datetime.now()
log(f'Finished a run which took {end-start}')
print('-' * 15)
sleep_minutes(interval)
def sleep_minutes(minutes):
"""
sleeps the execution of a program for the amount of minutes specified
:param minutes: amount of minutes to sleep the execution of the program
:return: executes time.sleep with the specified amount of minutes
"""
time.sleep(minutes * 60)
def git_push():
"""
pushes changes to the git repo, if there have been changes
:return: does not return anything, pushes to remote github repo
"""
PATH_OF_GIT_REPO = r'.git' # make sure .git folder is properly configured
try:
repo = Repo(PATH_OF_GIT_REPO)
# add all changes
repo.git.add(A=True)
status = repo.git.status()
diff = repo.index.diff('HEAD')
print(status)
if len(diff)>0:
# if there are changes in the files, push changes to github
COMMIT_MESSAGE = f'statistician @ {datetime.now().strftime("%d.%m.%Y %H:%M:%S")}'
print(f'commit message: {COMMIT_MESSAGE}')
repo.index.commit(COMMIT_MESSAGE)
origin = repo.remote(name='origin')
origin.push()
print('push successful')
else:
print('nothing to push, skipping...')
except Exception as e:
print('Error while pushing to remote repository (in git_push): ')
print(e)
def cleanupGameProgressions():
"""
Helper function. Gets rid of csvs from games that have not even been played yet
:return: does not return, housekeeping function
"""
gp_path = './output_csv/gameProgressions'
teams = os.listdir(gp_path)
for team in teams:
dir_path = f'{gp_path}/{team}'
seasons = os.listdir(dir_path)
for season in seasons:
g_path = f'{dir_path}/{season}'
games = os.listdir(g_path)
for game in games:
if game == 'median_performance.csv':
pass
else:
#check if game is played in the future, if so delete file
game_d = game[:8].split('_')
game_d[0] = '20'+game_d[0]
game_d.reverse()
game_date = '.'.join(game_d)
current_date = datetime.now().strftime('%d.%m.%Y')
t_format = '%d.%m.%Y'
game_played = datetime.strptime(game_date, t_format)
today = datetime.strptime(current_date, t_format)
#if the game in question is played in the future, delete the csv
if game_played > today or game.split('_')[4].isspace():
os.remove(f'{g_path}/'+game)
def emailAlert(message):
"""
sends error message to let me know if statistician failed somehow
:param message: string message to send
:return: sends message via email
"""
# load credentials
with open('gmail.credentials') as cred:
credentials = json.load(cred)
timestamp = datetime.now().strftime("%d.%m.%Y %H:%M:%S")
gmail_acc = credentials['user']
pw = credentials['pw']
# define receiving account
receiver = credentials['receiver']
cred.close()
# construct message
msg = f'@{timestamp}\n' \
f'statistician.py @ raspberry failed. Error message:\n{"-"*10}\n\n' \
f'{message}'
# set up email
port = 465
smtp_server = "smtp.gmail.com"
context = ssl.create_default_context()
# send email
print(f'Sending email about crash to {receiver}...')
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(gmail_acc, pw)
server.sendmail(gmail_acc, receiver, msg)
server.quit()
print(f'Sent email from {gmail_acc} to {receiver}')
def log(message, errormessage=None):
"""
Logs messages to a logfile, for debugging
:param message: string to log
:param errormessage: (voluntary) error message
:return: does not return, logs to file
"""
with open('handballstats.log', 'a') as logfile:
logfile.write('-'*10)
logfile.write(f'\n log @{datetime.now().strftime("%d.%m.%Y %H:%M:%S")}\n')
logfile.write(message)
if errormessage is not None:
logfile.write('\n')
logfile.write('#'*10)
logfile.write(errormessage)
logfile.write('#' * 10)
logfile.write('-'*10)
logfile.write('\n\n')
if __name__ == '__main__':
statistician()