10

I have a question about rate limits. I take a data from the CSV and enter it into the query and the output is stored in a list. I get an error because I make too many requests at once. (I can only make 20 requests per second). How can I determine the rate limit?

import requests
import pandas as pd 

df = pd.read_csv("Data_1000.csv")
list = []



def requestSummonerData(summonerName, APIKey):

    URL = "https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by-name/" + summonerName + "?api_key=" + APIKey
    response = requests.get(URL)
    return response.json()

def main():
    APIKey = (str)(input('Copy and paste your API Key here: '))

    for index, row in df.iterrows():
        summonerName = row['Player_Name']
        responseJSON  = requestSummonerData(summonerName, APIKey)
        ID = responseJSON ['accountId']
        ID = int(ID)
        list.insert(index,ID)

    df["accountId"]= list
Latika Agarwal
  • 973
  • 1
  • 6
  • 11
Sadiksk
  • 135
  • 1
  • 1
  • 8
  • Possible duplicate of [What's a good rate limiting algorithm?](https://stackoverflow.com/questions/667508/whats-a-good-rate-limiting-algorithm) – Klaus D. May 26 '18 at 11:28

1 Answers1

14

If you already know you can only make 20 requests per second, you just need to work out how long to wait between each request:

Divide 1 second by 20, which should give you 0.05. So you just need to sleep for 0.05 of a second between each request and you shouldn't hit the limit (maybe increase it a bit if you want to be safe).

import time at the top of your file and then time.sleep(0.05) inside of your for loop (you could also just do time.sleep(1/20))

loginn
  • 94
  • 1
  • 8
OdinX
  • 4,135
  • 1
  • 24
  • 33