hwet-j / Pubg

0 stars 0 forks source link

API 사용 #2

Open hwet-j opened 2 years ago

hwet-j commented 2 years ago

참고

다른분이 API 사용한 코드

https://right1203.tistory.com/2 https://github.com/dataitgirls4/team_5

PUBG의 대회 기록 정보

https://esports.pubgrank.org/events

API

https://chicken-dinner.readthedocs.io/en/latest/index.html

===========================

https://chickendinner.gg/player/Hwet_J/steam

PUBG 분석 도구

pip install chicken_dinner

hwet-j commented 2 years ago

2021년 PGC대회 데이터를 사용

PGC대회 방식

순위결정전 -> 위클리서바이벌 -> 위클리파이널 -> 바텀 -> 위클리서바이벌 -> 위클리파이널 -> 바텀 -> 위클리서바이벌 -> 위클리파이널 -> 그랜드서바이벌 -> 그랜드파이널

순위결정전(rd): 3일 18경기
위클리 시리즈
위클리 서바이벌(ws) : 3일 16경기
위클리 파이널(wf) : 2일 10경기
바텀 16(bm) : 1일 6경기 - 하위 16개팀만 경기를 진행
그랜드 서바이벌(gs) : 1일 4경기
그랜드 파이널(gf) : 3일간 15경기
hwet-j commented 2 years ago

2021년 모든 대회에서 PGC대회 데이터 가져오기

대회 데이터를 가져오는 url

url = "https://api.pubg.com/tournaments"

# 모든 대회 데이터
url = "https://api.pubg.com/tournaments"
r = requests.get(url, headers=headers)
pgc = r.json()

# 2021년 PGC대회 데이터 (시간과 대회명 저장 )
match_league = {league['attributes']['createdAt']: league['id'] for league in pgc['data'] \
                if 'pgc' in league['id'] and int(league['attributes']['createdAt'][0:4]) == 2021}

# 2021년 PGC대회의 데이터의 matchId를 가져오는 반복문 (각 대회마다 match기록을 가져와 저장)
pgc_matchId_dict = {}   # createdAt, id 데이터를 저장할 딕셔너리 
for league in match_league.values():
    url = "https://api.pubg.com/tournaments/" + league
    r = requests.get(url, headers=headers)
    leg_json = r.json()
    # 각 경기마다 데이터를 딕셔너리에 추가
    pgc_matchId_dict.update({match['attributes']['createdAt']: match['id'] for match in leg_json['included']})
hwet-j commented 2 years ago

2021년 PGC 대회의 GEN_G 팀이 경기한 데이터 가져오기

대회 데이터를 가져오는 url

url = "https://api.pubg.com/tournaments"

# Chicken_dinner 라이브러리 사용
pubg = PUBG(api_key=api_key, shard='tournament')
# Gen_G 선수들 경기만 저장
matchId_gen = []

# tqdm는 for문의 진행도를 나타내주는 라이브러리
for matchId in tqdm(pgc_matchId_df['matchId']):
    current_match = pubg.match(matchId) # 주소값 반환
    telemetry = current_match.get_telemetry()
    player_name = telemetry.player_names()
    findCount = (pd.Series(telemetry.player_names()).str.find('GEN') > -1).sum()    # str.find 못찾으면 -1 찾으면 0부터 시작
    if findCount > 0: # Count가 1개이상이면 그 matchId를 리스트에 추가
        matchId_gen.append(matchId)

# 우선 텍스트 저장 ( 이후 DB 사용 ) 
# 젠지가 경기한 matchId를 저장한 리스트를 텍스트로 저장 -> 같은 코드를 반복하지 않기 위해
with open('gen_matchId.txt','w+',encoding='UTF-8') as f:
    f.write('\n'.join(matchId_gen))  # 데이터와 데이터 사이에 \n(줄바꿈)을 넣어준다. (마지막에 추가되지 않음)

#  저장해둔 데이터 사용 
matchId_gen =[]
with open('gen_matchId.txt','r',encoding='UTF-8') as f:
    for line in f:
        matchId_gen.append(line.strip('\n'))  # 줄바꿈을 기준으로 하여 리스트에 저장
hwet-j commented 2 years ago

필터 URL

playerNames 필터 url = "https://api.pubg.com/shards/steam/players?filter[playerNames]=Hwet_J"

account Id 필터 url = "https://api.pubg.com/shards/steam/players/{account](https://api.pubg.com/shards/steam/players/{account) Id}"

matches 필터 url = "https://api.pubg.com/shards/steam/matches/3cace53f-0004-4af0-91b5-5f53526615fb"

tournaments 필터 url = "https://api.pubg.com/shards/tournament/{id}" ==> id를 적어주지 않으면 전체 대회기록을 불러옴

hwet-j commented 1 year ago

URL을 사용한 API로 데이터를 가져올 수 있지만, PUBG라이브러리를 통해서도 가져올 수 있다.

# API 라이브러리 호출
from chicken_dinner.pubgapi import PUBG 
# PUBG 인스턴스 설정 
pubg = PUBG(api)key, shard) 
# match 데이터를 가져올 수 있는 라이브러리 호출
from chicken_dinner.models.match import Match
# match_id에 맞는 match 인스턴스를 가져옴
data = Match(pubg, match_id, shard=None)