Skip to content

Instantly share code, notes, and snippets.

@souldeux
Last active August 29, 2015 14:07
Show Gist options
  • Save souldeux/bc365282a050790b3aae to your computer and use it in GitHub Desktop.
Save souldeux/bc365282a050790b3aae to your computer and use it in GitHub Desktop.
League of Legends Server Status Monitor
import requests
import json
from peewee import *
from datetime import datetime
import time
api_key = 'secretkey'
def get_shards():
"""Sends an API request to the lol-status/shards endpoint and strips out
the crap we dont care about from the response. We just get a list of slugs"""
jsoncrap = requests.get('http://status.leagueoflegends.com/shards?='+api_key).text
shards = []
for shard_dict in json.loads(jsoncrap):
shards.append(shard_dict['slug'])
return shards
def get_shard_status(shardslug):
"""Given a slug, sends an API request and returns the current server status
for that shard."""
url = 'http://status.leagueoflegends.com/shards/' + shardslug + '?=' + api_key
jsoncrap = requests.get(url).text
dumpdict = json.loads(jsoncrap)
stat_dict = {}
for s in dumpdict['services']:
stat_dict[s['name']] = s['status']
status = [shardslug, stat_dict]
return status
#peewee orm stuff
db = SqliteDatabase('lol.db')
class Shard(Model):
slug = CharField(unique=True)
class Meta:
database = db
class Status(Model):
shard = ForeignKeyField(Shard, related_name='stats')
website = BooleanField()
game = BooleanField()
forum = BooleanField()
store = BooleanField()
timestamp = DateTimeField()
class Meta:
database = db
def save_status(status):
"""Given a single status entry from get_shard_status, saves a status
report in the database."""
shard = Shard.select().where(Shard.slug == status[0]).get()
if status[1]['Website'] == 'online':
w = True
else:
w = False
if status[1]['Game'] == 'online':
g = True
else:
g = False
if status[1]['Forums'] == 'online':
f = True
else:
f = False
if status[1]['Store'] == 'online':
s = True
else:
s = False
timestamp = datetime.now()
report = Status.create(shard=shard, website=w, game=g, forum=f, store=s, timestamp=timestamp)
return report
if __name__ == '__main__':
while True:
for s in get_shards():
print 'Fetching status for %s' % s
status = save_status(get_shard_status(s))
print '%s: %s %s %s %s %s' % (status.shard.slug, status.website, status.game, status.forum, status.store, str(status.timestamp))
print '\n**********'
print 'Naptime'
print '**********'
time.sleep(3000)
@souldeux
Copy link
Author

souldeux commented Oct 5, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment