get_match_info.py 2.73 KB
import requests
from urllib import parse
import pymysql
def select(user_name):
    conn = pymysql.connect(host='localhost', user='root', password="!!fnsldkwjsrl11", db="project_troll", charset='utf8')
    check=0
    try:
        with conn.cursor() as curs:
            sql = "select name from match_info"
            curs.execute(sql)
            rs = curs.fetchall()
            for row in rs:
                if user_name in row:
                    check=1
    finally:
        conn.close()
    return check
def insert_table(name,country,season):
    if select(name)==1:
        conn = pymysql.connect(host='localhost', user='root', password="!!fnsldkwjsrl11", db="project_troll", charset='utf8')
        try:
            with conn.cursor() as curs:
                sql = 'UPDATE match_info SET country=%s,season=%s WHERE name=%s'
                curs.execute(sql,(country,season,name))
            conn.commit()
        finally:
            conn.close()
    else:
        conn = pymysql.connect(host='localhost', user='root', password="!!fnsldkwjsrl11", db="project_troll", charset='utf8')
        try:
            with conn.cursor() as curs:
                sql = 'insert into match_info values(%s, %s,%s)'
                curs.execute(sql, (name, country,season))
            conn.commit()
        finally:
            conn.close()
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 game_id(name,num):
    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_DATA=getAPI.json()['matches']
    #int값 리턴
    return LOL_API_DATA[num].get("gameId")
def season_country(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_DATA=getAPI.json()['matches']
    #str값리턴
    #insert_table(name,LOL_API_DATA[0].get("platformId"),LOL_API_DATA[0].get("season"))
    return LOL_API_DATA[0].get("platformId"),LOL_API_DATA[0].get("season")