Skip to content

Commit

Permalink
Fixed bug in which it would respond to tweets of itself.
Browse files Browse the repository at this point in the history
  • Loading branch information
brizandrew committed Feb 21, 2018
1 parent 86081fc commit 6531b93
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
20 changes: 13 additions & 7 deletions isthistruebot/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,33 @@
class IsThisTrueBotTest(unittest.TestCase):
def test_fact_check(self):
message = handle_incoming_tweet("963350466968121344")
should_be = "I found this on @PolitiFact. I think it might help. They rated the following as \
\"Pants on Fire!\". #FactCheck http://www.politifact.com/punditfact/statements/2018/feb/15/politico-news/\
no-four-million-democrat-votes-were-not-declared-f/"
should_be = "I found this on @PolitiFact. I think it might help. They rated the following as "
should_be += "\"Pants on Fire!\". #FactCheck http://www.politifact.com/punditfact/statements/2018/feb/"
should_be += "15/politico-news/no-four-million-democrat-votes-were-not-declared-f/"
self.assertEqual(message, should_be)

def test_no_fact_check_available(self):
message = handle_incoming_tweet("965984263341473793")
should_be = "Sorry. I couldn't find anything in any of my sources. I'm constantly learning though, so try \
again another time! \u1F605 #JustBotProbz"
should_be = "Sorry. I couldn't find anything in any of my sources. I'm constantly learning though, so try "
should_be += "again another time! " + u"\U0001f605" + " #JustBotProbz"
self.assertEqual(message, should_be)

def test_no_link_in_tweet(self):
message = handle_incoming_tweet("956535969469124611")
should_be = "Hmm. I can't seem to find a link. \u1F914 Are you sure you're replying to a Tweet with a link?"
should_be = "Hmm. I can't seem to find a link. "
should_be += u"\U0001F914" + " Are you sure you're replying to a Tweet with a link?"
self.assertEqual(message, should_be)

def test_not_response_tweet(self):
message = handle_incoming_tweet("964944917222092800")
should_be = "Hmm. I can't seem to find a link. \u1F914 Are you sure you're replying to a Tweet with a link?"
should_be = "Hmm. I can't seem to find a link. "
should_be += u"\U0001F914" + " Are you sure you're replying to a Tweet with a link?"
self.assertEqual(message, should_be)

def test_no_headline(self):
headline = get_headline("http://andrewbriz.com/file-transfer/blank.html")
self.assertEqual(headline, None)

def test_tweet_to_itself(self):
message = handle_incoming_tweet("966395681953931264")
self.assertEqual(message, "")
22 changes: 15 additions & 7 deletions isthistruebot/tweeter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ def get_tweet_response_link(tweet_id):
try:
top_level_tweet = twitter.get_status(top_level_id, tweet_mode='extended')
except TweepError:
return None
return

if top_level_tweet.user.screen_name == "IsThisTrueBot":
return ""

try:
url = top_level_tweet.entities["urls"][0]["expanded_url"]
except IndexError:
return None
return

twitter_regex = re.compile('^https?://twitter\.com')
if twitter_regex.match(url):
return None
return
else:
return url

Expand All @@ -47,12 +50,12 @@ def genererate_response(answer=0):
"""
# Error Text
if answer == 0:
text = "Hmm. I can't seem to find a link." + u"\U0001F914"
text = "Hmm. I can't seem to find a link. " + u"\U0001F914"
text += " Are you sure you're replying to a Tweet with a link?"
# Failure Text
elif answer is None:
text = "Sorry. I couldn't find anything in any of my sources."
text += " I'm constantly learning though, so try again another time! " + u"\U0001F605 " + "#JustBotProbz"
text += " I'm constantly learning though, so try again another time! " + u"\U0001F605" + " #JustBotProbz"
# Success Text
else:
# text = "I found this on" + answer["source"] + ". I think it might help."
Expand All @@ -77,6 +80,9 @@ def handle_incoming_tweet(tweet_id):
if article is None:
return genererate_response()

if article == "":
return ""

headline = get_headline(article)
pages = search_pf(headline)
answer = get_best_answer(pages)
Expand Down Expand Up @@ -106,5 +112,7 @@ def on_status(self, status):
Responds to the original Tweeter with the response.
"""
message = handle_incoming_tweet(status.id)
user = status.user.screen_name
tweet_reply(message, status.id, user)

if message != "":
user = status.user.screen_name
tweet_reply(message, status.id, user)

0 comments on commit 6531b93

Please sign in to comment.