Last active
October 7, 2019 04:00
-
-
Save mikechambers/fe7c8f15029e6bb45c81af83543d13b2 to your computer and use it in GitHub Desktop.
Simple dart based command line app for looking up Destiny 2 player glory on consoles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Copyright 2019 Mike Chambers | |
| //Released under an MIT License | |
| //https://opensource.org/licenses/MIT | |
| //usage : dart glory.dart platform[xbox|psn] gamertag | |
| //example : dart glory.dart xbox mesh | |
| import 'dart:convert'; | |
| import 'dart:io'; | |
| //insert your own key from | |
| //https://www.bungie.net/developer | |
| final String _apiKey = ""; | |
| final String _apiBase = "https://www.bungie.net/Platform"; | |
| final int xbox = 1; | |
| final int psn = 2; | |
| final int none = 0; | |
| void main(List<String> args) async { | |
| if(args.length != 2) { | |
| _displayUsage(); | |
| exit(1); | |
| } | |
| String tag = args[1]; | |
| int platformId = _parsePlatform(args[0]); | |
| if(platformId == none) { | |
| _displayUsage(); | |
| exit(1); | |
| } | |
| int memberId = await _getMemberId(tag, platformId); | |
| if (memberId == null) { | |
| print("Gamer tag not found"); | |
| exit(1); | |
| } | |
| int glory = await _getGlory(memberId); | |
| print("$tag : $glory Glory"); | |
| exit(0); | |
| } | |
| int _parsePlatform(String name) { | |
| name = name.toLowerCase(); | |
| if(name == "xbox") { | |
| return xbox; | |
| } else if(name == "psn") { | |
| return psn; | |
| } | |
| return none; | |
| } | |
| void _displayUsage() { | |
| print("dart glory.dart platform[xbox|psn] gamertag"); | |
| } | |
| Future<int> _getGlory(int memberId) async { | |
| String path = "/Destiny2/1/Profile/$memberId/?components=202"; | |
| Map<String, dynamic> jData = await _callApi(path); | |
| Map data = jData["Response"]["characterProgressions"]["data"]; | |
| String key = data.keys.first; | |
| int glory = data[key]["progressions"]["2679551909"]["currentProgress"]; | |
| return glory; | |
| } | |
| Future<int> _getMemberId(String name, int platformId) async { | |
| String path = | |
| "/Destiny2/SearchDestinyPlayer/$platformId/${Uri.encodeComponent(name)}/"; | |
| Map jData = await _callApi(path); | |
| List<dynamic> response = jData["Response"]; | |
| for (Map item in response) { | |
| if (item["displayName"] == name) { | |
| return int.parse(item["membershipId"]); | |
| } | |
| } | |
| //user not found | |
| return null; | |
| } | |
| Future<Map<String, dynamic>> _callApi(String apiPath) async { | |
| HttpClient client = HttpClient(); | |
| client.connectionTimeout = new Duration(seconds: 10); | |
| Uri uri = Uri.parse("$_apiBase$apiPath"); | |
| var request = await client.getUrl(uri); | |
| request.headers.set("X-API-Key", _apiKey); | |
| HttpClientResponse response = await request.close(); | |
| StringBuffer buffer = new StringBuffer(""); | |
| await for (String b in response.transform(utf8.decoder)) { | |
| buffer.write(b); | |
| } | |
| String body = buffer.toString(); | |
| Map<String, dynamic> jData = json.decode(body); | |
| return jData; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment