def test_update_social_status(self): """Updates status""" y = yql.ThreeLegged(YQL_API_KEY, YQL_SHARED_SECRET) timestamp = time() query = """UPDATE social.profile.status SET status='Using YQL. %s Update' WHERE guid=me""" % timestamp token_store = FileTokenStore(CACHE_DIR, secret='gsfdsfdsfdsfs') stored_token = token_store.get('foo') if not stored_token: # Do the dance request_token, auth_url = y.get_token_and_auth_url() print "Visit url %s and get a verifier string" % auth_url verifier = raw_input("Enter the code: ") token = y.get_access_token(request_token, verifier) token_store.set('foo', token) else: # Check access_token is within 1hour-old and if not refresh it # and stash it token = y.check_token(stored_token) if token != stored_token: token_store.set('foo', token) res = y.execute(query, token=token) assert res.rows[0] == "ok" new_query = """select message from social.profile.status where guid=me""" res = y.execute(new_query, token=token) assert res.rows[0].get("message") == "Using YQL. %s Update" % timestamp
def test_update_meme_status(self): """Updates status""" y = yql.ThreeLegged(YQL_API_KEY, YQL_SHARED_SECRET) query = 'INSERT INTO meme.user.posts (type, content) VALUES("text", "test with pythonyql")' token_store = FileTokenStore(CACHE_DIR, secret='fjdsfjllds') store_name = "meme" stored_token = token_store.get(store_name) if not stored_token: # Do the dance request_token, auth_url = y.get_token_and_auth_url() print "Visit url %s and get a verifier string" % auth_url verifier = raw_input("Enter the code: ") token = y.get_access_token(request_token, verifier) token_store.set(store_name, token) else: # Check access_token is within 1hour-old and if not refresh it # and stash it token = y.check_token(stored_token) if token != stored_token: token_store.set(store_name, token) # post a meme res = y.execute(query, token=token) assert y.uri == "http://query.yahooapis.com/v1/yql" assert res.rows[0].get("message") == "ok" pubid = None if res.rows[0].get("post") and res.rows[0]["post"].get("pubid"): pubid = res.rows[0]["post"]["pubid"] # Delete the post we've just created query = 'DELETE FROM meme.user.posts WHERE pubid=@pubid' res2 = y.execute(query, token=token, params={"pubid": pubid}) assert res2.rows[0].get("message") == "ok"
def test_get_access_token_request3(self): y = yql.ThreeLegged('test', 'test-does-not-exist', httplib2_inst=httplib2.Http()) new_token = yql.YahooToken('test', 'test2') new_token.session_handle = 'sess_handle_test' token = y.refresh_token(token=new_token) self.assertTrue(hasattr(token, 'key')) self.assertTrue(hasattr(token, 'secret'))
def test_y_token_without_timestamp_raises2(self): def refresh_token_replacement(token): return 'replaced' y = yql.ThreeLegged('test', 'test2') y.refresh_token = refresh_token_replacement token = yql.YahooToken('test', 'test2') token.timestamp = 11111 self.assertEqual(y.check_token(token), 'replaced')
def test_three_legged_execution_raises_value_error_with_invalid_uri(self): y = yql.ThreeLegged('test', 'test2', httplib2_inst=httplib2.Http()) y.uri = "fail" token = yql.YahooToken('tes1t', 'test2') y.execute("SELECT foo meh meh ", token=token)
def test_three_legged_execution(self): query = 'SELECT * from foo WHERE dog=@dog' y = yql.ThreeLegged('test', 'test2', httplib2_inst=httplib2.Http()) token = yql.YahooToken('test', 'test2') content = y.execute(query, {"dog": "fifi"}, token=token) self.assertEqual(content.count, 3)
def test_get_base_params(self): y = yql.ThreeLegged('test-api-key', 'test-secret') result = y.get_base_params() self.assertEqual( set(['oauth_nonce', 'oauth_version', 'oauth_timestamp']), set(result.keys()))
def test_api_key_and_secret_attrs2(self): y = yql.ThreeLegged('test-api-key', 'test-secret') self.assertEqual(y.api_key, 'test-api-key') self.assertEqual(y.secret, 'test-secret')
def test_y_token_without_timestamp_raises(self): token = yql.YahooToken('test', 'test2') y = yql.ThreeLegged('test', 'test2') y.check_token(token)
# easier view. Unique <tags> are keys of a dictionary # Multiple <tags> with the same name indicate these elements are all in a list import yql from yql.storage import FileTokenStore import os import fantasystats import draftresults import gspread from sys import argv from oauth2client.service_account import ServiceAccountCredentials y3 = yql.ThreeLegged('insert client ID', 'insert client secret') _cache_dir = 'C:\\Users\\Luke\\PycharmProjects\\YahooAPI' if not os.access(_cache_dir, os.R_OK): os.mkdir(_cache_dir) token_store = FileTokenStore(_cache_dir, secret='insert secret here') stored_token = token_store.get('foo') if not stored_token:
def test_get_base_params(): from httplib2 import Http y = yql.ThreeLegged('test-api-key', 'test-secret') result = y.get_base_params() assert set(['oauth_nonce', 'oauth_version', 'oauth_timestamp']) \ == set(result.keys())
def test_api_key_and_secret_attrs2(): from httplib2 import Http y = yql.ThreeLegged('test-api-key', 'test-secret') assert y.api_key == 'test-api-key' assert y.secret == 'test-secret'
def __init__(self, user_input_league_id=None, user_input_chosen_week=None): # config vars self.config = ConfigParser() self.config.read("config.ini") if user_input_league_id: self.league_id = user_input_league_id else: self.league_id = self.config.get( "Fantasy_Football_Report_Settings", "chosen_league_id") # verification output message print( "Generating fantasy football report for league with id: %s (report generated: %s)\n" % (self.league_id, "{:%Y-%b-%d %H:%M:%S}".format( datetime.datetime.now()))) # yahoo oauth api (consumer) key and secret with open("./authentication/private.txt", "r") as auth_file: auth_data = auth_file.read().split("\n") consumer_key = auth_data[0] consumer_secret = auth_data[1] # yahoo oauth process self.y3 = yql.ThreeLegged(consumer_key, consumer_secret) _cache_dir = self.config.get("OAuth_Settings", "yql_cache_dir") if not os.access(_cache_dir, os.R_OK): os.mkdir(_cache_dir) token_store = FileTokenStore(_cache_dir, secret="sasfasdfdasfdaf") stored_token = token_store.get("foo") if not stored_token: request_token, auth_url = self.y3.get_token_and_auth_url() print("Visit url %s and get a verifier string" % auth_url) verifier = raw_input("Enter the code: ") self.token = self.y3.get_access_token(request_token, verifier) token_store.set("foo", self.token) else: print("Verifying token...\n") self.token = self.y3.check_token(stored_token) if self.token != stored_token: print("Setting stored token!\n") token_store.set("foo", self.token) ''' run base yql queries ''' # get fantasy football game info game_data = self.yql_query( "select * from fantasysports.games where game_key='nfl'") # unique league key composed of this year's yahoo fantasy football game id and the unique league id self.league_key = game_data[0].get("game_key") + ".l." + self.league_id print("League key: %s\n" % self.league_key) # get individual league roster roster_data = self.yql_query( "select * from fantasysports.leagues.settings where league_key='" + self.league_key + "'") roster_slots = collections.defaultdict(int) self.league_roster_active_slots = [] flex_positions = [] for position in roster_data[0].get("settings").get( "roster_positions").get("roster_position"): position_name = position.get("position") position_count = int(position.get("count")) count = position_count while count > 0: if position_name != "BN": self.league_roster_active_slots.append(position_name) count -= 1 if position_name == "W/R": flex_positions = ["WR", "RB"] if position_name == "W/R/T": flex_positions = ["WR", "RB", "TE"] if "/" in position_name: position_name = "FLEX" roster_slots[position_name] += position_count self.roster = {"slots": roster_slots, "flex_positions": flex_positions} # get data for all teams in league self.teams_data = self.yql_query( "select * from fantasysports.teams where league_key='" + self.league_key + "'") # get data for all league standings self.league_standings_data = self.yql_query( "select * from fantasysports.leagues.standings where league_key='" + self.league_key + "'") self.league_name = self.league_standings_data[0].get("name") # TODO: incorporate winnings into reports # entry_fee = league_standings_data[0].get("entry_fee") if user_input_chosen_week: chosen_week = user_input_chosen_week else: chosen_week = self.config.get("Fantasy_Football_Report_Settings", "chosen_week") try: if chosen_week == "default": self.chosen_week = str( int(self.league_standings_data[0].get("current_week")) - 1) # self.chosen_week = "1" elif 0 < int(chosen_week) < 18: if 0 < int(chosen_week) <= int( self.league_standings_data[0].get("current_week")) - 1: self.chosen_week = chosen_week else: incomplete_week = raw_input( "Are you sure you want to generate a report for an incomplete week? (y/n) -> " ) if incomplete_week == "y": self.chosen_week = chosen_week elif incomplete_week == "n": raise ValueError( "It is recommended that you not generate a report for an incomplete week." ) else: raise ValueError( "Please only select 'y' or 'n'. Try running the report generator again." ) else: raise ValueError( "You must select either 'default' or an integer from 1 to 17 for the chosen week." ) except ValueError: raise ValueError( "You must select either 'default' or an integer from 1 to 17 for the chosen week." )