-
Notifications
You must be signed in to change notification settings - Fork 1
/
odds.py
209 lines (192 loc) · 7.44 KB
/
odds.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
import sopel.module
from sopel.formatting import *
import requests
import urllib.parse
import pendulum
API_URL = "https://www.sportsline.com/sportsline-web/service/v1/odds?league={league}&auth=3"
NHL_URL = "https://www.sportsline.com/sportsline-web/service/v1/picksheet?league=nhl&includeVideo=false&auth=3"
VALID_LEAGUES = {
"cfb": "ncaaf",
"ncaaf": "ncaaf",
"nfl": "nfl",
"mlb": "mlb",
"nba": "nba",
"nhl": "nhl",
}
@sopel.module.commands('odds')
@sopel.module.example('.odds nfl ne')
def odds(bot, trigger):
"""Fetches odds for provided sport/team."""
query = trigger.group(2)
if not query:
return bot.reply("You need to give me at least a league!")
league = query.split()[0]
if league.lower() not in VALID_LEAGUES:
return bot.reply(f"{league} is not a valid league")
if league.lower() == "nhl":
# fetch NHL odds
try:
results = requests.get(NHL_URL).json()
except:
return bot.reply('ERROR: Something went wrong fetching odds')
else:
league = VALID_LEAGUES[league.lower()]
try:
url = API_URL.format(league=league)
results = requests.get(url).json()
except:
return bot.reply('ERROR: Something went wrong fetching odds')
team = []
tmp = query.split()
if len(tmp) > 1:
# we have a team(s)
for q in tmp[1:]:
if q.lower() == 'wsh' and tmp[0].lower() == 'mlb':
q = 'was'
team.append(q)
if league == "nhl":
# parse NHL odds
parsed = _parseNHLOdds(results)
else:
parsed = _parseOdds(results)
if not parsed:
return bot.reply("No odds found")
if team or len(parsed) == 1:
plucked = []
if team:
for tm in team:
for game in parsed:
if tm.lower() == game["home"].lower() or tm.lower() == game["away"].lower():
plucked.append(game)
if not plucked:
for game in parsed:
if tm.lower() in game["home_full"].lower() or tm.lower() in game["away_full"].lower():
plucked.append(game)
else:
plucked = parsed
if not plucked:
return bot.reply("No results found for '{}'".format(' '.join(team)))
for game in plucked:
if game.get("spread_team"):
if game["spread_team"] == game["home"]:
reply_string = "{} @ \x02{}[{}]({})\x02 (ml: {} {}) (o/u: {}) {}".format(
game["away_full"],
game["home_full"],
game["handicap"],
game["spread"],
game["ml_team"],
game["ml"],
game["ou"],
game["game_time"].in_tz("US/Eastern").format("ddd MMM Do h:mm A zz")
)
else:
reply_string = "\x02{}[{}]({})\x02 @ {} (ml: {} {}) (o/u: {}) {}".format(
game["away_full"],
game["handicap"],
game["spread"],
game["home_full"],
game["ml_team"],
game["ml"],
game["ou"],
game["game_time"].in_tz("US/Eastern").format("ddd MMM Do h:mm A zz")
)
else:
reply_string = "{} @ {} (no odds yet) {}".format(
game["away_full"],
game["home_full"],
game["game_time"].in_tz("US/Eastern").format("ddd MMM Do h:mm A zz")
)
bot.say(reply_string)
else:
reply_strings = []
for game in parsed:
if game.get("spread_team"):
if game["spread_team"] == game["home"]:
rs = "{} @ \x02{}[{}]\x02 {}".format(
game["away"],
game["home"],
game["handicap"],
game["game_time"].in_tz("US/Eastern").format("M/DD hh:mmA zz")
)
reply_strings.append(rs)
else:
rs = "\x02{}[{}]\x02 @ {} {}".format(
game["away"],
game["handicap"],
game["home"],
game["game_time"].in_tz("US/Eastern").format("M/DD hh:mmA zz")
)
reply_strings.append(rs)
else:
continue
bot.say(" | ".join(reply_strings), max_messages=6)
return
def _parseOdds(results):
asof = pendulum.from_timestamp(results["lastUpdatedAt"]/1000)
parsed = []
for comp in results["competitions"]:
tmpd = {}
tmpd["home"] = comp["homeTeamAbbreviation"]
tmpd["home_full"] = comp["homeTeamName"]
tmpd["away"] = comp["awayTeamAbbreviation"]
tmpd["away_full"] = comp["awayTeamName"]
tmpd["game_time"] = pendulum.from_timestamp(comp["gameStartFullDate"]/1000)
consensus = None
if comp.get("sportsbookCompetitionOdds"):
for sb in comp["sportsbookCompetitionOdds"]:
if sb["sportsbook"] == "consensus":
consensus = sb
break
if not consensus:
tmpd["tbd"] = "No odds found"
else:
try:
tmpd["ou"] = consensus["overUnder"]["total"]
except:
tmpd["ou"] = "Even"
try:
tmpd["ml"] = consensus["moneyLine"]["favoredTeamOdds"]
tmpd["ml_team"] = consensus["moneyLine"]["favoredTeamAbbreviation"]
except:
tmpd["ml"] = ""
tmpd["ml_team"] = ""
try:
tmpd["spread"] = consensus["spread"]["favoredTeamOdds"]
tmpd["spread_team"] = consensus["spread"]["favoredTeamAbbreviation"]
tmpd["handicap"] = consensus["spread"]["handicap"]
except:
tmpd["spread"] = ""
tmpd["spread_team"] = ""
tmpd["handicap"] = ""
parsed.append(tmpd)
return parsed
def _parseNHLOdds(results):
asof = pendulum.from_timestamp(results["dataLastUpdated"]/1000)
parsed = []
for comp in results["picks"]:
tmpd = {}
tmpd["home"] = comp["homeTeamAbbrv"]
tmpd["home_full"] = comp["homeTeamName"] + " " + comp["homeTeamNickName"]
tmpd["away"] = comp["awayTeamAbbrv"]
tmpd["away_full"] = comp["awayTeamName"] + " " + comp["awayTeamNickName"]
tmpd["game_time"] = pendulum.from_timestamp(comp["gameStartFullDate"]/1000)
try:
tmpd["ou"] = comp["ouOddLabel"]
except:
tmpd["ou"] = "Even"
try:
tmpd["ml"] = comp["mlOddLabel"].split()[1]
tmpd["ml_team"] = comp["mlOddLabel"].split()[0]
except:
tmpd["ml"] = ""
tmpd["ml_team"] = ""
try:
tmpd["spread"] = consensus["spread"]["favoredTeamOdds"]
tmpd["spread_team"] = consensus["spread"]["favoredTeamAbbreviation"]
tmpd["handicap"] = consensus["spread"]["handicap"]
except:
tmpd["spread"] = tmpd["ml"]
tmpd["spread_team"] = tmpd["ml_team"]
tmpd["handicap"] = tmpd["ml"]
parsed.append(tmpd)
return parsed