Browse Source

ffpb: status is fetched in background (every 15s) + client count

Helge Jung 10 years ago
parent
commit
b77264c705
1 changed files with 36 additions and 5 deletions
  1. 36 5
      modules/ffpb.py

+ 36 - 5
modules/ffpb.py

@@ -4,6 +4,7 @@ import willie
 
 import git
 import netaddr
+import json
 import urllib2
 import re
 import os
@@ -16,6 +17,7 @@ import threading
 
 msgserver = None
 peers_repo = None
+stats = None
 
 ffpb_resolver = dns.resolver.Resolver ()
 ffpb_resolver.nameservers = ['10.132.254.53']
@@ -126,14 +128,43 @@ def ffpb_updatepeers(bot):
 
 		bot.msg(bot.config.ffpb.msg_target, response)
 
+@willie.module.interval(15)
+def ffpb_get_stats(bot):
+	global stats
+
+	response = urllib2.urlopen('http://map.paderborn.freifunk.net/nodes.json')
+
+	data = json.load(response)
+	nodes_active = 0
+	nodes_total = 0
+	clients_count = 0
+
+	for node in data['nodes']:
+		if node['flags']['gateway'] or node['flags']['client']:
+			continue
+
+		nodes_total += 1
+		if node['flags']['online']:
+			nodes_active += 1
+
+	for link in data['links']:
+		if link['type'] == 'client':
+			clients_count += 1
+
+	if stats is None:
+		stats = { }
+	stats["nodes_active"] = nodes_active
+	stats["nodes_total"] = nodes_total
+	stats["clients"] = clients_count
+
 @willie.module.commands('status')
 def ffpb_status(bot, trigger):
 	"""Status des FFPB-Netzes: Anzahl (aktiver) Knoten + Clients"""
-	response = urllib2.urlopen('http://nodecount.paderborn.freifunk.net/')
-	html = response.read()
-	m = re.search('<div id="nodecount">\s*(\d+)\s*</div>', html)
-	nodecount = int(m.group(1))
-	bot.say('nodecount = {}'.format(nodecount))
+	if stats is None:
+		bot.say('Uff, kein Plan wo der Zettel ist. Fragst du später nochmal?')
+		return
+
+	bot.say('Es sind {0} Knoten und ca. {1} Clients online.'.format(stats["nodes_active"], stats["clients"]))
 
 def ffpb_get_address(name):
 	peerfilename = '/home/ffpb-statusbot/knoten/' + name