-
Notifications
You must be signed in to change notification settings - Fork 3
/
yahoo_fantasy_sports.py
78 lines (62 loc) · 3.18 KB
/
yahoo_fantasy_sports.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
import json
import time
import webbrowser
from rauth import OAuth1Service
from rauth.utils import parse_utf8_qsl
class YahooFantasySports:
def __init__(self, credentials_file):
"""
Authenticate with the Yahoo Fantasy Sports API.
Parameters:
credentials_file: path to a json file
"""
# load credentials
self.credentials_file = open(credentials_file)
self.credentials = json.load(self.credentials_file)
self.credentials_file.close()
# create oauth object
self.oauth = OAuth1Service(consumer_key = self.credentials['consumer_key'],
consumer_secret = self.credentials['consumer_secret'],
name = "yahoo",
request_token_url = "https://api.login.yahoo.com/oauth/v2/get_request_token",
access_token_url = "https://api.login.yahoo.com/oauth/v2/get_token",
authorize_url = "https://api.login.yahoo.com/oauth/v2/request_auth",
base_url = "http://fantasysports.yahooapis.com/")
# leg 1
request_token, request_token_secret = self.oauth.get_request_token(params={"oauth_callback": "oob"})
# leg 2
authorize_url = self.oauth.get_authorize_url(request_token)
webbrowser.open(authorize_url)
verify = input('Enter code: ')
# leg 3
raw_access = self.oauth.get_raw_access_token(request_token,
request_token_secret,
params={"oauth_verifier": verify})
self.parsed_access_token = parse_utf8_qsl(raw_access.content)
access_token = (self.parsed_access_token['oauth_token'], self.parsed_access_token['oauth_token_secret'])
# log time
self.start_time = time.time()
self.end_time = self.start_time + 3600
# store tokens
self.credentials['access_token'] = self.parsed_access_token['oauth_token']
self.credentials['access_token_secret'] = self.parsed_access_token['oauth_token_secret']
self.tokens = (self.credentials['access_token'], self.credentials['access_token_secret'])
# start session
self.session = self.oauth.get_session(self.tokens)
def refresh_tokens(self):
"""
Refresh sessions tokens.
"""
# refresh a session
self.tokens = self.oauth.get_access_token(self.parsed_access_token['oauth_token'],
self.parsed_access_token['oauth_token_secret'],
params={'oauth_session_handle':self.parsed_access_token['oauth_session_handle']}
)
# update stored tokens
self.credentials['access_token'] = self.tokens[0]
self.credentials['access_token_secret'] = self.tokens[1]
# update log time
self.start_time = time.time()
self.end_time = self.start_time + 3600
# start a session with updated tokens
self.session = self.oauth.get_session(self.tokens)