-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
156 lines (140 loc) · 5.15 KB
/
functions.py
File metadata and controls
156 lines (140 loc) · 5.15 KB
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
import historic_data
import twitter_data
import time
import urllib.request
import json
import http.client
import config
from datetime import datetime
import util
def calc_scores(date, team, opp, is_live):
team_score_h, opp_score_h, draw_score_h = historic_data.calc_historic(date, team, opp, is_live)
team_score_t, opp_score_t, draw_score_t = twitter_data.calc_twitter(team, opp, is_live, config.DEBUG)
team_h = team_score_h / (draw_score_h + team_score_h + opp_score_h)
opp_h = opp_score_h / (draw_score_h + team_score_h + opp_score_h)
draw_h = draw_score_h / (draw_score_h + team_score_h + opp_score_h)
team_t = team_score_t / (draw_score_t + team_score_t + opp_score_t)
opp_t = opp_score_t / (draw_score_t + team_score_t + opp_score_t)
draw_t = draw_score_t / (draw_score_t + team_score_t + opp_score_t)
return team_h, opp_h, draw_h, team_t, opp_t, draw_t
def calc_live(team, opp):
date = datetime.today()
date = date.strftime('%Y-%m-%d')
team_h, opp_h, draw_h, team_t, opp_t, draw_t = calc_scores(date, team, opp, True)
team_final = (0.3 * team_h) + (0.7 * team_t)
opp_final = (0.3 * opp_h) + (0.7 * opp_t)
draw_final = (0.3 * draw_h) + (0.7 * draw_t)
# Formatting...
draw_final = '{0:.2f}'.format(draw_final)
team_final = '{0:.2f}'.format(team_final)
opp_final = '{0:.2f}'.format(opp_final)
return team_final, opp_final, draw_final
def calc_future(date, team, opp):
team_h, opp_h, draw_h, team_t, opp_t, draw_t = calc_scores(date, team, opp, False)
team_final = (0.9 * team_h) + (0.1 * team_t)
opp_final = (0.9 * opp_h) + (0.1 * opp_t)
draw_final = (0.9 * draw_h) + (0.1 * draw_t)
# Formatting
draw_final = '{0:.2f}'.format(draw_final)
team_final = '{0:.2f}'.format(team_final)
opp_final = '{0:.2f}'.format(opp_final)
return team_final, opp_final, draw_final
def get_live_matches():
today = time.strftime("%Y-%m-%d")
# teams = '&team_id=9002%2C9158%2C9423%2C9287%2C9378%2C9260' - this statement uses specific teams
request_url = 'http://api.football-api.com/2.0/matches?comp_id=1204&match_date=' + today + '&Authorization=' + config.FOOTBALL_API_KEY
try:
url = urllib.request.Request(request_url)
data = urllib.request.urlopen(url).read().decode('utf-8', 'ignore')
data = json.loads(data)
except urllib.error.URLError as e:
print('Error getting multiple live matches')
if config.DEBUG is True:
print(request_url)
print(e.strerror)
data = ''
except UnicodeEncodeError as e:
data = ''
except http.client.BadStatusLine as e:
data = ''
except http.client.IncompleteRead as e:
data = ''
return data
def get_live_match(team):
today = time.strftime("%Y-%m-%d")
_, team_b, _ = util.get_id_by_name(team)
request_url = 'http://api.football-api.com/2.0/matches?comp_id=1204&team_id=' + str(team_b) + '&match_date=' + today + '&Authorization=' + config.FOOTBALL_API_KEY
try:
url = urllib.request.Request(request_url)
data = urllib.request.urlopen(url).read().decode('utf-8', 'ignore')
data = json.loads(data)
except urllib.error.URLError as e:
print('Single live match error')
print(request_url)
print(e.strerror)
data = ''
except UnicodeEncodeError as e:
data = ''
except http.client.BadStatusLine as e:
data = ''
except http.client.IncompleteRead as e:
data = ''
team, opponent, status, timer = '0', '0', '0', '0'
if data is not '':
print(data[0])
data = data[0]
timer = data['timer']
status = data['status']
if status == 'HT':
timer = 'HT'
elif status == 'FT':
timer = 'FT'
if timer is '':
if status is 'HT':
timer = 'HT'
elif status is 'FT':
timer = 'FT'
else:
timer = '0'
if data['localteam_id'] == str(team_b):
team = data['localteam_score']
opponent = data['visitorteam_score']
else:
team = data['visitorteam_score']
opponent = data['localteam_score']
if team is '':
team = '0'
if opponent is '':
opponent = '0'
return timer, team, opponent
def get_future_matches(team):
if team == 'Arsenal':
myteam = "Arsenal"
# 9002 team information football-api.com
id = 57
opps = util.obtain_opponents(57)
elif team == 'ManUtd':
myteam = "Man Utd"
id = 66
opps = util.obtain_opponents(66)
elif team == 'Watford':
myteam = "Watford"
id = 346
opps = util.obtain_opponents(346)
elif team == 'Everton':
myteam = "Everton"
id = 62
opps = util.obtain_opponents(62)
elif team == 'Newcastle':
myteam = "Newcastle"
id = 67
opps = util.obtain_opponents(67)
elif team == 'Stoke':
myteam = "Stoke City"
id = 70
opps = util.obtain_opponents(70)
else:
myteam = team
id = 57
opps = util.obtain_opponents(57)
return id, opps, myteam