4zakiの日記

技術ブログです。

アフィリエイト広告を利用しています

ApexLegendsのTrackerのAPIをnode.jsで使ってみた。

ApexLegendsのTrackerを覗いていたらAPIが用意されていることに気が付いたので、少し触ってみた。

やりたいこと

node.jsを使って一部情報を取り出す。

参考にしたサイト

medium.com

環境

windows10
Git-bash
node js:v14.16.0

APIキーの取得

以下のサイトにログインをして、「Create Application」で入力を済ませるともらえます。
tracker.gg
f:id:H4zaki:20210303214741p:plain

取り出せる項目を確認する。

ターミナルで以下をたたく。

$ curl https://public-api.tracker.gg/apex/v1/standard/profile/[プラットフォーム]/[アカウント名] -H "TRN-API-KEY: [APIキー]"
((プラットフォームのところにはxboxの場合は「1」,psnの場合は「2」,pcの場合は「5」が入る。))

コード

const https = require('follow-redirects').https;
//プラットフォーム(xbox=1,psn=2,pc=5)
var platform ="[プラットフォーム]";
//ゲームID
var id = "[ゲームID]";
var url = 'https://public-api.tracker.gg/apex/v1/standard/profile/' + platform + '/' + id;

//メイン処理
https.get(url, {
    headers: {
        //APIキー
        "TRN-Api-Key" : "cdda4d52-c67a-4862-acb2-xxxxxxxxxxxx" 
    }
}, function (res) {
    var data = '';
    res.on('data', function (chunk) {
        data += chunk.toString();
    });
    res.on('end', function () { 
        
        var json; 
        try {             
            json = JSON.parse(data);
            //console.log(json.data);
            //取り出したいものを指定&表示
            console.log("アカウント" + json.data.metadata.platformUserHandle);
            console.log("レベル" + json.data.metadata.level);
            console.log("総キル数" + json.data.stats[1].displayValue);
        }
        catch (e) {
            console.log("error: " + e);
        }
    });
    }).on('error', function (err) {
   console.error(err);
   
});

実行

$ node apex.js
アカウント*************
レベル243
総キル数1,576

今回は触りだけ、もしかしたらなんか作るかも。
以上