LoginSignup
15
6

More than 3 years have passed since last update.

Apex LegendsのAPI w/ Python

Last updated at Posted at 2021-02-12

はじめに

この記事では、Pythonを使用してTRACKER NETWORKのAPIを叩いてAPEXの統計情報を取得する方法を解説します。

参考

目次

  • TRACKER NETWORKのAPIを設定する
    • 登録
    • アプリケーションの作成
    • APIキーの確認
  • PyhtonでAPIを叩く
    • APIの叩き方
    • JSONを扱う
    • コード
    • 結果
    • 色々見てみる

TRACKER NETWORKのAPIを設定する

登録

まず、APIを使用するためにTRACKER NETWORKに登録するスクリーンショット 2021-02-12 221420.png
ユーザー名(任意)とメアド、パスワードを入力して登録

アプリケーションの作成

登録が完了したら、次はAPIを利用するためにアプリケーションを作成

ログイン後、トップページのDevelopersタブ、もしくはhttps://tracker.gg/developers/appsからアプリケーション作成ページに移動し、Create Application
スクリーンショット 2021-02-12 222533.png

作成画面でアプリケーションの名前、説明、目的を書いてCreate app

APIキーの確認

作成したアプリケーションの詳細からAPI Keyを見つけてメモ

PythonでAPIを叩く

APIキーが用意できたので次はPythonでAPIを叩いてみます

APIの叩き方

PythonではREST APIを叩くのに便利なrequestsというライブラリがあるのでそれを使っていきます
urlとheadersを指定してAPIを叩きます

今回はApexの情報が欲しいので、https://public-api.tracker.gg/v2/apex/standard/profile/{platform}/{user}の部分にプラットフォーム(OriginやPS、XBOX)Apexのユーザー名を入れて、Apexの統計情報にアクセスします

JSONを扱う

APIを叩いて帰ってくる結果はJSON形式なので、Pythonで扱えるようにjsonライブラリを使います

コード

Apex_api.py
import requests, json, os
from pprint import pprint
url = "https://public-api.tracker.gg/v2/apex/standard/profile/origin/SIN_HA_JECHT_DA"
header = {"TRN-Api-Key":os.environ['TRN_API_KEY']}#環境変数から読み込むようにしてる

res = requests.get(url, headers=header).json()
pprint(res)

結果

結果は長すぎるので一部抜粋してます

