Option to omit date line from news.
authorCMDR furrycat <elite@furrycat.net>
Tue, 6 Dec 2016 10:21:56 +0000 (10:21 +0000)
committerCMDR furrycat <elite@furrycat.net>
Tue, 6 Dec 2016 10:21:56 +0000 (10:21 +0000)
plugin/feeds/feeds.py

index db70248..19c36db 100644 (file)
@@ -26,7 +26,7 @@ class Feeds(DBConnection):
 
   def create_tables(self):
     self.open_db()
-    cursor = self.query('create table if not exists feeds (id char(36) not null, bot_id varchar(32) not null, server_id varchar(32) not null, channel_id varchar(32) not null, description varchar(64), mention varchar(32), url varchar(128) not null, date boolean not null default true, summary boolean not null default false, show_link boolean not null default true, link_json varchar(256), permissions boolean not null default true, interval int, enabled boolean not null default true, last_spoke datetime)')
+    cursor = self.query('create table if not exists feeds (id char(36) not null, bot_id varchar(32) not null, server_id varchar(32) not null, channel_id varchar(32) not null, description varchar(64), mention varchar(32), url varchar(128) not null, date boolean not null default true, summary boolean not null default false, show_date boolean not null default true, show_link boolean not null default true, link_json varchar(256), permissions boolean not null default true, interval int, enabled boolean not null default true, last_spoke datetime)')
     cursor = self.query('create unique index if not exists feed_id on feeds (id)')
     cursor = self.query('create index if not exists feed_bot_id on feeds (bot_id)')
     self.dbh.commit()
@@ -36,7 +36,7 @@ class Feeds(DBConnection):
 
   def get_feeds(self, client):
     now = self.now()
-    cursor = self.query("select id, server_id, channel_id, mention, url, date, summary, show_link, link_json, permissions, interval, enabled, last_spoke from feeds where bot_id=? and interval is null or (last_spoke is null or last_spoke < ? - interval)", [client.user.id, now])
+    cursor = self.query("select id, server_id, channel_id, mention, url, date, summary, show_date, show_link, link_json, permissions, interval, enabled, last_spoke from feeds where bot_id=? and interval is null or (last_spoke is null or last_spoke < ? - interval)", [client.user.id, now])
     for row in cursor.fetchall():
       yield dict(row)
     self.close_db()
@@ -60,6 +60,7 @@ class Feeds(DBConnection):
       'description': 'title',
       'interval': 'every',
       'mention': 'tell',
+      'show_date': 'dateline',
       'show_link': 'link'
     }
     if k in d:
@@ -109,6 +110,11 @@ class Feeds(DBConnection):
     for feed in feeds:
       # Date can be trusted.
       date = bot.parse_boolean(feed['date']) if 'date' in feed else True
+      # Include date.
+      if date:
+        show_date = bot.parse_boolean(feed['show_date']) if 'show_date' in feed else True
+      else:
+        show_date = False
       # Include summary text.
       summary = bot.parse_boolean(feed['summary']) if 'summary' in feed else False
       # Post link
@@ -142,7 +148,7 @@ class Feeds(DBConnection):
         else:
           order = now
           now += 0.001
-        all_entries.append({ 'mention': feed['mention'], 'order': order, 'date': date, 'summary': summary, 'show_link': show_link, 'url': feed['url'], 'link_json': feed['link_json'], 'entry': entry })
+        all_entries.append({ 'mention': feed['mention'], 'order': order, 'date': date, 'summary': summary, 'show_date': show_date, 'show_link': show_link, 'url': feed['url'], 'link_json': feed['link_json'], 'entry': entry })
     if not len(all_entries):
       log.debug('No messages to post to {}'.format(channel.name))
     else:
@@ -160,7 +166,9 @@ class Feeds(DBConnection):
 
       for feed in all_entries:
         entry = feed['entry']
-        formatted = '*{}*\n'.format(entry['published']) if feed['date'] else ''
+        formatted = ''
+        if feed['show_date']:
+          formatted += '*{}*\n'.format(entry['published'])
         link = None
         if not feed['show_link']:
           log.debug('Entry {} link is hidden.'.format(feed['url']))
@@ -178,9 +186,11 @@ class Feeds(DBConnection):
         if feed['show_link'] and not link:
           log.warning('No link for entry {}'.format(entry['title']))
           continue
-        formatted += '**{}**{}'.format(entry['title'], '\n{}'.format(link) if feed['show_link'] else '')
         if feed['mention']:
           formatted += '{}\n'.format(bot.format_mention(feed['mention']))
+        formatted += '**{}**'.format(entry['title'])
+        if feed['show_link']:
+          formatted += '\n{}'.format(link)
         if feed['summary']:
           formatted += '\n{}'.format(''.join(bs4.BeautifulSoup(entry['summary'], 'html.parser').findAll(text = True)))
         formatted = formatted.strip()
@@ -347,12 +357,14 @@ class Feeds(DBConnection):
         # Can't mention a role in a private channel.
         text += ' tell @{}'.format(feed['mention'])
       else:
-        text += ' tell <@{}>'.format(feed['mention'])
+        text += ' tell {}'.format(bot.format_mention(feed['mention']))
     text += ' in <#{}>'.format(feed['channel_id'])
     if feed['interval']:
       text += ' every {}'.format(bot.unparse_seconds(int(feed['interval'])))
     if not bot.parse_boolean(feed['date']):
       text += ' date {}'.format(feed['date'])
+      if not bot.parse_boolean(feed['show_date']):
+        text += ' dateline {}'.format(feed['show_date'])
     if bot.parse_boolean(feed['summary']):
       text += ' summary {}'.format(feed['summary'])
     if not bot.parse_boolean(feed['show_link']):
@@ -447,9 +459,11 @@ class Feeds(DBConnection):
           ok = True
         else:
           break
-      elif arg in ['date', 'permissions', 'link', 'summary']:
+      elif arg in ['date', 'dateline', 'permissions', 'link', 'summary']:
         if arg == 'link':
           k = 'show_link'
+        elif arg == 'dateline':
+          k  ='show_date'
         else:
           k = arg
         if param in ['true', 'false']:
@@ -493,6 +507,8 @@ class Feeds(DBConnection):
       if 'description' not in parsed:
         yield from bot.say(message.channel, 'Missing title!')
         return None
+      if 'show_date' not in parsed:
+        parsed['show_date'] = True
       if 'show_link' not in parsed:
         parsed['show_link'] = True
       if 'permissions' not in parsed:
@@ -636,6 +652,9 @@ class Feeds(DBConnection):
         '```date false```',
         "Use this if the dates returned by the RSS URL aren't valid.  For instance the official GalNet feed always returns the date its cache was updated NOT the date the story was posted.",
         '',
+        '```dateline false```',
+        'Use this to hide the date heading in the post.  The date is shown by default but will never be shown if `date false` is used.',
+        '',
         '```summary true```',
         'Include part of the text from the feed in the post.',
         '',