ffpb_hamburg.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. """Called by willie upon loading this plugin."""
  7. try:
  8. ffpb_hamburg_fetch(bot)
  9. except:
  10. pass
  11. def shutdown(bot):
  12. """Called by willie upon unloading this plugin."""
  13. pass
  14. @willie.module.interval(60)
  15. def ffpb_hamburg_fetch(bot):
  16. ffpb_fetch_stats(bot, 'https://hamburg.freifunk.net/nodes_ffhh/nodes.json', 'ffhh_stats')
  17. if 'ffhh_rueckspiegel' in bot.memory and bot.memory['ffhh_rueckspiegel'] == True:
  18. return
  19. stats = bot.memory['ffpb_stats'] if 'ffpb_stats' in bot.memory else None
  20. hamburg_data = bot.memory['ffhh_stats'] if 'ffhh_stats' in bot.memory else None
  21. if not (stats is None or hamburg_data is None):
  22. if hamburg_data['nodes_active'] < stats['nodes_active']:
  23. print('HAMBURG überholt: {0}<{1}'.format(hamburg_data['nodes_active'], stats['nodes_active']))
  24. bot.memory['ffhh_rueckspiegel'] = True
  25. action_msg = u'schaut in den Rückspiegel und erblickt ... HAMBURG!'
  26. action_target = bot.config.ffpb.msg_target
  27. if not bot.config.ffpb.msg_target_public is None:
  28. action_target = bot.config.ffpb.msg_target_public
  29. bot.msg(action_target, '\x01ACTION %s\x01' % action_msg)
  30. @willie.module.commands('hamburg')
  31. def ffpb_hamburg(bot, trigger):
  32. hamburg_data = bot.memory.get('ffhh_stats', None)
  33. if hamburg_data is None:
  34. bot.say('Hamburg, was ist schon Hamburg ... zumindest habe ich gerade keine Daten.')
  35. return
  36. if not trigger.group(2) is None:
  37. insane_suffixes = ['rules', 'rulez', 'ist besser', 'siegt', 'ist cool', 'ist die nummer 1', 'vor', 'vorne', 'nr. 1']
  38. if trigger.group(2).lower() in insane_suffixes:
  39. bot.msg(trigger.sender, '\x01ACTION %s\x01' % 'bezweifelt das.')
  40. return
  41. stats = bot.memory['ffpb_stats'] if 'ffpb_stats' in bot.memory else None
  42. if stats is None:
  43. bot.say('Bitte später nochmal fragen :)')
  44. return
  45. diff_nodes = hamburg_data['nodes_active'] - stats['nodes_active']
  46. diff_clients = hamburg_data['clients'] - stats['clients']
  47. bot.say('Hamburg: {0} Knoten (= PB{1:+g}), {2} Clients (= PB{3:+g})'.format(
  48. hamburg_data['nodes_active'],
  49. diff_nodes,
  50. hamburg_data['clients'],
  51. diff_clients,
  52. ))