0

I'm trying to filer certain words with Twython before retweeting. I can't figure out a way to get it to work and instead of filtering out certain words, it's adding those words to the ones to retweet. Here is my code:

    naughty_words = ["",'"Sign up"', "kindle", "read", "book", "amzn", "amazon"]
    good_words = ["Giveaway", ""]

    filter = "OR".join(good_words)
    blacklist = "-".join(naughty_words)
    keywords = filter + blacklist

    search_results = twitter.search(q="keywords", count= 5)
    try:
        for tweet in search_results["statuses"]:
            twitter.retweet(id = tweet["id_str"])
            time.sleep(15)

    except TwythonError as e:
            print e
Alex
  • 488
  • 2
  • 5
  • 20

1 Answers1

0

Two issues that I see, fix those and see if it fixes your problem.

1) keywords isn't functioning as expected. From your code now I get GiveawaySign up -kindle -read -book -amzn -amazon. This is because good_words is a 1 element list, so the .join isn't working as expected.

2) The way "Sign Up" is done will show up as "Sign" AND "up" that's more likely the problem.

Try the following:

naughty_words = ["",'"Sign up"', "kindle", "read", "book", "amzn", "amazon"]
good_words = ["Giveaway", ""]

Also, remove the space after OR and keep the one before.

Edit

Change your filter and blacklist to:

filter = "".join(good_words)
blacklist = " -".join(naughty_words)

Since you only have one word in good_words there's not need for the OR. You should get:

Giveaway -"Sign up" -kindle -read -book -amzn -amazon

Leb
  • 15,483
  • 10
  • 56
  • 75
  • That didn't fix anything at all. I still have the same problem :'(. – Alex Sep 28 '15 at 01:15
  • `Giveaway OR -"Sign up" -kindle -read -book -amzn -amazon` is from the one I provided. `GiveawaySign up -kindle -read -book -amzn -amazon` is from your printout of `keywords`. The output is quite different at the beginning to comply with [Search API](https://dev.twitter.com/rest/public/search) make sure it's update correctly. I ran it and worked fine for me. – Leb Sep 28 '15 at 02:34
  • Where do I put that stuff? I updated the question above to comply with my changes. Could you tell me where I should change things specifically? (I'm new to this stuff") – Alex Sep 28 '15 at 15:14