get_game_info.py 2.87 KB
import requests
from urllib import parse
import pymysql
import sys, json


APIKEY="RGAPI-0f60cc69-78c3-414b-af06-a61c9a685d9c"
headers={
     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36",
    "Accept-Language": "ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7",
    "Accept-Charset": "application/x-www-form-urlencoded; charset=UTF-8",
    "Origin": "https://developer.riotgames.com",
    "X-Riot-Token": APIKEY
     }
def insert_table(name, kill,death,ass,cs,view):
    conn = pymysql.connect(host='localhost', user='root', password="!!fnsldkwjsrl11", db="project_troll", charset='utf8')
    try:
        with conn.cursor() as curs:
            sql = 'insert into game_info values(%s, %s,%s,%s,%s,%s)'
            curs.execute(sql, (name, kill,death,ass,cs,view))
        conn.commit()
    finally:
        conn.close()






def all_info(name):
    API="https://kr.api.riotgames.com/lol/summoner/v4/summoners/by-name/" + name
    getAPI=requests.get(API, headers=headers)
    LOL_API_DATA=getAPI.json()
    accountid=LOL_API_DATA["accountId"]
    API="https://kr.api.riotgames.com/lol/match/v4/matchlists/by-account/" + accountid
    getAPI=requests.get(API, headers=headers)
    LOL_API_DATA1=getAPI.json()['matches']
    kill=0
    death=0
    ass=0
    vision=0
    cs=0
    time=0

    for i in range(0,10):
        checkpoint=0
        gameid=LOL_API_DATA1[i].get("gameId")
        API="https://kr.api.riotgames.com/lol/match/v4/matches/" + str(gameid)
        getAPI=requests.get(API, headers=headers)
        LOL_API_DATA=getAPI.json()
        for j in range(0,10):
            if(LOL_API_DATA["participantIdentities"][j]["player"]["summonerName"]==name):
                checkpoint=j
                j=10
        kill=kill+LOL_API_DATA["participants"][checkpoint]["stats"]["kills"]
        death=death+LOL_API_DATA["participants"][checkpoint]["stats"]["deaths"]
        ass=ass+LOL_API_DATA["participants"][checkpoint]["stats"]["assists"]
        cs=cs+LOL_API_DATA["participants"][checkpoint]["stats"]["totalMinionsKilled"]+\
            LOL_API_DATA["participants"][checkpoint]["stats"]["neutralMinionsKilled"]
        vision=vision+LOL_API_DATA["participants"][checkpoint]["stats"]["visionScore"]
        time=time+LOL_API_DATA["gameDuration"]

    insert_table(name, kill,death,ass,cs,vision)
    return kill,death,ass,cs,vision,time

#Read data from stdin
def read_in():
       lines = sys.stdin.readlines()
       # Since our input would only be having one line, parse our JSON data from that
       return json.loads(lines[0])

def main():
       #get our data as an array from read_in()
       lines = read_in()

       # Sum  of all the items in the providen array
       for item in lines:
           name=item
       #return the sum to the output stream
       print(all_info(name))

# Start process
if __name__ == '__main__':
       main()