{'data': {'availableSegments': [{'attributes': {},
                                 'metadata': {},
                                 'type': 'legend'}],
          'expiryDate': '2021-02-12T14:36:57.1605506+00:00',
          'metadata': {'activeLegend': 'legend_15',
                       'activeLegendName': 'Horizon',
                       'activeLegendStats': None,
                       'currentSeason': 2},
          'platformInfo': {'additionalParameters': None,
                           'avatarUrl': 'https://secure.download.dm.origin.com/production/avatar/prod/userAvatar/33294085/416x416.PNG',
                           'platformSlug': 'origin',
                           'platformUserHandle': 'SIN_HA_JECHT_DA',
                           'platformUserId': 'SIN_HA_JECHT_DA',
                           'platformUserIdentifier': 'SIN_HA_JECHT_DA'},
          'segments': [{'attributes': {},
                        'expiryDate': '2021-02-12T14:36:57.1605506+00:00',
                        'metadata': {'name': 'Lifetime'},
                        'stats': {'damage': {'category': None,
                                             'displayCategory': 'Combat',
                                             'displayName': 'Damage',
                                             'displayType': 'Unspecified',
                                             'displayValue': '159,116',
                                             'metadata': {},
                                             'percentile': 50.0,
                                             'rank': None,
                                             'value': 159116.0},
                                  'headshots': {'category': None,
                                                'displayCategory': 'Combat',
                                                'displayName': 'Headshots',
                                                'displayType': 'Unspecified',
                                                'displayValue': '164',
                                                'metadata': {},
                                                'percentile': 25.0,
                                                'rank': None,
                                                'value': 164.0},
                                  'kills': {'category': None,
                                            'displayCategory': 'Combat',
                                            'displayName': 'Kills',
                                            'displayType': 'Unspecified',
                                            'displayValue': '1,060',
                                            'metadata': {},
                                            'percentile': 79.0,
                                            'rank': None,
                                            'value': 1060.0},
                                  'level': {'category': None,
                                            'displayCategory': 'Combat',
                                            'displayName': 'Level',
                                            'displayType': 'Unspecified',
                                            'displayValue': '295',
                                            'metadata': {},
                                            'percentile': 84.0,
                                            'rank': None,
                                            'value': 295.0},
                                  'rankScore': {'category': None,
                                                'displayCategory': 'Game',
                                                'displayName': 'Rank Score',
                                                'displayType': 'Unspecified',
                                                'displayValue': '4,828',
                                                'metadata': {'iconUrl': 'https://trackercdn.com/cdn/apex.tracker.gg/ranks/platinum4.png',
                                                             'rankName': 'Platinum '
                                                                         '4'},
                                                'percentile': None,
                                                'rank': None,
                                                'value': 4828.0},

有用そうなデータがあるのは、segmentsのあたり
これ以降はレジェンドごとの情報が書いてある

色々見てみる

入れ子になったJSONは辞書と同じように各要素にアクセスする
segmentsはリストになっていて、[0]にはアカウント全体のキル数などの情報があり
以降は各レジェンドごとの情報がある(レジェンドの順は不同)

res['data']['segments'][0]['stats']['rankScore']

これだとランクの情報が得られる
valueがランクポイントでmetadataにはランクの位置が書いてある
僕はプラチナ4です(永遠に)

{'category': None,
 'displayCategory': 'Game',
 'displayName': 'Rank Score',
 'displayType': 'Unspecified',
 'displayValue': '4,828',
 'metadata': {'iconUrl': 'https://trackercdn.com/cdn/apex.tracker.gg/ranks/platinum4.png',
              'rankName': 'Platinum 4'},
 'percentile': None,
 'rank': None,
 'value': 4828.0}

res['data']['segments'][6]

これは僕が一番使ってるホライゾンの情報です

{'attributes': {'id': 'legend_15'},
 'expiryDate': '2021-02-12T14:36:57.1605506+00:00',
 'metadata': {'bgImageUrl': 'https://trackercdn.com/cdn/apex.tracker.gg/legends/horizon-concept-bg-small.jpg',
              'imageUrl': 'https://trackercdn.com/cdn/apex.tracker.gg/legends/horizon-tile.png',
              'isActive': True,
              'name': 'Horizon',
              'tallImageUrl': 'https://trackercdn.com/cdn/apex.tracker.gg/legends/horizon-tall.png'},
 'stats': {'kills': {'category': None,
                     'displayCategory': 'Combat',
                     'displayName': 'Kills',
                     'displayType': 'Unspecified',
                     'displayValue': '504',
                     'metadata': {},
                     'percentile': 94.0,
                     'rank': None,
                     'value': 504.0},
           'season7Kills': {'category': None,
                            'displayCategory': 'Game',
                            'displayName': 'Season 7 Kills',
                            'displayType': 'Unspecified',
                            'displayValue': '402',
                            'metadata': {},
                            'percentile': 86.0,
                            'rank': None,
                            'value': 402.0},
           'season7Wins': {'category': None,
                           'displayCategory': 'Game',
                           'displayName': 'Season 7 Wins',
                           'displayType': 'Unspecified',
                           'displayValue': '17',
                           'metadata': {},
                           'percentile': 83.0,
                           'rank': None,
                           'value': 17.0},
           'ultimateEnemyDamage': {'category': None,
                                   'displayCategory': 'Game',
                                   'displayName': 'Ultimate Enemy Damage',
                                   'displayType': 'Unspecified',
                                   'displayValue': '3,707',
                                   'metadata': {},
                                   'percentile': 71.0,
                                   'rank': None,
                                   'value': 3707.0}},
 'type': 'legend'}

500キルぐらいしてる

終わり

15
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
6