Last active
May 29, 2018 13:55
-
-
Save json-m/18aa40d081ba730218ba73b3787383a7 to your computer and use it in GitHub Desktop.
forwards guild wars 2 wvw stats to graphite
This file contains 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
import requests | |
from requests import exceptions | |
import json | |
import time | |
import graphyte | |
# setup: | |
# pip3 install requests graphyte | |
match_url = 'https://api.guildwars2.com/v2/wvw/matches' | |
matches_dict = {} | |
graphyte.init('localhost', prefix='gw2.wvw') # change to your server | |
# structure: | |
# gw2.wvw.matchup.color.victory_points | |
# gw2.wvw.matchup.color.score | |
# gw2.wvw.matchup.color.kills | |
# gw2.wvw.matchup.color.deaths | |
# gw2.wvw.matchup.color.map.score | |
# gw2.wvw.matchup.color.map.kills | |
# gw2.wvw.matchup.color.map.deaths | |
def get_match_list(): | |
global matches_dict | |
try: | |
m = requests.get(match_url) | |
match_list = json.loads(m.text) | |
except exceptions: | |
now = time.time() | |
print(now, ' failed to get matches:\n\n') | |
print(exceptions) | |
exit(1) | |
for match_id in match_list: | |
matches_dict[match_id] = (match_url + "/" + match_id) | |
def poll_match(url): | |
global match_data | |
try: | |
r = requests.get(url) | |
match_data = json.loads(r.text) | |
except exceptions: | |
now = time.time() | |
print(now, ' failed to get match data:\n\n') | |
print(exceptions) | |
def get_score(): | |
global match_data | |
for vp_team, victory_points in match_data['victory_points'].items(): | |
graphyte.send(str(m_id) + "." + vp_team + ".victory_points", victory_points) | |
for s_team, total_score in match_data['scores'].items(): | |
graphyte.send(str(m_id) + "." + s_team + ".score", total_score) | |
def get_total_kills(): | |
global match_data | |
for tk_team, tk_kills in match_data['kills'].items(): | |
graphyte.send(str(m_id) + "." + tk_team + ".kills", tk_kills) | |
def get_total_deaths(): | |
global match_data | |
for td_team, td_deaths in match_data['deaths'].items(): | |
graphyte.send(str(m_id) + "." + td_team + ".deaths", td_deaths) | |
def zone_stats(): | |
for m_team, m_score in zone.get('scores').items(): | |
graphyte.send(str(m_id) + "." + m_team + "." + mapname + ".score", m_score) | |
for k_team, k_kills in zone.get('kills').items(): | |
for d_team, d_deaths in zone.get('deaths').items(): | |
if k_team in d_team: | |
graphyte.send(str(m_id) + "." + k_team + "." + mapname + ".kills", k_kills) | |
graphyte.send(str(m_id) + "." + d_team + "." + mapname + ".deaths", d_deaths) | |
def get_map_stats(): | |
global match_data | |
global zone | |
global mapname | |
for zone in match_data['maps']: | |
if zone.get('type') == "Center": | |
mapname = "eternal_bg" | |
zone_stats() | |
if zone.get('type') == "GreenHome": | |
mapname = "green_bl" | |
zone_stats() | |
if zone.get('type') == "BlueHome": | |
mapname = "blue_bl" | |
zone_stats() | |
if zone.get('type') == "RedHome": | |
mapname = "red_bl" | |
zone_stats() | |
if __name__ == '__main__': | |
while True: | |
get_match_list() # doing this in case tier count changes due to de-links | |
for m_id, m_url in matches_dict.items(): | |
poll_match(m_url) | |
get_score() | |
get_total_kills() | |
get_total_deaths() | |
get_map_stats() | |
time.sleep(30) # this is 20 req/min, max to api is 600/min |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this was like my 2nd python script ever, so i'm open to improvements
you can load these into graphite directly or grafana like so:
aliasByNode(gw2.wvw.1-3.blue.*.kills, 4)
demo this with:
http://localhost:8080
or you can go to http://localhost/ to access graphite if you prefer