[Python] Why does the Yahoo-API Start Parameter have no impact on results returned?

Discussão em 'Python' iniciado por Stack, Janeiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    My main goal is to pull all players from Yahoo-API for fantasy football. While Yahoo limits returned data to 25 players, another thread suggest changing the "Start" parameter and looping through the data might allow for whole data set retrieval.

    With that said, changing the start parameter is not yielding any change in results. In code terms,

    Start=0 and Start =25 and Start=2500 all return the SAME players in the URI below.

    url = f"{self.base_url}/league/{self.league_id}/players;status=A;start={start},count=25"

    Full Script

    import os
    import requests
    from requests_oauthlib import OAuth2Session
    import webbrowser
    import xml.etree.ElementTree as ET
    import csv
    import json
    # from yahoo_oauth import OAuth2

    class YahooFantasyAPI:

    def __init__(self):
    self.client_id = os.getenv('API_KEY')
    self.client_secret = os.getenv('API_SECRET')
    self.redirect_uri = 'https://localhost/'
    self.league_id = os.getenv('league_id')
    self.base_url = 'https://fantasysports.yahooapis.com/fantasy/v2'
    self.token = None
    self.oauth = OAuth2Session(self.client_id, redirect_uri=self.redirect_uri, scope='openid')
    # self.oauth2= OAuth2(self.client_id,self.client_secret,base_url=self.base_url)
    self.state = None
    self.item_dict=item_dict

    def authenticate(self):
    # Step 1: Get authorization URL and state
    authorization_url, self.state = self.oauth.authorization_url(
    'https://api.login.yahoo.com/oauth2/request_auth'
    )
    print(f"Please go to this URL and authorize access: {authorization_url}")
    webbrowser.open(authorization_url)

    # Step 2: User pastes the redirected URL with the code
    redirect_response = input("Paste the full redirect URL here: ")
    print(f"Redirect URL: {redirect_response}")

    # Step 3: Verify that the state parameter from the response matches the one in the request
    if self.state not in redirect_response:
    raise ValueError(f"State in the response does not match the original state. Response state: {redirect_response}")

    print(f"State during token request: {self.state}")

    # Step 4: Fetch the token, ensuring that the state is passed correctly
    self.token = self.oauth.fetch_token(
    'https://api.login.yahoo.com/oauth2/get_token',
    authorization_response=redirect_response,
    client_secret=self.client_secret,
    state=self.state # Explicitly pass the state value
    )
    print("Authentication successful!")

    def get_players(self):
    global item_dict
    if not self.token:
    raise ValueError("Authenticate first by calling the authenticate() method.")

    done = False
    start = 1
    while(not done) :
    # Define the endpoint URL to get league players
    url = f"{self.base_url}/league/{self.league_id}/players;status=A;start={start},count=25"

    print(url)

    # if not self.oauth.token_is_valid():
    # self.auth.refresh_access_token

    headers = {
    'Authorization': f"Bearer {self.token['access_token']}",
    'Accept': 'application/xml' # Expect XML response
    }

    response = self.oauth.get(url, params={'format': 'json'})

    # Print the status code and response content to debug
    print(f"Response Status Code: {response.status_code}")
    print(f"Content-Type: {response.headers.get('Content-Type')}")
    # print(response.content)

    with open('all.json', 'a') as csvfile:
    csvfile.write(response.text)
    csvfile.close()

    item_dict = json.loads(response.text)

    numPlayersInResp=len(item_dict['fantasy_content']['league'][1]['players'])
    # print(item_dict)
    print(numPlayersInResp)

    start += 25

    if numPlayersInResp < 25:
    done = True

    # Usage
    if __name__ == '__main__':

    yahoo_api = YahooFantasyAPI()
    yahoo_api.authenticate()
    yahoo_api.get_players()

    Continue reading...

Compartilhe esta Página