-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdankbot.py
executable file
·436 lines (372 loc) · 18.4 KB
/
dankbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import os
import time
import requests
import configparser
import logging
import logging.handlers
import sqlite3
from esipy import App
from esipy import EsiClient
from slackclient import SlackClient
itemdb = sqlite3.connect('itemdb.sqlite3')
logging.getLogger('requests').setLevel(logging.WARNING)
logging.getLogger('urllib3').setLevel(logging.WARNING)
config = configparser.SafeConfigParser()
searches = configparser.SafeConfigParser()
sc = None
def main(configpath="."):
global sc, swagger, esi
config.read("%s/config.ini" % configpath)
searches.read("%s/searches.ini" % configpath)
sc = SlackClient(config.get('slack', 'slack_api_token'))
swagger = App.create(url="https://esi.tech.ccp.is/latest/swagger.json?datasource=tranquility")
esi = EsiClient(
retry_requests=True, # set to retry on http 5xx error (default False)
header={'User-Agent': 'Killmail Slack Bot by Prozn https://github.com/prozn/dankbot/'}
)
while True:
if getRedisq():
time.sleep(0.5)
else:
time.sleep(5)
def getRedisq():
try:
r = requests.get('https://redisq.zkillboard.com/listen.php')
response = r.json()
except Exception:
logger.warning("Error occurred calling zkill redisq")
return False
if response.get('package') is None:
logger.debug('No killmail received.')
else:
cycleChannels(prepareKillmail(response.get('package')))
return True
def prepareKillmail(package):
attackerList = []
for attacker in package.get('killmail', {}).get('attackers', {}):
att = {
'character': attacker.get('character_id', 0),
'corporation': attacker.get('corporation_id', 0),
'alliance': attacker.get('alliance_id', 0),
'ship': attacker.get('ship_type_id', 0),
}
if att['corporation'] not in (None, "0", 0):
attackerList.append(att)
if attacker.get('final_blow') is True:
finalBlow = att
del att
if len(attackerList) == 0:
attackerList.append(finalBlow)
cleanMail = {
'id': package.get('killID'),
'solo': True if len(attackerList) == 1 else False,
'victim': {
'character': package.get('killmail', {}).get('victim', {}).get('character_id', 0),
'corporation': package.get('killmail', {}).get('victim', {}).get('corporation_id', 0),
'alliance': package.get('killmail', {}).get('victim', {}).get('alliance_id', 0),
'ship': package.get('killmail', {}).get('victim', {}).get('ship_type_id', 0),
},
'location': {
'id': package.get('killmail', {}).get('solar_system_id', 0),
},
'value': package.get('zkb', {}).get('totalValue'),
'attackers': attackerList,
'finalBlow': finalBlow
}
return cleanMail
# This function is a COMPLETE MESS and needs cleaning up at some point...
def cycleChannels(km):
sentchannels = []
for channel in searches.sections():
logger.debug("Searching channel %s" % channel)
if not searches.has_option(channel,'is_intel_search') or not searches.getboolean(channel,'is_intel_search'):
if searches.get(channel, 'channel_name') in sentchannels:
logger.debug("Killmail has already been sent to channel %s, skipping."
% searches.get(channel, 'channel_name'))
continue
if searches.getboolean(channel, 'include_capsules') is False and \
km['victim']['ship'] in map(int, config.get('killboard', 'capsule_type_ids').split(',')):
logger.debug("Kill is a pod and pods are ignored by config.")
continue
if km['victim']['ship'] in map(int, config.get('killboard', 'capsule_type_ids').split(',')) and \
km['value'] < searches.getfloat(channel, 'minimum_capsule_value'):
logger.debug("Kill is a pod and value is below minimum capsule value in config.")
continue
if any(a[searches.get(channel, 'zkill_search_type')]
in map(int, searches.get(channel, 'zkill_search_id').split(',')) for a in km['attackers']):
if km['solo'] is True:
sendKill('solo', channel, km)
sentchannels.append(searches.get(channel, 'channel_name'))
continue
if km['value'] >= searches.getfloat(channel, 'expensive_kill_limit'):
sendKill('expensive', channel, km)
continue
logger.debug("Matching kill found for channel (%s) but it was not solo or expensive" % channel)
if searches.getboolean(channel, 'post_losses') and \
km['victim'][searches.get(channel, 'zkill_search_type')] == searches.getint(channel, 'zkill_search_id'):
if len(searches.get(channel, 'loss_ship_type_ids')) > 0 and \
km['victim']['ship'] in map(int, searches.get(channel, 'loss_ship_type_ids').split(',')):
sendKill('loss_ship', channel, km)
continue
try:
if searches.getboolean(channel, 'loss_value') is False:
pass
else:
logger.warning("Loss value config param evaluated to True"
" - permitted settings are False or numerical")
pass
except ValueError:
if km['value'] >= searches.getfloat(channel, 'loss_value'):
sendKill('loss_expensive', channel, km)
continue
if km['victim']['ship'] in map(int, config.get('killboard', 'super_type_ids').split(',')) and \
searches.getboolean(channel, 'post_all_super_kills'):
sendKill('super', channel, km)
continue
else:
ships_to_search = set([])
if ( searches.getboolean(channel,'post_kills') \
and any(a[searches.get(channel, 'zkill_search_type')] in map(int, searches.get(channel, 'zkill_search_id').split(',')) for a in km['attackers']) ) \
or ( searches.getboolean(channel,'post_kills') and any(a[searches.get(channel, 'zkill_search_type')] in \
map(int, searches.get(channel, 'zkill_search_id').split(',')) for a in km['attackers']) ):
if searches.getboolean(channel,'include_supers'):
ships_to_search.update(list(map(int, config.get('killboard','super_type_ids').split(','))))
if searches.getboolean(channel,'include_carriers'):
ships_to_search.update(list(map(int, config.get('killboard','carrier_type_ids').split(','))))
if searches.getboolean(channel,'include_dreads'):
ships_to_search.update(list(map(int, config.get('killboard','dreadnaught_type_ids').split(','))))
if searches.getboolean(channel,'include_faxes'):
ships_to_search.update(list(map(int, config.get('killboard','fax_type_ids').split(','))))
if searches.getboolean(channel,'include_blops'):
ships_to_search.update(list(map(int, config.get('killboard','blops_type_ids').split(','))))
# logger.debug(','.join(map(str, ships_to_search)))
if searches.getboolean(channel,'post_kills') and any(a[searches.get(channel, 'zkill_search_type')] in \
map(int, searches.get(channel, 'zkill_search_id').split(',')) for a in km['attackers']):
if searches.getboolean(channel, 'include_all') or any(int(b['ship']) in ships_to_search for b in km['attackers']):
sendKill('intel', channel, km)
else:
logger.debug("Kill was found but not by an interesting ship.")
if searches.getboolean(channel, 'post_losses') and \
km['victim'][searches.get(channel,'zkill_search_type')] == searches.get(channel, 'zkill_search_id'):
if searches.getboolean(channel, 'include_all') or int(km['victim']['ship']) in ships_to_search:
sendKill('intel', channel, km)
else:
logger.debug("Kill was found but not by an interesting ship.")
def fluffKillmail(km):
# Call ESI API and add:
# For victim, attackers, finalBlow: name, shipName
# For victim, finalBlow: corpName, allianceName
# For location: name
# Build lists of characters, corporations, alliances, ships and solar systems to get details for
characters, corporations, alliances, ships = (set([]), set([]), set([]), set([]))
characters.add(km['victim']['character'])
corporations.add(km['victim']['corporation'])
alliances.add(km['victim']['alliance'])
km['victim']['shipName'] = getItemName(km['victim']['ship'])
for character in km['attackers']:
characters.add(character['character'])
corporations.add(character['corporation'])
alliances.add(character['alliance'])
# Lists built, get the data from ESI
get_system_details = swagger.op['get_universe_systems_system_id'](
system_id=km['location']['id']
)
system_details = esi.request(get_system_details)
# Maximum number of characters we can request at once is 100, get 90 to be safe
char_array = {0: 'Unknown'}
for characters_chunk in [list(characters)[i:i+90] for i in range(0, len(characters), 90)]:
get_character_details = swagger.op['get_characters_names'](
character_ids=','.join(str(x) for x in characters_chunk if x != 0)
)
char_name_list = esi.request(get_character_details)
for character in char_name_list.data:
char_array[character.character_id] = character.character_name
# Maximum number of corporations we can request at once is 100, get 90 to be safe
corp_array = {0: 'Unknown'}
for corporations_chunk in [list(corporations)[i:i+90] for i in range(0, len(corporations), 90)]:
get_corporation_details = swagger.op['get_corporations_names'](
corporation_ids=','.join(str(x) for x in corporations_chunk if x != 0)
)
corp_name_list = esi.request(get_corporation_details)
for corp in corp_name_list.data:
corp_array[corp.corporation_id] = corp.corporation_name
# Maximum number of alliances we can request at once is 100, get 90 to be safe
alliance_array = {0: 'Unknown'}
for alliances_chunk in [list(alliances)[i:i+90] for i in range(0, len(alliances), 90)]:
get_alliance_details = swagger.op['get_alliances_names'](
alliance_ids=','.join(str(x) for x in alliances_chunk if x != 0)
)
alliance_name_list = esi.request(get_alliance_details)
for alliance in alliance_name_list.data:
alliance_array[alliance.alliance_id] = alliance.alliance_name
km['location']['name'] = system_details.data.name
km['victim']['name'] = char_array[km['victim']['character']]
km['victim']['corpName'] = corp_array[km['victim']['corporation']]
km['victim']['allianceName'] = alliance_array[km['victim']['alliance']]
km['finalBlow']['name'] = char_array[km['finalBlow']['character']]
km['finalBlow']['corpName'] = corp_array[km['finalBlow']['corporation']]
km['finalBlow']['allianceName'] = alliance_array[km['finalBlow']['alliance']]
km['finalBlow']['shipName'] = getItemName(km['finalBlow']['ship']) if km['finalBlow']['ship'] != 0 else "Unknown"
for i in range(0, len(km['attackers'])):
km['attackers'][i]['name'] = char_array[km['attackers'][i]['character']]
km['attackers'][i]['shipName'] = getItemName(km['attackers'][i]['ship']) if km['attackers'][i]['ship'] != 0 else "Unknown"
return km
def getItemName(id):
idb = itemdb.cursor()
itemid = (str(id),)
idb.execute('SELECT typeName FROM invTypes WHERE typeID=?', itemid)
itemname = idb.fetchone()
itemname = "Unknown (%s)" % id if itemname is None else itemname[0]
idb.close()
return itemname
def sendKill(killtype, searchsection, km):
km = fluffKillmail(km)
if killtype == "expensive" or killtype == "intel":
fields = [
{
'title': "Involved Players (%s)" % len(km['attackers']),
'value': '\n'.join(["{name} ({shipName})".format(**a) for a in km['attackers']
if a[searches.get(searchsection, 'zkill_search_type')]
== int(searches.get(searchsection, 'zkill_search_id'))]),
'short': True
},
{
'title': 'Final Blow',
'value': "{name} ({shipName})".format(**km['finalBlow']),
'short': True
}
]
elif killtype == "solo":
fields = [
{
'title': 'Killer',
'value': km['finalBlow'].get('name'),
'short': True
},
{
'title': 'Using',
'value': km['finalBlow'].get('shipName'),
'short': True
}
]
elif killtype == "super":
fields = [
{
'title': 'Losing corp/alliance',
'value': "%s/%s" % (km['victim'].get('corpName'), km['victim'].get('allianceName')),
'short': True
},
{
'title': 'Killer',
'value': "%s (%s)" % (km['finalBlow'].get('name'),
km['finalBlow'].get('corpName') if km['finalBlow'].get('allianceName') == "Unknown"
else km['finalBlow'].get('allianceName')),
'short': True
},
{
'title': 'Location',
'value': km['location']['name'],
'short': True
}
]
elif killtype in ("loss_ship", "loss_expensive"):
fields = [
{
'title': 'Killer',
'value': "%s (%s)" % (km['finalBlow'].get('name'),
km['finalBlow'].get('corpName')
if km['finalBlow'].get('allianceName') == "Unknown"
else km['finalBlow'].get('allianceName')),
'short': True
},
{
'title': 'Using',
'value': km['finalBlow'].get('shipName'),
'short': True
}
]
attachment_payload = {
'fallback': 'Alert!!! %s died in a %s worth %s -- %s%s' % (
km['victim']['name'], km['victim']['shipName'], "{:,.0f}".format(km['value']),
config.get('killboard', 'kill_url'), km['id']),
'color': 'danger' if killtype != 'super' else 'warning',
'pretext': "*Solo Kill!!!*" if killtype == "solo"
else "*No scrubs... no poors...*" if killtype[:4] == "loss" else "*Intel*" if killtype == "intel" else "*Dank Frag!!!*",
'title': '%s died in a %s worth %s ISK in %s' % (km['victim']['name'], km['victim']['shipName'],
"{:,.0f}".format(km['value']), km['location']['name']),
'title_link': '%s%s' % (config.get('killboard', 'kill_url'), km['id']),
'fields': fields,
'thumb_url': '%s%s_256.png' % (config.get('killboard', 'ship_renders'), km['victim']['ship']),
'mrkdwn_in': ['pretext']
}
if killtype == "super":
attachment_payload.update({'footer': 'This is a generic super kill.'})
sc.api_call(
"chat.postMessage",
as_user="false",
username=config.get('slack', 'slack_bot_name'),
channel=searches.get(searchsection, 'channel_name'),
icon_emoji=config.get('slack', 'slack_bot_icon'),
attachments=[attachment_payload]
)
logger.info("Kill sent to slack...")
def checkConfigFiles(path):
# implement config file checks
return True
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--nodaemon", help="Do not run in daemon mode", action="store_true")
parser.add_argument("--config", help="Specify path to folder containing config files")
parser.add_argument("--forcelogfile", help="Force the bot to write to logfiles when not running in daemon mode",
action="store_true")
parser.add_argument("--debug", help="Log/show debugging messages", action="store_true")
parser.add_argument("--nologging", help="Do not log anything", action="store_true")
parser.add_argument("--logfile", help="Specify custom log file path")
args = parser.parse_args()
logger = logging.getLogger()
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
logger.setLevel(logging.DEBUG)
if args.logfile:
logfilename = os.path.abspath(args.logfile)
else:
logdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "logs")
if not os.path.exists(logdir):
os.mkdir(logdir)
logfilename = os.path.join(logdir, "dankbot.log")
logfile = logging.handlers.RotatingFileHandler(logfilename, maxBytes=10000000, backupCount=5)
logfile.setFormatter(formatter)
console = logging.StreamHandler()
console.setFormatter(formatter)
if args.debug:
logfile.setLevel(logging.DEBUG)
console.setLevel(logging.DEBUG)
else:
logfile.setLevel(logging.INFO)
console.setLevel(logging.INFO)
if args.nodaemon and not args.nologging:
logger.addHandler(console)
if not args.nologging:
if not args.nodaemon or args.forcelogfile:
logger.debug("Logging to %s" % logfilename)
logger.addHandler(logfile)
if args.config:
configpath = os.path.abspath(args.config)
logger.info("Configuration path specified: %s" % configpath)
else:
configpath = os.path.abspath(".")
logger.info("Using default config path: %s" % configpath)
if checkConfigFiles(configpath):
if args.nodaemon:
logger.info("Running bot in the terminal...")
main(configpath)
else:
logger.info("Running bot in daemon mode...")
import daemon
with daemon.DaemonContext():
main(configpath)
else:
logger.critical("Config files did not pass validation checks, exiting...")
exit()