josuebrunel / myql

mYQL is a Python wrapper of the Yahoo! Query Language a.k.a YQL (Finances, ...)
https://myql.readthedocs.org/en/latest/
MIT License
31 stars 8 forks source link

Very Helpful! #65

Closed peacing closed 9 years ago

peacing commented 9 years ago

Thank you @josuebrunel ! I'm not an expert on Python/APIs so your code should be very helpful with my fantasy baseball project. I'm going to spend time trying to understand how to get this to work, but if I run into issues any help would be greatly appreciated.

Cordialement, Paul

josuebrunel commented 9 years ago

OK @unpairestgood , will do my best to help :wink: :

peacing commented 9 years ago

Just want to add I was able to use your code to access the API and it's very clean/easy to understand!

peacing commented 9 years ago

@josuebrunel I have been able to get the working for one API request (with oauth), but if I execute a code with two requests, I get an error on the second one. Specifically I'm using the fantasy sports API and want to pull data for each team. My code looks like this:

response = yql.select('fantasysports.teams.roster').where(['team_key', '=', 'mlb.l.1328.t.1'], ['date', '=', '2015-05-05'])

Which works fine, but if I run a python script with two requests like this:

response = yql.select('fantasysports.teams.roster').where(['team_key', '=', 'mlb.l.1328.t.1'], ['date', '=', '2015-05-05'])
response2 = yql.select('fantasysports.teams.roster').where(['team_key', '=', 'mlb.l.1328.t.2'], ['date', '=', '2015-05-05'])

I get an error on the response2 variable. Any idea why this happens or how to make it work? The error I get in terminal looks like this:

[2015-05-06 00:15:23,908 DEBUG] [root.yoauth.token_is_valid] TOKEN IS STILL VALID 

Traceback (most recent call last):
  File "ycontrol.py", line 26, in <module>
    response2 = response = yql.select('fantasysports.teams.roster').where(['team_key', '=', 'mlb.l.1328.t.2'], ['date', '=', '2015-05-05'])
  File "/Users/paulsingman/pyth/myql-master/myql/myql.py", line 219, in where
    response = self.executeQuery(payload)
  File "/Users/paulsingman/pyth/myql-master/myql/myql.py", line 89, in executeQuery
    response = self.oauth.session.get(self.url, params= payload)
  File "/Library/Python/2.7/site-packages/requests/sessions.py", line 477, in get
    return self.request('GET', url, **kwargs)
  File "/Library/Python/2.7/site-packages/rauth/session.py", line 210, in request
    return super(OAuth1Session, self).request(method, url, **req_kwargs)
  File "/Library/Python/2.7/site-packages/requests/sessions.py", line 465, in request
    resp = self.send(prep, **send_kwargs)
  File "/Library/Python/2.7/site-packages/requests/sessions.py", line 573, in send
    r = adapter.send(request, **kwargs)
  File "/Library/Python/2.7/site-packages/requests/adapters.py", line 415, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', ResponseNotReady())

Thank you.

josuebrunel commented 9 years ago

@unpairestgood ok thank you. It's the first release though. I'm checking the issue

josuebrunel commented 9 years ago

@unpairestgood try this fix for now.

oauth = YOAuth(None, None, from_file='credentials.json') 
yql = MYQL(format='json', oauth=oauth)
#response = yql.select('fantasysports.teams.roster').where(['team_key', '=', 'mlb.l.1328.t.1'], ['date', '=', '2015-05-05'])
response = yql.rawQuery("SELECT * FROM fantasysports.teams.roster WHERE team_key IN ('mlb.l.1328.t.1','mlb.l.1328.t.2') AND date = '2015-05-05'")
#time.sleep(10)
 #response2 = yql.rawQuery("SELECT * FROM fantasysports.teams.roster WHERE team_key = 'mlb.l.1328.t.2' AND date = '2015-05-05'")
  #response2 = yql.select('fantasysports.teams.roster').where(['team_key', '=', 'mlb.l.1328.t.2'], ['date', '=', '2015-05-05'])
