From 4ef15d2e32fc2e7045e5678d244071f96fc77df8 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 3 Aug 2021 19:59:32 +0200 Subject: [PATCH] set a limit of age (1day) --- main.py | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index 6c01399..63e2ad6 100644 --- a/main.py +++ b/main.py @@ -3,10 +3,8 @@ from os import environ from tweepy import OAuthHandler, API, StreamListener, Stream from re import sub from random import choice - -quoi = ["quoi", "koi"] -feur = ["feur", "(feur)", "FEUR", "feur lol"] -friends = [] +from datetime import datetime, timedelta +from pytz import timezone def load(variables): """Load env variables.""" @@ -23,20 +21,26 @@ def load(variables): class Listener(StreamListener): def __init__(self, api = None): super(Listener, self).__init__() - self.num_tweets = 0 self.api = api def on_status(self, status): - """Réponse au tweet""" - tweetText = sub(r' ?\?| ?\!', '', status._json["text"]) - if tweetText.endswith(tuple(quoi)): - try: + """Answer to tweets.""" + if seniority(status._json["created_at"]): + tweetText = sub(r' +?\?|\?| +?\!| ?\!', '', status._json["text"]) + if tweetText.endswith(tuple(quoi)): if status._json["user"]["screen_name"] in friends: - self.api.update_status(status = choice(feur), in_reply_to_status_id = status._json["id"], auto_populate_reply_metadata = True) - print(f"{status._json['user']['screen_name']} est passé au coiffeur !") - except Exception as error: - print(error) - pass + try: + self.api.update_status(status = choice(feur), in_reply_to_status_id = status._json["id"], auto_populate_reply_metadata = True) + print(f"{status._json['user']['screen_name']} est passé au coiffeur !") + except Exception as error: + print(f"Error happens! {error}") + pass + +def seniority(date: str): + datetimeObject = datetime.strptime(date, '%a %b %d %H:%M:%S +0000 %Y') # Convert String format to datetime format + datetimeObject = datetimeObject.replace(tzinfo = timezone('UTC')) # Twitter give us an UTC time + age = datetime.now(timezone('UTC')) - datetimeObject # Time now in UTC minus the time we got to get the age of the date + return False if age.days >= 1 else True # False if older than a day def main(accessToken, accessTokenSecret, consumerKey, consumerSecret, user): """Main method.""" @@ -52,7 +56,7 @@ def main(accessToken, accessTokenSecret, consumerKey, consumerSecret, user): friends.append(friend._json["screen_name"]) print(f"Scroll sur Twitter avec les abonnés de @{user}...") - stream.filter(track = quoi, languages=["fr"], is_async = True) + stream.filter(track = quoi, languages = ["fr"], is_async = True) if __name__ == '__main__': """ @@ -63,5 +67,8 @@ if __name__ == '__main__': -- PSEUDO is the PSEUDO of the account you want to listen to snipe. A proportion of who s.he follow will be targeted. """ + quoi = ["quoi", "koi"] + feur = ["feur", "(feur)", "FEUR", "feur lol"] + friends = [] keys = load(["TOKEN", "TOKEN_SECRET", "CONSUMER_KEY", "CONSUMER_SECRET", "PSEUDO"]) main(keys["TOKEN"], keys["TOKEN_SECRET"], keys["CONSUMER_KEY"], keys["CONSUMER_SECRET"], keys["PSEUDO"])