mirror of
https://github.com/nikdoof/dropbot.git
synced 2025-12-13 02:42:17 +00:00
Switch to listening on STOMP instead of polling.
This commit is contained in:
@@ -16,6 +16,7 @@ from eveapi import EVEAPIConnection
|
||||
|
||||
from dropbot.map import Map, base_range, ship_class_to_range
|
||||
from dropbot.utils import EVEAPIRedisCache
|
||||
from dropbot.stomp_listener import ZKillboardStompListener
|
||||
|
||||
urlparse.uses_netloc.append("redis")
|
||||
|
||||
@@ -63,6 +64,9 @@ class DropBot(ClientXMPP):
|
||||
self.add_event_handler('session_start', self.handle_session_start)
|
||||
self.add_event_handler('message', self.handle_message)
|
||||
|
||||
# STOMP listener
|
||||
self.stomp = ZKillboardStompListener(self)
|
||||
|
||||
# Reference Data
|
||||
|
||||
@property
|
||||
@@ -83,7 +87,7 @@ class DropBot(ClientXMPP):
|
||||
self.plugin['xep_0045'].joinMUC(room, self.nickname, wait=True)
|
||||
|
||||
# Start the killchecker
|
||||
self._get_kills()
|
||||
self.stomp.connect('tcp://eve-kill.net:61613')
|
||||
|
||||
def call_command(self, command, *args, **kwargs):
|
||||
if hasattr(self, 'cmd_%s' % command):
|
||||
@@ -196,22 +200,6 @@ class DropBot(ClientXMPP):
|
||||
intcomma(float(root.findall("./marketstat/type[@id='{}']/buy/max".format(type_id))[0].text)),
|
||||
)
|
||||
|
||||
def _get_kills(self):
|
||||
secs = (datetime.utcnow() - self.last_killdate).total_seconds()
|
||||
if secs >= self.kill_check_timeout:
|
||||
for corp_id in self.kill_corps:
|
||||
headers, kills = ZKillboard().corporationID(corp_id).pastSeconds(int(secs)).kills().get()
|
||||
res = []
|
||||
for kill in kills:
|
||||
body, html = self.call_command('kill', [kill['killID']], None, no_url=False)
|
||||
res.append(body)
|
||||
if len(res):
|
||||
text = 'New Kills:\n{}'.format('\n'.join(res))
|
||||
for room in self.rooms:
|
||||
self.send_message(room, text, mtype='groupchat')
|
||||
self.last_killdate = datetime.utcnow()
|
||||
self.schedule('zkb_check', self.kill_check_timeout, self._get_kills)
|
||||
|
||||
def get_eveapi(self):
|
||||
return EVEAPIConnection(cacheHandler=EVEAPIRedisCache(self.redis))
|
||||
|
||||
@@ -589,21 +577,26 @@ class DropBot(ClientXMPP):
|
||||
', '.join([x for x, y in alli_assoc])
|
||||
)
|
||||
|
||||
def cmd_kill(self, args, msg, no_url=False):
|
||||
def cmd_kill(self, args, msg, no_url=False, raw=None):
|
||||
"""Returns a summary of a zKillboard killmail"""
|
||||
if len(args) == 0:
|
||||
return '!kill <Kill ID/zKillboard URL>'
|
||||
kill_id = args[0]
|
||||
try:
|
||||
kill_id = int(kill_id)
|
||||
except ValueError:
|
||||
m = zkillboard_regex.match(kill_id)
|
||||
if m:
|
||||
kill_id = m.groupdict()['killID']
|
||||
else:
|
||||
return 'Invalid kill ID'
|
||||
headers, data = ZKillboard().killID(kill_id).get()
|
||||
kill = data[0]
|
||||
if not raw:
|
||||
if len(args) == 0:
|
||||
return '!kill <Kill ID/zKillboard URL>'
|
||||
kill_id = args[0]
|
||||
try:
|
||||
kill_id = int(kill_id)
|
||||
except ValueError:
|
||||
m = zkillboard_regex.match(kill_id)
|
||||
if m:
|
||||
kill_id = m.groupdict()['killID']
|
||||
else:
|
||||
return 'Invalid kill ID'
|
||||
|
||||
headers, data = ZKillboard().killID(kill_id).get()
|
||||
kill = data[0]
|
||||
else:
|
||||
kill = raw
|
||||
kill_id = raw['killID']
|
||||
|
||||
if no_url:
|
||||
url = ''
|
||||
|
||||
38
dropbot/stomp_listener.py
Normal file
38
dropbot/stomp_listener.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import stomp
|
||||
import urlparse
|
||||
import json
|
||||
|
||||
urlparse.uses_netloc.append('tcp')
|
||||
|
||||
class ZKillboardStompListener(object):
|
||||
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
self.conn = None
|
||||
|
||||
def on_error(self, headers, message):
|
||||
pass
|
||||
|
||||
def on_message(self, headers, message):
|
||||
kill = json.loads(message)
|
||||
for attacker in kill['attackers']:
|
||||
if int(attacker['corporationID']) in self.bot.kill_corps:
|
||||
break
|
||||
else:
|
||||
if kill['victim']['corporationID'] not in self.bot.kill_corps:
|
||||
return
|
||||
|
||||
print message
|
||||
body, html = self.bot.call_command('kill', [], None, no_url=False, raw=kill)
|
||||
text = 'New Kill: {}'.format(body)
|
||||
for room in self.bot.rooms:
|
||||
self.bot.send_message(room, text, mtype='groupchat')
|
||||
|
||||
|
||||
def connect(self, url):
|
||||
url = urlparse.urlparse(url)
|
||||
self.conn = stomp.Connection([(url.hostname, url.port)])
|
||||
self.conn.set_listener('', self)
|
||||
self.conn.start()
|
||||
self.conn.connect('guest', 'guest')
|
||||
self.conn.subscribe(destination='/topic/kills', ack='auto', id=1)
|
||||
@@ -5,4 +5,5 @@ requests==2.3.0
|
||||
humanize==0.5
|
||||
dnspython==1.11.1
|
||||
networkx==1.9
|
||||
https://github.com/nikdoof/pyzkb/archive/master.zip#egg=pyzkb
|
||||
https://github.com/nikdoof/pyzkb/archive/master.zip#egg=pyzkb
|
||||
stomp.py==4.0.12
|
||||
Reference in New Issue
Block a user