class DraftState(object): def __init__(self, league_key, credentials='credentials.json'): oauth = OAuth1(None, None, from_file=credentials) self.yql = MYQL(format='json', oauth=oauth) self.last_pick = 0 self.league_key = league_key self.teams = self._query(QUERY_TEAMS.format(self.league_key))['team'] self.teams = { t['team_key']: t['name'] for t in self.teams } print(json.dumps(self.teams, indent=2)) self.rosters = { name: [] for name in self.teams.values() } def _query(self, query): response = self.yql.raw_query(query) data = json.loads(response.content) return data['query']['results'] def refresh(self): # TODO exclude known picks from query picks = self._query(QUERY_DRAFT_RESULTS.format(self.league_key))['league']['draft_results']['draft_result'][self.last_pick:] player_keys = [] teams = [] for p in picks: if 'player_key' in p and p['player_key']: player_keys.append("'{}'".format(p['player_key'])) teams.append(self.teams[p['team_key']]) else: self.last_pick = int(p['pick']) - 1 break players = [] if player_keys: players = self._query(QUERY_PLAYER_KEYS.format(', '.join(player_keys)))['player'] if isinstance(players, dict): players = [players] players = [p['name']['full'] for p in players] return jsonify(picks=players, teams=teams)
class TestMYQL(unittest.TestCase): def setUp(self,): self.yql = MYQL(format='json',community=True) self.insert_result = None def tearDown(self): pass def test_desc(self,): response = self.yql.desc('weather.forecast') logging.debug(prettyfy(response, 'json')) self.assertEqual(response.status_code, 200) def test_show_tables(self,): yql = MYQL(format='xml', community=False) response = yql.show_tables(format='xml') logging.debug(prettyfy(response, 'xml')) self.assertEqual(response.status_code, 200) def test_use(self): self.yql.use('http://www.josuebrunel.org/users.xml',name='users') response = self.yql.raw_query('select * from users', format='xml') self.yql.yql_table_url = None logging.debug(pretty_xml(response.content)) self.assertEqual(response.status_code, 200) def test_raw_query(self,): response = self.yql.raw_query('select name, woeid from geo.states where place="Congo"') logging.debug(pretty_json(response.content)) self.assertEqual(response.status_code, 200) def test_get(self,): self.yql.format = 'xml' response = self.yql.get('geo.countries', ['name', 'woeid'], 1) self.yql.format = 'json' logging.debug(pretty_xml(response.content)) self.assertEqual(response.status_code, 200) def test_select(self,): response = self.yql.select('geo.countries', ['name', 'code', 'woeid']).where(['name', '=', 'Canada']) logging.debug(pretty_json(response.content)) self.assertEqual(response.status_code, 200) def test_select_in(self,): response = self.yql.select('yahoo.finance.quotes').where(['symbol','in',("YHOO","AAPL","GOOG")]) logging.debug(pretty_json(response.content)) self.assertEqual(response.status_code, 200) def test_select_in_2(self,): response = self.yql.select('weather.forecast',['units','atmosphere']).where(['woeid','IN',('select woeid from geo.places(1) where text="Paris"',)]) logging.debug(pretty_json(response.content)) self.assertEqual(response.status_code, 200) def test_raise_exception_select_where_in(self,): with self.assertRaises(TypeError): response = self.yql.select('weather.forecast',['units','atmosphere']).where(['woeid','IN',('select woeid from geo.places(1) where text="Paris"')]) def test_1_insert(self,): response = self.yql.insert('yql.storage.admin',('value',),('http://josuebrunel.org',)) try: logging.debug(pretty_json(response.content)) data = response.json()['query']['results']['inserted'] logging.debug(data) json_write_data(data,'yql_storage.json') except (Exception,) as e: logging.error(response.content) logging.error(e) self.assertEqual(response.status_code, 200) def test_2_check_insert(self,): json_data = json_get_data('yql_storage.json') response = self.yql.select('yql.storage').where(['name','=',json_data['select']]) logging.debug(pretty_json(response.content)) self.assertEqual(response.status_code, 200) def test_3_update(self,): json_data = json_get_data('yql_storage.json') response = self.yql.update('yql.storage',('value',),('https://josuebrunel.org',)).where(['name','=',json_data['update']]) logging.debug(pretty_json(response.content)) self.assertEqual(response.status_code, 200) def test_4_delete(self,): json_data = json_get_data('yql_storage.json') response = self.yql.delete('yql.storage').where(['name','=',json_data['update']]) logging.debug(pretty_json(response.content)) self.assertEqual(response.status_code, 200) def test_cross_product(self): yql = YQL(format='xml', crossProduct=True) response = yql.select('weather.forecast').where(['location', '=', '90210']) logging.debug("{0} {1}".format(response.status_code, response.reason)) self.assertEqual(response.status_code, 200) def test_variable_substitution(self,): yql = YQL() var = {'home': 'Congo'} yql.set(var) response = yql.select('geo.states', remote_filter=(5,)).where(['place', '=', '@home']) logging.debug(pretty_json(response.content)) self.assertEqual(response.status_code, 200) def test_raise_exception_no_table_selected(self): with self.assertRaises(NoTableSelectedError): response = self.yql.select(None).where([])
from myql import MYQL from stat_ids import stat_ids import csv import os # read in consumer secret and key # only consumer_key and consumer_secret are required. oauth = OAuth1(None, None, from_file='credentials.json') if not oauth.token_is_valid(): oauth.refresh_access_token() yql = MYQL(format='json', oauth=oauth) # get league key info from yahoo response = yql.raw_query('select * from fantasysports.leagues where use_login=1 and game_key in ("mlb")') r = response.json() # if in multiple leagues will need to adjust these lines to make sure you're grabbing the right data. league_key = r['query']['results']['league'][0]['league_key'] num_teams = int(r['query']['results']['league'][0]['num_teams']) print league_key teamid = range(1,(num_teams + 1)) rotostats = {} first = 0 for i in teamid: team_key = league_key + '.t.' + str(i) stats_query = "select * from fantasysports.teams.stats where team_key='" + team_key +"'" response = yql.raw_query(stats_query) response = response.json() rotostats['teamname'] = str(response['query']['results']['team']['name'])
class TestMYQL(unittest.TestCase): def setUp(self, ): self.yql = MYQL(format='json', community=True) self.insert_result = None def tearDown(self): pass def test_desc(self, ): response = self.yql.desc('weather.forecast') logging.debug(prettyfy(response, 'json')) self.assertEqual(response.status_code, 200) def test_show_tables(self, ): yql = MYQL(format='xml', community=False) response = yql.show_tables(format='xml') logging.debug(prettyfy(response, 'xml')) self.assertEqual(response.status_code, 200) def test_use(self): self.yql.use('http://www.josuebrunel.org/users.xml', name='users') response = self.yql.raw_query('select * from users', format='xml') self.yql.yql_table_url = None logging.debug(pretty_xml(response.content)) self.assertEqual(response.status_code, 200) def test_raw_query(self, ): response = self.yql.raw_query( 'select name, woeid from geo.states where place="Congo"') logging.debug(pretty_json(response.content)) self.assertEqual(response.status_code, 200) def test_get(self, ): self.yql.format = 'xml' response = self.yql.get('geo.countries', ['name', 'woeid'], 1) self.yql.format = 'json' logging.debug(pretty_xml(response.content)) self.assertEqual(response.status_code, 200) def test_select(self, ): response = self.yql.select('geo.countries', ['name', 'code', 'woeid']).where( ['name', '=', 'Canada']) logging.debug(pretty_json(response.content)) self.assertEqual(response.status_code, 200) def test_select_in(self, ): response = self.yql.select('yahoo.finance.quotes').where( ['symbol', 'in', ("YHOO", "AAPL", "GOOG")]) logging.debug(pretty_json(response.content)) self.assertEqual(response.status_code, 200) def test_select_in_2(self, ): response = self.yql.select( 'weather.forecast', ['units', 'atmosphere']).where([ 'woeid', 'IN', ('select woeid from geo.places(1) where text="Paris"', ) ]) logging.debug(pretty_json(response.content)) self.assertEqual(response.status_code, 200) def test_raise_exception_select_where_in(self, ): with self.assertRaises(TypeError): response = self.yql.select( 'weather.forecast', ['units', 'atmosphere']).where([ 'woeid', 'IN', ('select woeid from geo.places(1) where text="Paris"') ]) def test_1_insert(self, ): response = self.yql.insert('yql.storage.admin', ('value', ), ('http://josuebrunel.org', )) try: logging.debug(pretty_json(response.content)) data = response.json()['query']['results']['inserted'] logging.debug(data) json_write_data(data, 'yql_storage.json') except (Exception, ) as e: logging.error(response.content) logging.error(e) self.assertEqual(response.status_code, 200) def test_2_check_insert(self, ): json_data = json_get_data('yql_storage.json') response = self.yql.select('yql.storage').where( ['name', '=', json_data['select']]) logging.debug(pretty_json(response.content)) self.assertEqual(response.status_code, 200) def test_3_update(self, ): json_data = json_get_data('yql_storage.json') response = self.yql.update('yql.storage', ('value', ), ('https://josuebrunel.org', )).where( ['name', '=', json_data['update']]) logging.debug(pretty_json(response.content)) self.assertEqual(response.status_code, 200) def test_4_delete(self, ): json_data = json_get_data('yql_storage.json') response = self.yql.delete('yql.storage').where( ['name', '=', json_data['update']]) logging.debug(pretty_json(response.content)) self.assertEqual(response.status_code, 200) def test_cross_product(self): yql = YQL(format='xml', crossProduct=True) response = yql.select('weather.forecast').where( ['location', '=', '90210']) logging.debug("{0} {1}".format(response.status_code, response.reason)) self.assertEqual(response.status_code, 200) def test_variable_substitution(self, ): yql = YQL() var = {'home': 'Congo'} yql.set(var) response = yql.select('geo.states', remote_filter=(5, )).where( ['place', '=', '@home']) logging.debug(pretty_json(response.content)) self.assertEqual(response.status_code, 200) def test_raise_exception_no_table_selected(self): with self.assertRaises(NoTableSelectedError): response = self.yql.select(None).where([])