Skip to content

Instantly share code, notes, and snippets.

@json-m json-m/wvw_forwarder.py
Last active May 29, 2018

Embed
What would you like to do?
forwards guild wars 2 wvw stats to graphite
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
@json-m

This comment has been minimized.

Copy link
Owner Author

json-m commented May 29, 2018

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:
demo_img
aliasByNode(gw2.wvw.1-3.blue.*.kills, 4)

demo this with:

docker run -d --name=graphite --restart=unless-stopped -p 80:80 -p 2003-2004:2003-2004 -p 2023-2024:2023-2024 -p 8125:8125/udp -p 8126:8126 -p 8080:8080 graphiteapp/graphite-statsd
docker run -d --name=grafana --restart=unless-stopped -p 3000 -e GF_SERVER_ROOT_URL="http://localhost" -e "GF_SECURITY_ADMIN_PASSWORD=supersecure" grafana/grafana
  1. go to http://localhost:3000/
  2. login with admin/supersecure,
  3. add graphite datasource as http://localhost:8080
  4. start building dashboards

or you can go to http://localhost/ to access graphite if you prefer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.