data = response.json()
for team_data in data['query']['results']['team']: # data['query']['results']['team'] is a lit of result
    print team['team_id'], team['name'], team['manager']

Output should be like

(u'1', u'Y! - Funston', {u'manager': {u'is_commissioner': u'1', u'guid': u'3H7IQ3F2742K2ODHSJK5YXL23E', u'nickname': u'--hidden--', u'manager_id': u'1'}})
(u'2', u'Y! - Pianowski', {u'manager': {u'guid': u'WMKEJTV3VUJA4VZWQ25O27W43M', u'nickname': u'--hidden--', u'manager_id': u'2'}})

Let me know if you need anything else.

I think @almartin82 is the one who has more experience with Yahoo Fantasy Sport, if ain't mistaken, i think they have a straightforward API .

I will keep investigating on the issue. When the second query is run, the connection is closed

requests.exceptions.ConnectionError: ('Connection aborted.', ResponseNotReady())

I need to know why.

Have a good one guys !!

josuebrunel commented 9 years ago

@unpairestgood is this what you're trying to do ? https://github.com/josuebrunel/myql/blob/master/tests/tests.py#L35-49 This works for me

peacing commented 9 years ago

Josue, I haven't gotten your fixes to work yet, but I did try your solutions I'm sure they'll help me get it to work. Just for your knowledge, when I tried your code above using the rawQuery function, I got the following error:

Traceback (most recent call last):
  File "ycontrol.py", line 51, in <module>
    data = response.json()
AttributeError: 'dict' object has no attribute 'json'

And then when I tried to put the test_yahoo_fantasy_sport function in the myql.py file, I got a different error:

Traceback (most recent call last):
  File "ycontrol.py", line 43, in <module>
    x = test.test_yahoo_fantasy_sport()
  File "/Users/paulsingman/pyth/myql-master/myql/myql.py", line 260, in test_yahoo_fantasy_sport
    current_team = data['query']['results']['team']
TypeError: 'NoneType' object has no attribute '__getitem__'

Next chance I get I'm going to try different things to get this to work, but as always if you think you understand what the problem is, that's appreciated.

THe @almartin82 code looks very helpful as well. Thank you!

josuebrunel commented 9 years ago

Thanks @unpairestgood . Just trying my best to help.

Traceback (most recent call last):
  File "ycontrol.py", line 51, in <module>
    data = response.json()
AttributeError: 'dict' object has no attribute 'json

It seems like the response object is already a dict. Normally, a rawQuery() returns a response object with json() method when a json response is returned from the server.

So the error below is quite logical because of the previous one

Traceback (most recent call last):
  File "ycontrol.py", line 43, in <module>
    x = test.test_yahoo_fantasy_sport()
  File "/Users/paulsingman/pyth/myql-master/myql/myql.py", line 260, in test_yahoo_fantasy_sport
    current_team = data['query']['results']['team']
TypeError: 'NoneType' object has no attribute '__getitem__'

Can i have code snippet so i can help you better ?

Thanks

btw nice article http://razzball.com/fantasy-baseball-bot-basic-coding-introduction/

josuebrunel commented 9 years ago

I've been able to reproduce the real issue. Everything is explained here. Thanks for reporting that @unpairestgood

josuebrunel commented 9 years ago

hello @unpairestgood , i have your fix. The version of requests in the package needs tbe updated. In your virtualenv

$ pip install --upgrade requests rauth

then try your code

I will release a new version by the end of the day.

josuebrunel commented 9 years ago

@unpairestgood thanks one more time for your report. The bug is fixed. Run the command below to apply the fix:

$ pip install --upgrade myql
peacing commented 9 years ago

I tested this out quickly, and it seems to work! I'm starting to write my program to pull all the information, I'll let you know how it goes. Thanks for the help.

josuebrunel commented 9 years ago

You welcome buddy