Skip to content

Instantly share code, notes, and snippets.

@yoshimov
Last active December 3, 2015 16:32
Show Gist options
  • Save yoshimov/97f3999449c371539906 to your computer and use it in GitHub Desktop.
Save yoshimov/97f3999449c371539906 to your computer and use it in GitHub Desktop.
import os
import asyncio
import xml.etree.ElementTree as ET
import hangups
import subprocess
import datetime
import shlex
from Core.Commands.Dispatcher import DispatcherSingleton
ingress_log="/home/nomura/ingress-comm/ingress-comm.log"
ingressall_log="/home/nomura/ingress-comm/ingress-comm*.log"
ingress_faction="RESISTANCE"
log_fifo_size=10
def ingtostr(root):
str="%s: %s attacked %s %s" % (root.find('.//div[@class="pl_timestamp_date"]').text,
root.find('.//span[@data-isfaction="false"]').text,
root.find('.//span[@class="pl_portal_name"]').text,
root.find('.//span[@class="pl_portal_address"]').text)
return str
def ingtoseg(root):
time=root.find('.//div[@class="pl_timestamp_date"]').text
agent=root.find('.//span[@data-isfaction="false"]').text
portalet=root.find('.//span[@class="pl_portal_name"]')
portal=portalet.text
address=root.find('.//span[@class="pl_portal_address"]').text
plat=portalet.get('data-plat')
plng=portalet.get('data-plng')
url='https://www.ingress.com/intel?ll=%s,%s&z=18&pll=%s,%s' % (plat, plng, plat, plng)
return [hangups.ChatMessageSegment(time + ': '),
hangups.ChatMessageSegment(agent, is_bold=True),
hangups.ChatMessageSegment(' attacked '),
hangups.ChatMessageSegment(portal, hangups.SegmentType.LINK, link_target=url),
hangups.ChatMessageSegment('\n', hangups.SegmentType.LINE_BREAK),
hangups.ChatMessageSegment(address) ]
@asyncio.coroutine
def firecomm(bot):
print("firecomm invoked")
p = yield from asyncio.create_subprocess_shell("tail -F %s" % (ingress_log), stdout=asyncio.subprocess.PIPE)
log_fifo = []
while True:
mes = yield from p.stdout.readline()
if not mes:
continue
#print(mes)
mes=mes.decode('utf-8')
if not ingress_faction in mes or not "destroyed" in mes or not "Resonator" in mes:
continue
try:
root=ET.fromstring(mes)
except:
print("fail to parse message: " + mes)
continue
str=ingtostr(root)
if str in log_fifo:
continue
log_fifo.append(str)
if len(log_fifo) > log_fifo_size:
log_fifo.pop(0)
for conv in bot.list_conversations():
list = bot.get_config_suboption(conv.id_, 'ingwatch')
if list and any(key in str for key in list):
print(str)
bot.send_message_segments(conv, ingtoseg(root))
@DispatcherSingleton.register_init
def subscribecomm(bot):
asyncio.async(firecomm(bot))
@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')
@asyncio.coroutine
def send_shell_output(bot, event, key, stype, shell):
p = yield from asyncio.subprocess.create_subprocess_shell(shell, stdout=asyncio.subprocess.PIPE)
seg=[hangups.ChatMessageSegment(key, is_bold=True), hangups.ChatMessageSegment(stype)]
while not p.stdout.at_eof():
line = yield from p.stdout.readline()
if line:
item = line.decode('utf-8')
seg.append(hangups.ChatMessageSegment('\n', hangups.SegmentType.LINE_BREAK))
seg.append(hangups.ChatMessageSegment(item))
yield from p.wait()
if len(seg) > 2:
bot.send_message_segments(event.conv, seg)
else:
bot.send_message(event.conv, 'no result')
@DispatcherSingleton.register
def history(bot, event, *args):
if len(args) == 0:
bot.send_message(event.conv, 'please specify agent name.')
return
agent = shlex.quote(args[0])
num = 20
if len(args) > 1:
num = shlex.quote(args[1])
if len(args) > 2 and ("csv" in args[2] or "CSV" in args[2]):
output="echo $day $time,$portal,$lat,$lng;"
else:
output="echo $day $time $portal;"
process="""grep %s %s | egrep 'destroyed|deployed' | grep -v 'Link' | while read line; do
day=${line#*ingress-comm-}; if [ "$line" = "$day" ]; then day=""; else day=${day%%%%.log*}; fi;
time=${line#*timestamp_date};time=${time#*>};time=${time%%%%<*};
portal=${line#*portal_name};portal=${portal#*>};portal=${portal%%%%<*};
lat=${line#*data-plat=\\\"};lat=${lat%%%%\\\"*};
lng=${line#*data-plng=\\\"};lng=${lng%%%%\\\"*};
%s
done | uniq | tail -%s""" % (agent, ingressall_log, output, num)
yield from send_shell_output(bot, event, agent, " history:", process)
@DispatcherSingleton.register
def pfav(bot, event, *args):
if len(args) == 0:
bot.send_message(event.conv, 'please specify agent name.')
return
agent = shlex.quote(args[0])
num = 20
if len(args) > 1:
num = shlex.quote(args[1])
process="""grep %s %s | egrep 'destroyed|deployed' | grep -v 'Link' | while read line; do
day=${line#*ingress-comm-}; if [ "$line" = "$day" ]; then day=""; else day=${day%%%%.log*}; fi;
time=${line#*timestamp_date};time=${time#*>};time=${time%%%%<*};
portal=${line#*portal_name};portal=${portal#*>};portal=${portal%%%%<*};
lat=${line#*data-plat=\\\"};lat=${lat%%%%\\\"*};
lng=${line#*data-plng=\\\"};lng=${lng%%%%\\\"*};
echo $portal;
done | sort | uniq -c | sort -n -r | head -%s""" % (agent, ingressall_log, num)
yield from send_shell_output(bot, event, agent, " favorite portal:", process)
@DispatcherSingleton.register
def hour(bot, event, *args):
if len(args) == 0:
bot.send_message(event.conv, 'please specify agent name.')
return
agent = shlex.quote(args[0])
process="""grep %s %s | egrep 'destroyed|deployed' | grep -v 'Link' | while read line; do
day=${line#*ingress-comm-}; if [ "$line" = "$day" ]; then day=""; else day=${day%%%%.log*}; fi;
time=${line#*timestamp_date};time=${time#*>};time=${time%%%%<*};
htime=${time%%:*};
if [ "${time%%AM}" != "$time" ]; then
echo "$htime";
elif [ "${time%%PM}" != "$time" ]; then
echo "$(($htime + 12))";
fi;
done | sort -n | uniq -c | while read l; do
set -- $l; echo "${2}: $1";
done""" % (agent, ingressall_log)
yield from send_shell_output(bot, event, agent, " active hour:", process)
@DispatcherSingleton.register
def commstatus(bot, event, *args):
process="""pstatus=$(pgrep -l phantomjs);
if [ -n "$pstatus" ]; then
echo "running";
else
echo "stopped";
fi"""
yield from send_shell_output(bot, event, "", " comm process:", process)
process="""grep timestamp %s | tail -1 | while read line; do
time=${line#*timestamp_date};
if [ "$time" != "$line" ]; then
time=${time#*>};time=${time%%%%<*};
echo "$time";
fi;
done""" % (ingress_log)
yield from send_shell_output(bot, event, "", " last log:", process)
@penkoba
Copy link

penkoba commented Mar 8, 2015

同じログをミュートするのを実装してみたのですが、気が向いたら見ていただけますか?
https://gist.github.com/penkoba/7b1d2192734345f499ac
実環境での動作確認をしていないので申し訳ないのですが。

@penkoba
Copy link

penkoba commented Mar 8, 2015

あ、今スペルミスみつけて修正しました。すみません。

@yoshimov
Copy link
Author

yoshimov commented Mar 9, 2015

ありがとうございます。FIFOを取り入れて少し修正しました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment