ignore retweet and retrieve entire tweet

This commit is contained in:
Mylloon 2021-08-04 20:24:22 +02:00
parent e9937d1bb3
commit 79caf5b097

45
main.py
View file

@ -33,27 +33,32 @@ class Listener(StreamListener):
"""Answer to tweets.""" """Answer to tweets."""
if status._json["user"]["id"] in self.listOfFriendsID: # verification of the author of the tweet if status._json["user"]["id"] in self.listOfFriendsID: # verification of the author of the tweet
if seniority(status._json["created_at"]): # verification of the age of the tweet if seniority(status._json["created_at"]): # verification of the age of the tweet
# recovery of the last "usable" word of the tweet if not hasattr(status, "retweeted_status"): # ignore Retweet
regex = r"https?:\/\/\S+| +?\?|\?| +?\!| ?\!|-|~|(?<=ui)i+|@\S+|\.+|(?<=na)a+(?<!n)|(?<=quoi)i+|(?<=no)o+(?<!n)|…" try: # retrieve the entire tweet
tweetText = sub(regex, "", status._json["text"].lower()) tweet = status.extended_tweet["full_text"]
lastWord = tweetText.split()[-1:][0] except AttributeError:
print(f"Tweet trouvé de {status._json['user']['screen_name']} (dernier mot : \"{lastWord}\")...", end = " ") tweet = status.text
if lastWord in universalBase: # check if the last word found is a supported word # recovery of the last "usable" word of the tweet
answer = None regex = r"https?:\/\/\S+| +?\?|\?| +?\!| ?\!|-|~|(?<=ui)i+|@\S+|\.+|(?<=na)a+(?<!n)|(?<=quoi)i+|(?<=no)o+(?<!n)|…"
for mot in base.items(): tweetText = sub(regex, "", tweet.lower())
if lastWord in mot[1]: lastWord = tweetText.split()[-1:][0]
answer = answers[mot[0]] print(f"Tweet trouvé de {status._json['user']['screen_name']} (dernier mot : \"{lastWord}\")...", end = " ")
if answer == None: if lastWord in universalBase: # check if the last word found is a supported word
print(f"{errorMessage} Aucune réponse trouvée.") answer = None
for mot in base.items():
if lastWord in mot[1]:
answer = answers[mot[0]]
if answer == None:
print(f"{errorMessage} Aucune réponse trouvée.")
else:
print(f"Envoie d'un {answer[0]}...", end = " ")
try: # send answer
self.api.update_status(status = choice(answer), in_reply_to_status_id = status._json["id"], auto_populate_reply_metadata = True)
print(f"{status._json['user']['screen_name']} s'est fait {answer[0]} !")
except Exception as error:
print(f"\n{errorMessage} {error}")
else: else:
print(f"Envoie d'un {answer[0]}...", end = " ") print("Annulation car le dernier mot n'est pas intéressant.")
try: # send answer
self.api.update_status(status = choice(answer), in_reply_to_status_id = status._json["id"], auto_populate_reply_metadata = True)
print(f"{status._json['user']['screen_name']} s'est fait {answer[0]} !")
except Exception as error:
print(f"\n{errorMessage} {error}")
else:
print("Annulation car le dernier mot n'est pas intéressant.")
def do_stuff(self): def do_stuff(self):
while True: while True: