-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathscore_matchup.py
executable file
·171 lines (159 loc) · 5.16 KB
/
score_matchup.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
#!/usr/bin/env python3
import os, os.path
import sys, getopt
from datetime import datetime
from pathlib import Path
import pdb
from pynotifier import NotificationClient, Notification
from pynotifier.backends import platform
import subprocess
from collections import OrderedDict
import pandas as pd
from collections import OrderedDict
import json
import settings
import pyBlitz
def GetShorterTeams(f, s, db):
first = ""
second = ""
for item in db["displayName"]:
if f == db["displayName"][item]:
first = db["shortDisplayName"][item]
if s == db["displayName"][item]:
second = db["shortDisplayName"][item]
if first and second:
break
return first, second
def ParseResult(s):
result={}
y = s.splitlines()
t1 = y[1].split("data: ")
t2 = y[3].split("data: ")
chk = y[4].split("form: ")
chk2 = chk[1].split("|")
result["first"] = t1[1]
result["second"] = t2[1]
if "TRUE" in chk2[0]:
verbose = True
else:
verbose = False
if "TRUE" in chk2[1]:
neutral = True
else:
neutral = False
result["verbose"] = verbose
result["neutral"] = neutral
return result
def CurrentStatsFile(filename):
if (not os.path.exists(filename)):
return False
stat = os.path.getmtime(filename)
stat_date = datetime.fromtimestamp(stat)
if stat_date.date() < datetime.now().date():
return False
return True
def RefreshStats():
import scrape_teams
import scrape_bornpowerindex
import scrape_teamrankings
import scrape_schedule
import scrape_espn_odds
import combine_stats
def main(argv):
cwd = os.getcwd()
print("... retrieving teams spreadsheet")
team_file = '{0}teams.xlsx'.format(settings.data_path)
if (os.path.exists(team_file)):
excel_df = pd.read_excel(team_file, sheet_name='Sheet1')
team_json = json.loads(excel_df.to_json())
else:
print (" *** run scrape_teams and then come back ***")
exit()
first = ""
second = ""
neutral = False
test = False
try:
opts, args = getopt.getopt(argv, "hf:s:nt", ["help", "first=", "second=", "neutral","test"])
except getopt.GetoptError as err:
print(err)
usage()
sys.exit(2)
output = None
for o, a in opts:
if o in ("-n", "--neutral"):
neutral = True
elif o in ("-t", "--test"):
test = True
elif o in ("-h", "--help"):
usage()
exit()
elif o in ("-f", "--first"):
first = a
elif o in ("-s", "--second"):
second = a
else:
assert False, "unhandled option"
print ("Score Matchup Tool")
print ("**************************")
usage()
print ("**************************")
if (test):
testResult = pyBlitz.Test()
if (testResult):
print ("Test result - pass")
else:
print ("Test result - fail")
else:
run_gui = '{0}/score_matchup_gui.sh'.format(cwd)
from_bash = subprocess.run([run_gui], capture_output=True)
result = ParseResult(from_bash.stdout.decode("utf-8"))
first, second = GetShorterTeams(result["first"], result["second"], team_json)
verbose = result["verbose"]
neutral = result["neutral"]
if (not first and not second):
print ("Score Matchup Tool")
print ("**************************")
usage()
print ("**************************")
exit()
Path(settings.data_path).mkdir(parents=True, exist_ok=True)
stat_file = "{0}json/stats.json".format(settings.data_path)
if (not CurrentStatsFile(stat_file)):
RefreshStats()
ds = {}
settings.exceptions = []
ds = pyBlitz.Calculate(first, second, neutral)
if (settings.exceptions):
print (" ")
print ("\t*** Warnings ***")
for item in settings.exceptions:
print (item)
print ("\t*** Warnings ***")
print (" ")
if (not ds):
exit()
if (neutral):
answer = "{0} {1}% vs {2} {3}% {4}-{5}".format(ds["teama"], ds["chancea"], ds["teamb"], ds["chanceb"], \
ds["scorea"], ds["scoreb"])
else:
answer = "{0} {1}% at {2} {3}% {4}-{5}".format(ds["teama"], ds["chancea"], ds["teamb"], ds["chanceb"], \
ds["scorea"], ds["scoreb"])
print (answer)
blitz_icon = '{0}/football.ico'.format(cwd)
c = NotificationClient()
c.register_backend(platform.Backend())
notification = Notification(title='pyBlitz Predictor', message=answer,\
icon_path=blitz_icon, duration=20)
c.notify_all(notification)
def usage():
usage = """
-h --help Prints this
-f --first First Team (The Away Team)
-s --second Second Team (The Home Team)
-n --neutral Playing on a neutral Field
-t --test runs test routine to check calculations
"""
print (usage)
if __name__ == "__main__":
main(sys.argv[1:])