본문 바로가기

Coding/Riot API를 사용하여 lolchess.gg 클론 코딩

라이엇 api를 사용하여 TFT(롤토체스) 데이터 수집 -5

1.한국 챌린져 큐의 최근  게임 가져오기

 

1-5 ) match history 가져오기

 

 

옆에 match hx 기록 사진을 가져와봤습니다. 대충 데이터 모양을 보면 

1경기에 8명의 덱 기록이 나오는데

옆 사진은 그중 2명 데이터를 가져 오다가 잘린 사진입니다.

 

겨우 matchid 경기 하나의 데이터도 저렇게 길기 때문에

해당 데이터는 상당히 거대합니다.

 

그러다 보니 오류도 많이 발생하였고

 

그러한 오류를 최대한 해결하기 위해 코드를 작성하였었지만,,

조금 난잡할 수 있으며, 코드 역시 분할하여 올리도록 하겠습니다.

 

 

1-5-1) matchids의 저장 후 get_match_history() 실행

 

 

matchid를 csv로 새로 저장한 후에

get_match_history를 시행시키는 코드입니다.

 

이 경우에도 해당 파일이 존재할 경우에 따라 if 문이 작성되어있으며,

존재하는 경우에는 new_matchids를 새로 저장을 시켜주며,

matchid에서도 새롭게 저장이 필요한 경우 (중간에 코드가 중단되어버리는 경우)

등이 발생하는 경우가 있어서 다시 한 번 set로 분류해 주었습니다. 

if os.path.isfile("data/matchids.csv"):
        matchids_df = pd.read_csv("data/matchids.csv")
        matchids_hx = matchids_df["matchid"].to_list()
        matchids_hx2 = list(set(matchids_hx + sorted_matchids))
        sorted_new = []
        for matchid in matchids_hx2:
            if matchid not in saved_matchids:
                sorted_new.append(matchid)
        print(f"to do : {len(sorted_new)}")
        save_matchids(matchids_hx2)
        print("matchid.csv saved!!")

        try:
            with open("data/match_hx.json", "r") as f:
                match_hx = json.load(f)
        except Exception:
            match_hx = []

        get_match_hx = get_match_historys(sorted_new, match_hx)
else:
    sorted_new = sorted_matchids
    save_matchids(sorted_new)
    print("match id saved!!")
    try:
        with open("data/match_hx.json", "r") as f:
            match_hx = json.load(f)
    except Exception:
        match_hx = []
    print(f"to do: {len(sorted_new)}")
    get_match_hx = get_match_historys(sorted_new, match_hx)
def save_matchids(a):
    matchids_df = pd.DataFrame(a, columns=["matchid"])
    matchids_df.to_csv("data/matchids.csv", index=False, encoding="utf-8-sig")