misc: Fix logging and add carbons

This commit is contained in:
PapaTutuWawa 2020-09-17 17:47:08 +02:00
parent 50983cae44
commit e31b0ebf8a

View File

@ -18,7 +18,8 @@ from janine.utils import find_one, find_all
from janine.sources import sources_from_config, MiscDataSources from janine.sources import sources_from_config, MiscDataSources
log = logging.getLogger('janine') log = logging.getLogger('janine')
#log.basicConfig(level=logging.INFO) log.setLevel(logging.INFO)
log.addHandler(logging.StreamHandler())
Warning = namedtuple('Warning', ['id', Warning = namedtuple('Warning', ['id',
'sent', 'sent',
@ -31,6 +32,23 @@ Warning = namedtuple('Warning', ['id',
'instruction', 'instruction',
'landkreise']) 'landkreise'])
def stub_warning(id_):
'''
Returns a stubbed warning for loading warnings from disk.
The only real attribute is the @id_ .
'''
return Warning(id=id_,
sent='',
effective_from='',
expires='',
urgency='',
sender='',
headline='',
description='',
instruction='',
landkreise=[])
def to_warning(data): def to_warning(data):
''' '''
Returns a Warning given the raw data Returns a Warning given the raw data
@ -91,7 +109,7 @@ class WarningBot:
aioxmpp.make_security_layer(self._password)) aioxmpp.make_security_layer(self._password))
async with self._client.connected() as stream: async with self._client.connected() as stream:
logging.info('Client connected to server') log.info('Client connected to server')
# In case you want a nice avatar # In case you want a nice avatar
if self._avatar: if self._avatar:
@ -101,20 +119,25 @@ class WarningBot:
avatar_set = aioxmpp.avatar.AvatarSet() avatar_set = aioxmpp.avatar.AvatarSet()
avatar_set.add_avatar_image('image/png', image_bytes=image_data) avatar_set.add_avatar_image('image/png', image_bytes=image_data)
await selg.avatar.publish_avatar_set(avatar_set) await (self._client.summon(aioxmpp.avatar.AvatarService)
.publish_avatar_set(avatar_set))
# Set some presence information # Set some presence information
self._client.set_presence( self._client.set_presence(
aioxmpp.PresenceState(available=True, aioxmpp.PresenceState(available=True,
show=PresenceShow.DND), show=PresenceShow.CHAT),
self._status) self._status)
# Enable Carbons
await self._client.summon(aioxmpp.CarbonsClient).enable()
log.info('Message carbons enabled')
# Register the message handler # Register the message handler
self._client.stream.register_message_callback( self._client.stream.register_message_callback(
aioxmpp.MessageType.CHAT, aioxmpp.MessageType.CHAT,
None, None,
self._handle_message) self._handle_message)
logging.info('Message handler registered') log.info('Message handler registered')
# Start our fetch-send loop # Start our fetch-send loop
# NOTE: Originally, I wanted to use a cronjob and # NOTE: Originally, I wanted to use a cronjob and
@ -122,7 +145,7 @@ class WarningBot:
# use async in event handlers # use async in event handlers
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
periodic = loop.create_task(self._periodic_requests()) periodic = loop.create_task(self._periodic_requests())
logging.info('Periodic ticker started') log.info('Periodic ticker started')
await periodic await periodic
def __is_message_valid(self, msg): def __is_message_valid(self, msg):
@ -240,8 +263,14 @@ class WarningBot:
any new ones. any new ones.
''' '''
while True: while True:
logging.debug('Refreshing warning list') log.debug('Refreshing warning list')
await self._fetch_warnings() await self._fetch_warnings()
# Flush the warnings to disk
ids = list(map(lambda x: x.id, self._warnings0))
with open(self._warnings_file, 'w') as wf:
wf.write(json.dumps(ids))
await asyncio.sleep(self._refresh_timeout) await asyncio.sleep(self._refresh_timeout)
async def _fetch_warnings(self): async def _fetch_warnings(self):
@ -324,13 +353,20 @@ class WarningBot:
if os.path.exists(self._client_store): if os.path.exists(self._client_store):
with open(self._client_store, 'r') as cf: with open(self._client_store, 'r') as cf:
self._warn_clients = json.loads(cf.read()) self._warn_clients = json.loads(cf.read())
log.info('Clients read from disk')
self._warnings_file = os.path.join(self._data_dir, 'warnings.json')
if os.path.exists(self._warnings_file):
with open(self._warnings_file, 'r') as wf:
self._warnings0 = list(map(stub_warning, json.loads(wf.read())))
log.info('Warnings read from disk')
# Landkreise # Landkreise
self._channels = [] self._channels = []
channels = {} channels = {}
channel_file = os.path.join(self._data_dir, 'channels.json') channel_file = os.path.join(self._data_dir, 'channels.json')
if not os.path.exists(channel_file): if not os.path.exists(channel_file):
log.debug('Requesting search channels') log.info('Requesting search channels')
req = requests.get(MiscDataSources.channels()) req = requests.get(MiscDataSources.channels())
channels = json.loads(req.text) channels = json.loads(req.text)
self._channels = list(map(lambda key: channels[key].get('NAME', ''), self._channels = list(map(lambda key: channels[key].get('NAME', ''),
@ -345,6 +381,7 @@ class WarningBot:
else: else:
with open(channel_file, 'r') as f: with open(channel_file, 'r') as f:
self._channels = json.loads(f.read()) self._channels = json.loads(f.read())
log.info('Search channels read from disk')
# Bot Config # Bot Config
self._jid = aioxmpp.JID.fromstr(config['Bot']['JID']) self._jid = aioxmpp.JID.fromstr(config['Bot']['JID'])