diff --git a/janine/janine.py b/janine/janine.py index 7c46224..840486e 100644 --- a/janine/janine.py +++ b/janine/janine.py @@ -18,7 +18,8 @@ from janine.utils import find_one, find_all from janine.sources import sources_from_config, MiscDataSources log = logging.getLogger('janine') -#log.basicConfig(level=logging.INFO) +log.setLevel(logging.INFO) +log.addHandler(logging.StreamHandler()) Warning = namedtuple('Warning', ['id', 'sent', @@ -31,6 +32,23 @@ Warning = namedtuple('Warning', ['id', 'instruction', '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): ''' Returns a Warning given the raw data @@ -91,7 +109,7 @@ class WarningBot: aioxmpp.make_security_layer(self._password)) 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 if self._avatar: @@ -101,20 +119,25 @@ class WarningBot: avatar_set = aioxmpp.avatar.AvatarSet() 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 self._client.set_presence( aioxmpp.PresenceState(available=True, - show=PresenceShow.DND), + show=PresenceShow.CHAT), self._status) - + + # Enable Carbons + await self._client.summon(aioxmpp.CarbonsClient).enable() + log.info('Message carbons enabled') + # Register the message handler self._client.stream.register_message_callback( aioxmpp.MessageType.CHAT, None, self._handle_message) - logging.info('Message handler registered') + log.info('Message handler registered') # Start our fetch-send loop # NOTE: Originally, I wanted to use a cronjob and @@ -122,7 +145,7 @@ class WarningBot: # use async in event handlers loop = asyncio.get_event_loop() periodic = loop.create_task(self._periodic_requests()) - logging.info('Periodic ticker started') + log.info('Periodic ticker started') await periodic def __is_message_valid(self, msg): @@ -240,8 +263,14 @@ class WarningBot: any new ones. ''' while True: - logging.debug('Refreshing warning list') + log.debug('Refreshing warning list') 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) async def _fetch_warnings(self): @@ -324,13 +353,20 @@ class WarningBot: if os.path.exists(self._client_store): with open(self._client_store, 'r') as cf: 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 self._channels = [] channels = {} channel_file = os.path.join(self._data_dir, 'channels.json') if not os.path.exists(channel_file): - log.debug('Requesting search channels') + log.info('Requesting search channels') req = requests.get(MiscDataSources.channels()) channels = json.loads(req.text) self._channels = list(map(lambda key: channels[key].get('NAME', ''), @@ -345,6 +381,7 @@ class WarningBot: else: with open(channel_file, 'r') as f: self._channels = json.loads(f.read()) + log.info('Search channels read from disk') # Bot Config self._jid = aioxmpp.JID.fromstr(config['Bot']['JID'])