Want to speed up Python programs that retrieve results from APIs

Asked 2 months ago, Updated 2 months ago, 4 views

What do you want to solve

This is my first time to write a proper code, but sometimes it takes 5 minutes to execute, and I thought there might be a place where I can do it faster depending on how I write it.

Program Overview

The process is to get the API for a game called Crash Royale, take the necessary information, and create a data frame for pandas.

Actual Code

#Program to retrieve professional player information from the Clarova API
import time
import json
import requests
import pandas aspd
import numpy as np

access_key="Omitted here"

URL='https://api.clashroyale.com/v1'

# Create a dictionary that combines player names and paths
dic={"Tangerine Boy": "%232VYJYJ09", "Ten God": "%232G0QUGLU", "kota": "%23889VQ8JP", "RAD": "%238QRCJQ9Y", "Like Jones": "%2398Q8LPQ9",
    "Jack": "%23YRVL9U98", "Kitashin": "%23P8RLYOV9", "Dani": "%238LJVVGJP", "Kenzushi": "%23PQR0CG9",
    "Rorapolon": "%239JPRJ9R", "Yakitori": "%232Y8GL0V2", "Yuihiro": "%23R2GRQPCJ", "Blossom": "%238Q20LRC8Y", "kkk19212": "%23RU2CC2LG",
    "Reya": "%232LRVG0C8", "HANE×HANE": "%238Y088VU8U", "Lewis": "%238Q020U0U", "Pirameki": "%232YGGGY92V", "Tempura": "%238Q2V2CGR", "Scott": "%232Q98GVP9V"}

# Create a list containing player names
list=["Tangerine Boy", "Ten God", "kota", "RAD", "Like Jones",
    "Jack", "Kitashin", "Dani", "Kenzushi",
    "Rorapolon", "Yakitori", "Yuihiro", "Blossom", "kk19212",
    "Reiya", "HANE×HANE", "Lewis", "Pirameki", "Tempura", "Scott"]

# Get basic player information
default_info(name):
    target_api=URL+"/players/"
    playerTag=dic [name]
    url=target_api+playerTag
    headers = {
        "content-type": "application/json; charset=utf-8",
        "cache-control": "max-age=60",
        "authorization": "Bearer%s" %access_key}
    r=requests.get(url,headers=headers)
    data=r.json()
    return data

__name__=='__general_info_'


# Get player match information
def battle_info(name):
    target_api=URL+"/players/"
    playerTag=dic [name]
    url=target_api+playerTag+"/battlelog"
    headers = {
        "content-type": "application/json; charset=utf-8",
        "cache-control": "max-age=60",
        "authorization": "Bearer%s" %access_key}
    r=requests.get(url,headers=headers)
    data=r.json()
    return data

__name__=='_battle_info_'


# Create your own deck list (variables are player name and what deck)
def selfdecck_list(name, newnum):
    # decktype=battle_info(name)[0]["type"]
    # cardsname=battle_info(name)[0]["team"][0]["cards"][1]["name"]

    # Normal Writing Version
    decklist=[ ]

    for decknum in range (0,25):
        decktype=battle_info(name) [decknum] ["type" ]

        for numindeck in range (0,8):
            cardsname=battle_info(name)[decknum]["team"][0]["cards"][numindeck]["name" ]
            decklist.append(cardsname)

    """Included version?"""
    decklist=[battle_info(name)[decknum]["team"][0]["cards"][numindeck]["name"]for decknum in range(0,25)for numindeck in range(0,8)]
    decktype=[battle_info(name)[decknum]["type" ] US>"for decknum in range(0,25)]"

    return decktype [int(newnum)]
    return decklist [int(newnum*8): int(newnum*8+8)]
    elapsed_time=time.time()-start
    print("elapped_time: {0}".format(elapped_time)+"[sec]")

selfdecck_list("Scott", 0)


# Create a deck list of opponents (variables are player name and what deck)
defopponentdeck_list(name,newnum):
    # decktype=battle_info(name)[0]["type"]
    # cardsname=battle_info(name)[0]["team"][0]["cards"][1]["name"]
    # How many games is the first [ ] and the third out of eight?

    """ normal version
    decklist=[ ]

    for decknum in range (0,25):
        decktype=battle_info(name) [decknum] ["type" ]

        for numindeck in range (0,8):
            cardsname=battle_info(name)[decknum]["opponent"][0]["cards"][numindeck]["name" ]
            decklist.append(cardsname)"

    #Included version?

    decklist=[battle_info(name)[decknum]["opponent"][0]["cards"][numindeck]["name"]for decknum in range(0,25)for numindeck in range(0,8)]
    decktype=[battle_info(name)[decknum]["type" ] for decknum in range (0,25)]

    return [ decktype [int(newnum)], decklist [int(newnum*8): int(newnum*8+8)]]



#2. Create a dateframe for the deck
"""
columns1 = ["Type of Battle", "Self Deck", "Enemy Deck", "Winning or losing"]

for number in range (0,25):
    player="Scott"
    datas=selfdeck_list(player, number), opponentdeck_list(player, number)

deckdata=pd.DateFrame(data=data,index=number,columns=columns1)

print(deckdata)
"""


#3. Create DateFrame in dateset
"""
columns2=["Clan", "Tag", "Current Toro", "Most Toro", "Challenge Name", "Deck"]

for player in list:
    dataset=general_info(player)["tag"], dic[player], general_info(player)["trophies"], general_info(player)["bestTrophies"], battle_info(player, 0)

generaldata=pd.DateFrame(data=dateset, index=list, columns=columns2)
print(generaldata)
"""

python python-requests

2022-09-30 14:15

1 Answers

As far as the code is concerned, 258=200 API tapping process (battle_info(name)) runs, while there seems to be no other heavy calculation, so there seems to be a bottleneck in the API execution area (even if 100ms per request, it will take 100ms200=20s).

マルチIt seems that the same answer has been solved in the multi-post destination.

I edited this post based on @PicoSushi's Comment and posted it as Community Wiki.I edited this post based on @PicoSushi's Comment and posted it as Community Wiki.


2022-09-30 14:15

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.