using dict instead of variables

This commit is contained in:
Mylloon 2021-08-04 18:30:49 +02:00
parent aee50a863b
commit c638a6b5df

44
main.py
View file

@ -18,7 +18,7 @@ def load(variables) -> dict:
res = list(set(res.split(',')) - {""}) # create a list for the channels and remove blank channels and doubles res = list(set(res.split(',')) - {""}) # create a list for the channels and remove blank channels and doubles
keys[var] = res keys[var] = res
except KeyError: except KeyError:
print(f"Please set the environment variable {var} (.env file supported)") print(f"Veuillez définir la variable d'environnement {var} (fichier .env supporté)")
exit(1) exit(1)
return keys return keys
@ -38,18 +38,21 @@ class Listener(StreamListener):
lastWord = tweetText.split()[-1:][0] lastWord = tweetText.split()[-1:][0]
print(f"Tweet trouvé (dernier mot: \"{lastWord}\")...", end = " ") print(f"Tweet trouvé (dernier mot: \"{lastWord}\")...", end = " ")
if lastWord in universalBase: # check if the last word found is a supported word if lastWord in universalBase: # check if the last word found is a supported word
if lastWord in quoiBase: answer = None
answer = feur for mot in base.items():
elif lastWord in ouiBase: if lastWord in mot[1]:
answer = stiti answer = answers[mot[0]]
elif lastWord in nonBase: if answer == None:
answer = bril print(f"{errorMessage} Aucune réponse trouvée.")
else:
print(f"Envoie d'un {answer[0]}...", end = " ") print(f"Envoie d'un {answer[0]}...", end = " ")
try: # send answer try: # send answer
self.api.update_status(status = choice(answer), in_reply_to_status_id = status._json["id"], auto_populate_reply_metadata = True) 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]} !") print(f"{status._json['user']['screen_name']} s'est fait {answer[0]} !")
except Exception as error: except Exception as error:
print(f"\n{errorMessage} {error}") print(f"\n{errorMessage} {error}")
else:
print("Annulation parce que le dernier mot n'est pas intéressant.")
def do_stuff(self): def do_stuff(self):
while True: while True:
@ -91,7 +94,7 @@ def permute(array: list) -> list:
quoiListe.append(temp) quoiListe.append(temp)
return quoiListe return quoiListe
def createBaseTrigger(*lists) -> list: def createBaseTrigger(lists) -> list:
"""Merges all given lists into one.""" """Merges all given lists into one."""
listing = [] listing = []
for liste in lists: for liste in lists:
@ -124,22 +127,29 @@ if __name__ == '__main__':
-- --
PSEUDO is the PSEUDO of the account you want to listen to snipe. PSEUDO is the PSEUDO of the account you want to listen to snipe.
""" """
errorMessage = "Error happens!" # error message errorMessage = "Une erreur survient !" # error message
# words to detect # words to detect
quoiBase = ["quoi", "koi", "quoient", "q u o i"] base = {
ouiBase = ["oui", "ui"] "quoi": ["quoi", "koi", "quoient", "q u o i"],
nonBase = ["non", "nn"] "oui": ["oui", "ui"],
universalBase = createBaseTrigger(quoiBase, ouiBase, nonBase) "non": ["non", "nn"]
}
universalBase = createBaseTrigger(list(base.values()))
# creation of the list with all alternatives (upper/lower case) # creation of the list with all alternatives (upper/lower case)
triggerWords = permute(universalBase) triggerWords = permute(universalBase)
# creation of answers # creation of answers
feur = createBaseAnswers("feur") answers = {
feur.extend(["https://twitter.com/shukuzi62/status/1422611919538724868/video/1", "feur (-isson)", "https://twitter.com/antoinelae/status/1422943594403581957/video/1"]) # add a message in addition to the default answers "quoi": createBaseAnswers("feur") + [
stiti = createBaseAnswers("stiti") "https://twitter.com/shukuzi62/status/1422611919538724868/video/1",
bril = createBaseAnswers("bril") "feur (-isson)",
"https://twitter.com/antoinelae/status/1422943594403581957/video/1"
],
"oui": createBaseAnswers("stiti"),
"non": createBaseAnswers("bril")
}
# loading environment variables and launching the bot # loading environment variables and launching the bot
keys = load(["TOKEN", "TOKEN_SECRET", "CONSUMER_KEY", "CONSUMER_SECRET", "PSEUDOS"]) keys = load(["TOKEN", "TOKEN_SECRET", "CONSUMER_KEY", "CONSUMER_SECRET", "PSEUDOS"])