-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
178 lines (136 loc) · 5.21 KB
/
handler.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
import json, config, requests, random
from twython import Twython, TwythonError
""" Example method
def retweet(event, context):
twitter = Twython(config.APP_KEY, config.APP_SECRET, config.OAUTH_TOKEN, config.OAUTH_TOKEN_SECRET)
search_results = twitter.search(q='serverless', result_type='mixed', lang='en')
message = ""
for tweet in search_results['statuses']:
try:
twitter.retweet(id=tweet['id'])
message = f"Retweeted \"{tweet['text']}\" by {tweet['user']['name']}"
twitter.create_friendship(id=tweet['user']['id'])
break
except TwythonError:
pass
body = {
"message": message,
"input": event
}
response = {
"statusCode": 200,
"body": json.dumps(body)
}
return response
"""
def get_random_key():
#sport_keys = {"NBA": "basketball_nba", "MMA": "mma_mixed_martial_arts", "NCAAB": "basketball_ncaab", "NHL": "icehockey_nhl"}
sport_keys = ["basketball_nba", "basketball_ncaab", "icehockey_nhl", "americanfootball_nfl"]
sports_response = requests.get('https://api.the-odds-api.com/v3/sports', params={'api_key': config.API_KEY})
sports_json = json.loads(sports_response.text)
if not sports_json['success']:
print(
'There was a problem with the sports request:',
sports_json['msg']
)
else:
print()
print(
'Successfully got {} sports'.format(len(sports_json['data'])),
'Here\'s all the sports:'
)
inseason_keys = []
for item in sports_json["data"]:
if item in sport_keys:
inseason_keys.append(item)
return random.choice(sport_keys)
# This requests current odds data and calls get_best_odds to generate tweets
def update_odds_json(sport_key):
region = "us"
mkt = "h2h"
# request odds for the key
odds_response = requests.get('https://api.the-odds-api.com/v3/odds', params={
'api_key': config.API_KEY,
'sport': sport_key,
'region': region, # uk | us | eu | au
'mkt': mkt # h2h | spreads | totals
})
odds_json = json.loads(odds_response.text)
if not odds_json['success']:
print('There was a problem with the odds request:' + odds_json['msg'])
else:
# Write todays odds to file
# with open('data/NBAodds.txt', 'w') as outfile:
# json.dump(odds_json, outfile)
# odds_json['data'] contains a list of live and
# upcoming events and odds for different bookmakers.
# Events are ordered by start time (live events are first)
print('\nSuccessfully got {} events. Here\'s the first event:'.format( len(odds_json['data']) ) )
print(odds_json['data'][0])
# Log odd api usage
print('\nRemaining requests', odds_response.headers['x-requests-remaining'])
print('Used requests', odds_response.headers['x-requests-used'])
return odds_json
# This determines the best odds and generates the tweet
def get_best_odds(odds_json):
# with open('data/NBAodds.txt') as odds:
# odds_json = json.load(odds)
tweets = []
for i, game in enumerate(odds_json["data"]):
game = odds_json["data"][i]
sites = game["sites"]
team1 = {"team": game["teams"][0], "odds": 0, "site": ""}
team2 = {"team": game["teams"][1], "odds": 0, "site": ""}
for site in sites:
key = site["site_key"]
team1_odds = site["odds"]["h2h"][0]
team2_odds = site["odds"]["h2h"][1]
if team1_odds > team1["odds"]:
team1["odds"] = team1_odds
team1["site"] = site["site_nice"]
if team2_odds > team2["odds"]:
team2["odds"] = team2_odds
team2["site"] = site["site_nice"]
if team1["odds"] < team2["odds"]:
favorite = team1
underdog = team2
else:
favorite = team2
underdog = team1
tweets.append("{} ({}) vs. {} ({})\n\nFavorite odds from {}.\nUnderdog odds from {}.".format(favorite["team"], favorite["odds"], underdog["team"], underdog["odds"], favorite["site"], underdog["site"]))
return tweets
def generate_tweets():
tweets = []
#if no tweets generated, try another key
while len(tweets) == 0:
#key = get_random_key()
key = "basketball_nba"
odds_json = update_odds_json(key)
tweets = get_best_odds(odds_json)
return tweets
# this generates and tweets the bet
def tweet_bet(event, context):
twitter = Twython(config.APP_KEY, config.APP_SECRET, config.OAUTH_TOKEN, config.OAUTH_TOKEN_SECRET)
tweets = generate_tweets()
error = ["(0)", "Favorite odds from .", "underdog odds from ."]
bet = ""
while len(bet) == 0:
bet = random.choice(tweets)
for item in error:
if item in bet:
bet = ""
try:
twitter.update_status(status=bet)
message = "Tweeted bet: " + bet
except:
message = "ERROR TWEETING BET"
pass
body = {
"message": message,
"input": event
}
response = {
"statusCode": 200,
"body": json.dumps(body)
}
return response