-
-
Save penkoba/7b1d2192734345f499ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import asyncio | |
import xml.etree.ElementTree as ET | |
from Core.Commands.Dispatcher import DispatcherSingleton | |
ingress_log="/home/nomura/ingress-comm/ingress-comm.log" | |
ingress_faction="RESISTANCE" | |
log_dict_size=10 | |
#@asyncio.coroutine | |
def firecomm(bot, file): | |
print("firecomm invoked") | |
log_dict = {} | |
log_fifo = [''] * log_dict_size | |
log_fifo_idx = 0 | |
while True: | |
yield from asyncio.sleep(1) | |
mes = file.readline() | |
if mes: | |
# print(mes) | |
if not ingress_faction in mes or not "destroyed" in mes: | |
continue | |
for conv in bot.list_conversations(): | |
list = bot.get_config_suboption(conv.id_, 'ingwatch') | |
if not list: | |
continue | |
flag=False | |
for key in list: | |
if key in mes: | |
flag=True | |
if flag: | |
print(mes) | |
root=ET.fromstring(mes) | |
str="%s: %s attacked %s %s" % (root.find('./div[1]/div[1]/div[1]').text, | |
root.find('./div[2]/span[1]').text, | |
root.find('./div[2]/span[2]').text, root.find('./div[2]/span[3]').text) | |
if str in log_dict: | |
continue # mute duplicated log | |
log_dict[str] = True # register to the dict | |
# limit the dict contents up to the log_dict_size | |
if log_fifo[log_fifo_idx] != '': | |
del log_dict[log_fifo[log_fifo_idx]] | |
log_fifo[log_fifo_idx] = str | |
log_fifo_idx = (log_fifo_idx + 1) % log_dict_size | |
bot.send_message(conv, str) | |
@DispatcherSingleton.register_init | |
def subscribecomm(bot): | |
file = open(ingress_log, 'r') | |
while file.readline(): | |
pass | |
asyncio.async(firecomm(bot, file)) | |
@DispatcherSingleton.register | |
def watch(bot, event, *args): | |
list = bot.get_config_suboption(event.conv_id, 'ingwatch') | |
if not list: | |
list = [] | |
list.append(args[0]) | |
bot.config['conversations'][event.conv_id]['ingwatch'] = list | |
bot.config.save() | |
bot.send_message(event.conv, 'now watching ' + ' '.join(list)) | |
@DispatcherSingleton.register | |
def watchlist(bot, event, *args): | |
list = bot.get_config_suboption(event.conv_id, 'ingwatch') | |
if list: | |
bot.send_message(event.conv, 'watching ' + ' '.join(list)) | |
else: | |
bot.send_message(event.conv, 'no watch list') | |
@DispatcherSingleton.register | |
def unwatch(bot, event, *args): | |
list = bot.get_config_suboption(event.conv_id, 'ingwatch') | |
if list: | |
list.remove(args[0]) | |
bot.config['conversations'][event.conv_id]['ingwatch'] = list | |
bot.config.save() | |
if list: | |
bot.send_message(event.conv, 'now watching ' + ' '.join(list)) | |
else: | |
bot.send_message(event.conv, 'no watch list') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment