Search code examples
python-2.7rssbotsfeedslack

slack bot news feeder not banning words


I'm having trouble getting my slack bots news feeder to ban words, the trigger words are working fine, but its letting banned words through but it catches them because it prints out found banned word: xxxxx. I'm new to python and I just don't get what's up.

triggers = ['SEC', 'CSA', 'OSC', 'CFTC', 'CME', 'CBOE', 'AMD', 'Intel', 'Nvidia',
            'Bitcoin', 'blockchain', 'Apple', 'Amazon', 'Google', 'Microsoft',
            'commerce', 'business', 'law', 'legal', 'financial', 'hack', 'hacked',
            'chains', 'chairman', 'CEO', 'board', 'bank']

banned = ['technical', 'analysis', 'bearish', 'bullish', 'trading', 'trade', 'opinion', 
          'sponsored', 'price', 'watch']

def feedparsecheck(url):
    feed = feedparser.parse(url)
    feed_title = feed['feed']['title']
    feed_entries = feed.entries
    database()
    print "feed 30 min"

    for entry in feed.entries:

        article_title = entry.title
        article_link = entry.link

        for trig in triggers:
            if trig.lower() in article_title.lower():#trigger
                for ban in banned:
                    if ban.lower() not in article_title.lower():#banned
                        response = "%s\n%s\n" % (article_title, article_link)
                        article_link = str(article_link.strip())

                        if not in_database(article_link):
                            update_database(article_link)
                            #print article_link
                            slack_client.api_call("chat.postMessage", channel=NEWS, text=''.join(response), as_user=True)
                    else:
                        print "found banned word:- " + ban

Solution

  • You are iterating over all triggers and all banned words for all articles, which means that every article will get sent to your channel for every banned word that's not in the title * number of triggers in the title.

    Example:

    Bitcoin trading for lower price after hack Will get sent to your channel 16 times. 2 triggers (Bitcoin, hack) * 8 banned words not in title (10 - len(trading, price)) = 16.

    To fix:

    title_lower = article_title.lower()
    if any(trig.lower() in title_lower for trig in triggers):
        if any(ban.lower() in title_lower for ban in banned):
            print 'found banned word:- '+ ban
        else:
            # post to slack channel