Allow hiding link in news feeds.
authorCMDR furrycat <elite@furrycat.net>
Fri, 25 Nov 2016 15:05:05 +0000 (15:05 +0000)
committerCMDR furrycat <elite@furrycat.net>
Fri, 25 Nov 2016 15:05:05 +0000 (15:05 +0000)
plugin/feeds/feeds.py

index 8aa6343..c8ee847 100644 (file)
@@ -25,7 +25,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), url varchar(128) not null, date boolean not null default true, summary boolean not null default false, link_json varchar(256), permissions boolean not null default true, enabled boolean not null default true)')
+    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), 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, enabled boolean not null default true)')
     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()
@@ -35,7 +35,7 @@ class Feeds(DBConnection):
 
   def get_feeds(self, client):
     now = self.now()
-    cursor = self.query("select id, server_id, channel_id, url, date, summary, link_json, permissions, enabled from feeds where bot_id=?", [client.user.id])
+    cursor = self.query("select id, server_id, channel_id, url, date, summary, show_link, link_json, permissions, enabled from feeds where bot_id=?", [client.user.id])
     for row in cursor.fetchall():
       yield dict(row)
     self.close_db()
@@ -56,7 +56,8 @@ class Feeds(DBConnection):
   def feed_key(self, k):
     d = {
       'channel_id': 'in',
-      'description': 'title'
+      'description': 'title',
+      'show_link': 'link'
     }
     if k in d:
       return d[k]
@@ -106,6 +107,8 @@ class Feeds(DBConnection):
       date = bot.parse_boolean(feed['date']) if 'date' in feed else True
       # Include summary text.
       summary = bot.parse_boolean(feed['summary']) if 'summary' in feed else False
+      # Post link
+      show_link = bot.parse_boolean(feed['show_link']) if 'show_link' in feed else True
       url = feed['url']
       d = feedparser.parse(url)
       for entry in d.entries:
@@ -114,7 +117,7 @@ class Feeds(DBConnection):
         else:
           order = now
           now += 0.001
-        all_entries.append({ 'order': order, 'date': date, 'summary': summary, 'url': feed['url'], 'link_json': feed['link_json'], 'entry': entry })
+        all_entries.append({ 'order': order, 'date': date, 'summary': summary, '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))
       return
@@ -136,7 +139,9 @@ class Feeds(DBConnection):
       entry = feed['entry']
       formatted = '*{}*\n'.format(entry['published']) if feed['date'] else ''
       link = None
-      if 'links' in entry:
+      if not feed['show_link']:
+        log.debug('Entry {} link is hidden.'.format(feed['url']))
+      elif 'links' in entry:
         link = entry['links'][0]['href']
       elif 'link_json' in feed:
         try:
@@ -147,10 +152,10 @@ class Feeds(DBConnection):
           log.error('Invalid link_json in feed {}'.format(feed['url']))
         except:
           log.exception('do_rss:link_json')
-      if not link:
+      if feed['show_link'] and not link:
         log.warning('No link for entry {}'.format(entry['title']))
         continue
-      formatted += '**{}**\n{}'.format(entry['title'], link)
+      formatted += '**{}**{}'.format(entry['title'], '\n{}'.format(link) if feed['show_link'] else '')
       if feed['summary']:
         formatted += '\n{}'.format(''.join(bs4.BeautifulSoup(entry['summary'], 'html.parser').findAll(text = True)))
       formatted = formatted.strip()
@@ -305,6 +310,7 @@ class Feeds(DBConnection):
     text += ' in <#{}>'.format(feed['channel_id'])
     text += ' date {}'.format(feed['date'])
     text += ' summary {}'.format(feed['summary'])
+    text += ' link {}'.format(feed['show_link'])
     text += ' permissions {}'.format(feed['permissions'])
     if feed['link_json']:
       text += "--link_json '{}'".format(feed['link_json'])
@@ -372,8 +378,11 @@ class Feeds(DBConnection):
           ok = True
         else:
           break
-      elif arg in ['date', 'permissions', 'summary']:
-        k = arg
+      elif arg in ['date', 'permissions', 'link', 'summary']:
+        if arg == 'link':
+          k = 'show_link'
+        else:
+          k = arg
         if param in ['true', 'false']:
           parsed[k] = param
           ok = True
@@ -393,9 +402,6 @@ class Feeds(DBConnection):
       yield from bot.say(message.channel, '{}?'.format(arg))
       return None
 
-    if 'permissions' not in parsed:
-      parsed['permissions'] = True
-
     if 'channel_id' in parsed:
       channel = client.get_channel(parsed['channel_id'])
       if not channel:
@@ -418,6 +424,10 @@ class Feeds(DBConnection):
       if 'description' not in parsed:
         yield from bot.say(message.channel, 'Missing title!')
         return None
+      if 'show_link' not in parsed:
+        parsed['show_link'] = True
+      if 'permissions' not in parsed:
+        parsed['permissions'] = True
 
     return parsed
 
@@ -544,6 +554,9 @@ class Feeds(DBConnection):
         '```permissions false```',
         "Don't override the `CHANNEL` permissions to prevent others from posting there.",
         '',
+        '```link false```',
+        "Don't post a link to the entry.",
+        '',
       ]
     elif command == 'delete':
       lines = [