From 3f39d013ccf33f9ddd167d25fce31c3fd6470e99 Mon Sep 17 00:00:00 2001 From: CMDR furrycat Date: Fri, 7 Oct 2016 16:35:42 +0100 Subject: [PATCH] Tidy setting announcement states. --- bot.py | 26 ++++++++++++++++++-------- db.py | 9 --------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/bot.py b/bot.py index e106363..9b0e739 100755 --- a/bot.py +++ b/bot.py @@ -1223,13 +1223,12 @@ def manage_announcements(message, command, raw): @asyncio.coroutine def announce(announcement): + update = {} # Adhere to schedule. - last_spoke = None - asap = None if announcement['asap'] == 'true': log.info('Announcement {} was requested ASAP'.format(announcement['id'])) announcement['probability'] = 1.0 - asap = 'false' + update['asap'] = 'false' elif announcement['interval']: if announcement['start_date'] or announcement['last_spoke']: now = int(time.time()) @@ -1243,11 +1242,18 @@ def announce(announcement): scheduled = start + intervals * announcement['interval'] if now - scheduled > max(60, announcement['interval'] / 2): log.info('Announcement {} should have been given at {}'.format(announcement['id'], iso8601(scheduled))) - db.set_announcement(client, announcement['id'], digest = announcement['digest'] if 'digest' in announcement else None, last_spoke = scheduled) + if 'digest' in announcement: + update['digest'] = announcement['digest'] + update['last_spoke'] = scheduled + db.update_announcement(client, announcement['id'], **update) return # Give the announcement now but set the original schedule. last_spoke = start + (intervals + 1) * announcement['interval'] - log.info('Forcing last_spoke for announcement {} to {} in line with schedule'.format(announcement['id'], iso8601(last_spoke))) + if last_spoke < now: + update['last_spoke'] = last_spoke + log.info('Forcing last_spoke for announcement {} to {} in line with schedule'.format(announcement['id'], iso8601(update['last_spoke']))) + if 'asap' not in update and 'last_spoke' not in update: + update['last_spoke'] = int(time.time()) channel = None channel_id = str(announcement['channel_id']) @@ -1265,7 +1271,7 @@ def announce(announcement): if channel is None: log.warning("Can't get channel for announcement {}".format(announcement['id'])) # Set last_spoke so we don't spam. - db.set_announcement(client, announcement['id'], digest = announcement['digest'] if 'digest' in announcement else None, last_spoke = last_spoke, asap = asap) + db.update_announcement(client, announcement['id'], **update) return voice_only = announcement['voice_id'] == announcement['channel_id'] @@ -1313,9 +1319,13 @@ def announce(announcement): message = yield from say(channel, text) digest = hashlib.sha224(message.content.encode('utf-8')).hexdigest() else: - log.info("Didn't bother with announcement {}".format(announcement['id'])) + if announcement['probability'] < 0: + log.info("Announcement {} is paused".format(announcement['id'])) + else: + log.info("Didn't bother with announcement {}".format(announcement['id'])) # Set last_spoke even if we chose not to announce. - db.set_announcement(client, announcement['id'], digest = digest, last_spoke = last_spoke, asap = asap) + update['digest'] = digest + db.update_announcement(client, announcement['id'], **update) if not announce: return if announcement['voice_id']: diff --git a/db.py b/db.py index 8b8b2aa..5226f9f 100644 --- a/db.py +++ b/db.py @@ -106,15 +106,6 @@ class DBConnection(object): yield dict(row) self.close_db() - def set_announcement(self, client, id, digest = None, last_spoke = None, asap = None): - if last_spoke is None and asap is None: - last_spoke = self.now() - if asap is None: - asap = 'asap' - cursor = self.query('update announcements set last_spoke=?, asap=?, digest=? where id=?', [last_spoke, asap, digest, id]) - self.dbh.commit() - self.close_db() - def create_announcement(self, client, **args): id = self.uuid() -- 2.7.4