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

Adding PUT/POST capability #103

Closed peacing closed 9 years ago

peacing commented 9 years ago

Hello Josue,

So, I'm attempting to use the API for more than just GETs, I'm trying to use PUT and POSTs to add functionality (like benching or adding a player on my team) to my fantasy sports specific version of your code. According to the Fantasy Sports documentation (https://developer.yahoo.com/fantasysports/guide/) I should be able to do this... and for example this code (https://github.com/jcreamer898/fantasy-sports) in node.js is apparently capable of doing it.

My attempt so far has been a function that looks like this:

def put(self):
    ''' This method uses PUT to modify a resource
    '''
    url = 'http://fantasysports.yahooapis.com/fantasy/v2/team/346.l.1328.t.12/roster'
    date = '2015-06-01'
    hit1key = '346.p.9340' # Jake Marisnik
    pos1 = 'OF'
    hit2key = '346.p.9719' # David Peralta
    pos2 = 'BN"

    xmlString = ('<?xml version="1.0"?>'
'<fantasy_content>'
  '<roster>'
    '<coverage_type>date</coverage_type>'
    '<date>%s</date>'
    '<players>'
      '<player>'
        '<player_key>%s</player_key>'
        '<position>%s</position>'
      '</player>'
      '<player>'
        '<player_key>%s</player_key>'
        '<position>%s</position>'
      '</player>'
    '</players>'
  '</roster>'
'</fantasy_content>\n') % (date, hit1key, pos1, hit2key, pos2)

if vars(self).get('oauth'): 
        self.url = self.private_url
        if not self.oauth.token_is_valid(): # Refresh token if token has expired
            self.oauth.refresh_token()
 response = self.oauth.session.put(url, xmlString)
 return None

I know this isn't directly related to what you're working on and it will also be difficult for you to test because you need to have a team to make changes... but if you see any obvious error or have any input I appreciate it.

Thanks! Paul

peacing commented 9 years ago

Sorry that XML code didn't come out, here's a screenshot:

screen shot 2015-05-31 at 2 41 53 pm

josuebrunel commented 9 years ago

Hello @unpairestgood (Paul). Yeah i see what you want to do. It would have been great to have your project open sourced. Anyway

...
response = self.oauth.session.put(url, xmlString[0])

Anyway, kind of late here in. Will try to play with the yahoo fantasy sport API tomorrow if i can. By the way it would be great if i could have a test case.

Have good one buddy

peacing commented 9 years ago

I'm still figuring out how to use github effectively. I uploaded my code in a separate repository (sorry for copying! if that's bad etiquette let me know) https://github.com/unpairestgood/ycontrol-myql-master

Most of my code is in ycontrol.py and then there are a few additions to myql.py. Specifically for this goal in ycontrol I'm calling the function set_hitters which then calls my PUT function. I tried your first suggestions and I couldn't get it to work still. My error is 401 which is supposed to mean the authorization isn't working correctly but I suspect it has to do with how XML code formatting.

screen shot 2015-05-31 at 7 56 54 pm

That's what my error message looks like. Let me know what I can do to help you have a test case... I'm willing to give you my yahoo username and pass so you can test on my team.

Thanks again

josuebrunel commented 9 years ago

Hi @unpairestgood !

I uploaded my code in a separate repository (sorry for copying! if that's bad etiquette let me know)

The code is under the M.I.T License, so you can almost do whatever you want with it :wink:

Advice:

If you're working on a Linux or OSX, i think you must have a tools like _virtualenvwrapper and **pip**_. So you could do something like

$ mkvirtualenv ycontrol
$(ycontrol) pip install myql

And you won't need to have a local copy of the libraries you're using :+1:

Most of my code is in ycontrol.py and then there are a few additions to myql.py. Specifically for this goal in ycontrol I'm calling the function set_hitters which then calls my PUT function. I tried your first suggestions and I couldn't get it to work still. My error is 401 which is supposed to mean the authorization isn't working correctly but I suspect it has to do with how XML code formatting.

Thanks for the tips. I went through that a lil bit. I'm trying to reproduce the issue. As soon as i get something, i will let you know.

If you don't mind, i would like to reorganize your project ( less files ). I will externalize the _Yahoo OAuth_ module in a new project, so people will be able to use it as a stand alone library.

Don't hesitate to ask whatsoever questions you may have. One of the greatest goal of open source communities is to learn from/help each other.

Hope to come back to you very soon

peacing commented 9 years ago

Josue,

I'm going to work on finding a solution this weekend... either in my script in Python or I'll try running the repository I linked to above in Node to get something to work. I'm curious if you've looked into this problem more, if you've come close or maybe think it's too difficult to figure out. Let me know!

josuebrunel commented 9 years ago

@unpairestgood , yes i did :wink: I created a OAuth library yahoo-oauth. I just ran tests and they all passed https://travis-ci.org/josuebrunel/yahoo-oauth/builds/65652001

This means that, you won't be using mYQL anymore, since all you need is OAuth credentials. You will have to do stuff like

from yahoo_oauth import OAuth2

oauth = OAuth2(None, None, from_file='creds.json')

# run you requests to the protected api

response = oauth.session.get(uri, params={})
...
response = oauth.session.put(url,data={})
...
response = oauth.session.post(url, data={})

It works just like requests.

By the way, i'm on vacation, i might work on your project if you tell me what you really want to do

Hope it has helped. It's like 3 am here, i need to turn in :sleepy:

Have a good one buddy

josuebrunel commented 9 years ago

Btw, you will need to use REST Api of Fantasy Sport. Here is the Documentation. To better help you, i need to know what you want to do

Thanks

josuebrunel commented 9 years ago

Hello @unpairestgood , Do you use python3 or python2 ? I might be working on your project today. I've read the code and i've planned the actions below :

  1. create a new repository (with you as member)
  2. Remove useless files ( most of them are from mYQL )
  3. Restructure the your project
  4. Rewrite one or two things ( will do my best to as faithful as i can to what you did )
  5. Transfert you the project

Thanks

peacing commented 9 years ago

Josue, thanks for the help, try to enjoy your vacation too! So to answer your question, I am using python 2. I'm going to take a look at your code now.

josuebrunel commented 9 years ago

Thanks. Don't worry, even right now i'm coding :wink:

josuebrunel commented 9 years ago

@unpairestgood all my changes are in this pull request. You can reach me on skype if you want : _thehokage

peacing commented 9 years ago

@josuebrunel I implemented those changes... and they worked!! Thank you so much, really! I'm still working in my local copy, but now that the PUT works, I'll spend some time learning how to use the virtual environment and merging/pulling all changes.

Best, Paul

P.S. for the record, my response looked like this:

screen shot 2015-06-06 at 12 46 42 pm

While it was successful, I'm not sure what the 218 code means.

josuebrunel commented 9 years ago

@unpairestgood so happy for you :wink: . I don't know what the 218 means. But i really happy to see the 200 though lol. Keep it up. As always don't hesitate to bother me if you need anything