ffpb_hamburg.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. from __future__ import print_function
  3. import willie
  4. from ffpb import ffpb_fetch_stats
  5. def setup(bot):
  6. try:
  7. ffpb_hamburg_fetch(bot)
  8. except:
  9. pass
  10. def shutdown(bot):
  11. pass
  12. @willie.module.interval(60)
  13. def ffpb_hamburg_fetch(bot):
  14. ffpb_fetch_stats(bot, 'http://hamburg.freifunk.net/nodes_ffhh/nodes.json', 'ffhh_stats')
  15. if 'ffhh_rueckspiegel' in bot.memory and bot.memory['ffhh_rueckspiegel'] == True:
  16. return
  17. stats = bot.memory['ffpb_stats'] if 'ffpb_stats' in bot.memory else None
  18. hamburg_data = bot.memory['ffhh_stats'] if 'ffhh_stats' in bot.memory else None
  19. if not (stats is None or hamburg_data is None):
  20. if hamburg_data['nodes_active'] < stats['nodes_active']:
  21. print('HAMBURG überholt: {0}<{1}'.format(hamburg_data['nodes_active'], stats['nodes_active']))
  22. bot.memory['ffhh_rueckspiegel'] = True
  23. action_msg = u'schaut in den Rückspiegel und erblickt HAMBURG!'
  24. action_target = bot.config.ffpb.msg_target
  25. if not bot.config.ffpb.msg_target_public is None: action_target = bot.config.ffpb.msg_target_public
  26. bot.msg(action_target, '\x01ACTION %s\x01' % action_msg)
  27. @willie.module.commands('hamburg')
  28. def ffpb_hamburg(bot, trigger):
  29. hamburg_data = bot.memory['ffhh_stats'] if 'ffhh_stats' in bot.memory else None
  30. if hamburg_data is None:
  31. bot.say('Hamburg, was ist schon Hamburg ... zumindest habe ich gerade keine Daten.')
  32. return
  33. if (not trigger.group(2) is None) and trigger.group(2).lower() in ['rules','rulez','ist besser','siegt','ist cool','ist die nummer 1','vor','vorne','nr. 1']:
  34. bot.msg(trigger.sender, '\x01ACTION %s\x01' % 'bezweifelt das.')
  35. return
  36. stats = bot.memory['ffpb_stats'] if 'ffpb_stats' in bot.memory else None
  37. if stats is None:
  38. bot.say('Bitte später nochmal fragen :)')
  39. return
  40. diff_nodes = hamburg_data['nodes_active'] - stats['nodes_active']
  41. diff_clients = hamburg_data['clients'] - stats['clients']
  42. bot.say('Hamburg: {0} Knoten (= PB{1:+g}), {2} Clients (= PB{3:+g})'.format(hamburg_data['nodes_active'], diff_nodes, hamburg_data['clients'], diff_clients))