APIから結果を取得するPythonプログラムの処理速度を上げたい
解決したいこと
初めてちゃんとしたコードを書いたのですが、実行に5分もかかることがあり、書き方次第でもっと早くできるところがあるんじゃないかと思い質問させていただきました。
プログラムの概要
クラッシュロワイヤルというゲームのAPIを取得し、必要な情報を取り入れ、pandasのデータフレームを作成するという流れです。
実際のコード
#クラロワAPIからプロ選手の情報を取得するプログラム
import time
import json
import requests
import pandas as pd
import numpy as np
access_key = "ここでは省略"
URL = 'https://api.clashroyale.com/v1'
#選手名とパスを結合する辞書を作成
dic={"みかん坊や":"%232VYJYJ09","天GOD":"%232G0QUGLU","kota":"%23889VQ8JP","RAD":"%238QRCJQ9Y","ライキジョーンズ":"%2398Q8LPQ9",
"Jack":"%23YRVL9U98","きたっしゃん":"%23P8RLYOV9","だに":"%238LJVVGJP","けんつめし":"%23PQRR0CG9",
"Rorapolon":"%239JPRJ9R","焼き鳥":"%232Y8GL0V2","ユイヒイロ":"%23R2GRQPCJ","Blossom":"%238Q20LRC8Y","kk19212":"%23RU2CC2LG",
"れいや":"%232LRVG0C8","HANE×HANE":"%238Y088VU8U","Lewis":"%238Q020U0U","ピラメキ":"%232YGQGY92V","天ぷら":"%238Q2V2CGR","Scott":"%232Q98GVP9V"}
# 選手名を含むリストを作成
list= ["みかん坊や","天GOD","kota","RAD","ライキジョーンズ",
"Jack","きたっしゃん","だに","けんつめし",
"Rorapolon","焼き鳥","ユイヒイロ","Blossom","kk19212",
"れいや","HANE×HANE","Lewis","ピラメキ","天ぷら","Scott"]
# 選手の基本情報を取得
def general_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__'
# 選手の対戦情報を取得
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__'
# 自分のデッキリストを作成(変数は選手名と何番目のデッキか))
def selfdeck_list(name,newnum):
#decktype = battle_info(name)[0]["type"]
#cardsname = battle_info(name)[0]["team"][0]["cards"][1]["name"]
# 普通の書き方バージョン
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)
"""内包表記バージョン?
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"] 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 ("elapsed_time:{0}".format(elapsed_time) + "[sec]")
selfdeck_list("Scott",0)
# 対戦相手のデッキリストを作成(変数は選手名と何番目のデッキか)
def opponentdeck_list(name, newnum):
#decktype = battle_info(name)[0]["type"]
#cardsname = battle_info(name)[0]["team"][0]["cards"][1]["name"]
# 1つ目の[]は何試合目か、3つ目は8個の中の何番目
""" 普通バージョン
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)"""
#内包表記バージョン?
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. デッキのdateframe作成
"""
columns1 = ["対戦種類","自デッキ","敵デッキ","勝敗"]
for number in range(0,25):
player = "Scott"
datas = selfdeck_list(player,number),opponentdeck_list(player,number)
deckdata = pd.DateFrame(data=datas,index=number,columns=columns1 )
print(deckdata)
"""
#3. datesetの中に入れ、DateFrameを作成
"""
columns2 = ["クラン","タグ","現在トロ","最多トロ","チャレンジ名","デッキ"]
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)
"""