From fc4ef07418003dce43155794b94c63c0ff3b1921 Mon Sep 17 00:00:00 2001 From: Yutsa Date: Fri, 10 Jul 2015 16:44:49 +0200 Subject: [PATCH 1/3] Added 'reply' method. --- TwitterFollowBot/__init__.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/TwitterFollowBot/__init__.py b/TwitterFollowBot/__init__.py index e227d70..8902c90 100644 --- a/TwitterFollowBot/__init__.py +++ b/TwitterFollowBot/__init__.py @@ -451,3 +451,17 @@ def send_tweet(self, message): """ return self.TWITTER_CONNECTION.statuses.update(status=message) + + def reply(self, keyword, message, count): + """ + Reply to people who tweeted a message with the keyword given. + Count is the number of people to tweet to. + """ + + result = self.search_tweets('#shopping', 2) + for tweets in result["statuses"]: + id = tweets["id_str"] + user = tweets["user"]["screen_name"] + final_message = "@" + user + " " + message + print(final_message) + self.TWITTER_CONNECTION.statuses.update(status=final_message, in_reply_to_status_id=id) From 3967922ffc8a0ef412dab7e361f06dd419fbb75c Mon Sep 17 00:00:00 2001 From: Yutsa Date: Fri, 10 Jul 2015 16:50:38 +0200 Subject: [PATCH 2/3] Added 'reply' method. --- TwitterFollowBot/__init__.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/TwitterFollowBot/__init__.py b/TwitterFollowBot/__init__.py index 8902c90..d96b19c 100644 --- a/TwitterFollowBot/__init__.py +++ b/TwitterFollowBot/__init__.py @@ -452,16 +452,15 @@ def send_tweet(self, message): return self.TWITTER_CONNECTION.statuses.update(status=message) - def reply(self, keyword, message, count): + def reply(self, keyword, message, count): """ Reply to people who tweeted a message with the keyword given. Count is the number of people to tweet to. """ - result = self.search_tweets('#shopping', 2) + result = self.search_tweets(keyword, 2) for tweets in result["statuses"]: id = tweets["id_str"] user = tweets["user"]["screen_name"] final_message = "@" + user + " " + message - print(final_message) self.TWITTER_CONNECTION.statuses.update(status=final_message, in_reply_to_status_id=id) From fa8bed6161f81d8c1c8434ba4af8665355a770d4 Mon Sep 17 00:00:00 2001 From: Yutsa Date: Mon, 20 Jul 2015 21:41:19 +0200 Subject: [PATCH 3/3] Reply method updated. --- TwitterFollowBot/__init__.py | 105 ++++++++++++++++++++++++++++++++--- messages.txt | 1 + 2 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 messages.txt diff --git a/TwitterFollowBot/__init__.py b/TwitterFollowBot/__init__.py index d96b19c..f531d49 100644 --- a/TwitterFollowBot/__init__.py +++ b/TwitterFollowBot/__init__.py @@ -44,6 +44,27 @@ def __init__(self, config_file="config.txt"): # Used for random timers random.seed() + self.tweet_count = 0 + # Opens the file with all the tweet's id. Loads the ids in a list. + # Handles exception if the file's not there and creates it. + try: + # If the file is younger than a day we use it + if time.time() - os.path.getmtime('id_tweets.txt') < 86400: + with open('id_tweets.txt', 'r') as list_id: + self.var_list_id = list_id.read().splitlines() + # If the file is too old, we erase its content and var_list_id will be an empty list. + else: + print('File too old.') + open('id_tweets.txt', 'w').close() + self.var_list_id = list() + + except FileNotFoundError: + # If the file is not there, we create it. + with open('id_tweets.txt', 'w') as list_id: + self.var_list_id = list() + list_id.close() + print('Creating the id_tweets.txt file...') + def wait_on_action(self): min_time = 0 max_time = 0 @@ -452,15 +473,81 @@ def send_tweet(self, message): return self.TWITTER_CONNECTION.statuses.update(status=message) - def reply(self, keyword, message, count): + def reply(self, keyword, delay=30, message_file="messages.txt", count=50): """ Reply to people who tweeted a message with the keyword given. Count is the number of people to tweet to. - """ - - result = self.search_tweets(keyword, 2) - for tweets in result["statuses"]: - id = tweets["id_str"] - user = tweets["user"]["screen_name"] - final_message = "@" + user + " " + message - self.TWITTER_CONNECTION.statuses.update(status=final_message, in_reply_to_status_id=id) + Can't reply to the same tweet several times. + Delay is the time between each tweets. + Handles several keywords + """ + text = '' + # Gets the tweets + result = self.search_tweets(keyword, count) + list_keyword = list() + # If there are several keywords use only the twitter search. + if " " in keyword: + count = 0 + list_keyword = keyword.split(" ") + for tweets in result["statuses"]: + # We count the keywords in the tweet text. + for words in list_keyword: + if words.lower() in tweets["text"].lower(): + count += 1 + #If all the keywords are in the text we reply. + if count == len(list_keyword): + self.send_reply(tweets, message_file, delay) + else: + print("No tweets found for the keywords given...\n") + count = 0 + # If there is only one keyword check if it's found in the text and not in the username. + else: + try: + for tweets in result["statuses"]: + if keyword.lower() in tweets["text"].lower(): + self.send_reply(tweets, message_file, delay) + except TwitterHTTPError as e: + print("You may have been detected as a spammer. The program will now exit.\n") + print(e) + exit() + pass + except: + print('Error getting the tweets') + # Add the tweets' id to the file if we already replied. Happens when we replied to all tweets. + with open('id_tweets.txt', 'w') as list_id: + for id in self.var_list_id: + text += id + "\n" + list_id.write(text) + + def send_reply(self, tweets, message_file, delay=30): + text = '' + id = tweets["id_str"] + # We check if we already replied to the tweet before. If not we reply. + if id not in self.var_list_id: + # Open the file with the messages to send. + try: + with open(message_file, 'r') as file_messages: + # Put the messages in a list. + list_messages = file_messages.read().splitlines() + # Generate a random number to use a random message from the list. + rand = random.randrange(0, len(list_messages)) + # Gets the username of the person who tweeted. + user = tweets["user"]["screen_name"] + # Create the message with the @username to reply + final_message = "@" + user + " " + list_messages[rand] + # Delay between each tweets + if delay < 30: + delay = 30 + time.sleep(random.randrange(delay - delay/2, delay + delay/2)) + self.TWITTER_CONNECTION.statuses.update(status=final_message, in_reply_to_status_id=id) + self.tweet_count += 1 + print("Id : " + id + "\nText : " + str(tweets["text"].encode("utf-8")) + "\nUsername : " + + tweets["user"]["screen_name"] + + "\nReply : " + final_message + "\n" + "Tweet envoyé : " + str(self.tweet_count) + "\n") + self.var_list_id.append(id) + except FileNotFoundError: + print("You specified a wrong path to the message file..\n") + raise FileNotFoundError + # If we already replied to the tweet, inform the user. + else: + print('Already replied to this tweet.\n') \ No newline at end of file diff --git a/messages.txt b/messages.txt new file mode 100644 index 0000000..228fdbc --- /dev/null +++ b/messages.txt @@ -0,0 +1 @@ +Put the tweets you want to send here. \ No newline at end of file