Adding Whitelist
This commit is contained in:
parent
ad8c242cc7
commit
370277043f
2 changed files with 25 additions and 17 deletions
|
@ -25,6 +25,7 @@ TOKEN_SECRET | Token d'accès secret disponible dans la section `Authenticati
|
||||||
CONSUMER_KEY | Clé API disponible dans la section `Consumer Keys`
|
CONSUMER_KEY | Clé API disponible dans la section `Consumer Keys`
|
||||||
CONSUMER_SECRET | Clé secrète API disponible dans la section `Consumer Keys`
|
CONSUMER_SECRET | Clé secrète API disponible dans la section `Consumer Keys`
|
||||||
PSEUDOS | Pseudos du ou des compte.s que vous voulez écouter pour le snipe (a séparer avec une virgule **sans** espaces)
|
PSEUDOS | Pseudos du ou des compte.s que vous voulez écouter pour le snipe (a séparer avec une virgule **sans** espaces)
|
||||||
|
WHITELIST | Pseudos des comptes qui ne seront pas touché par le Bot (facultatif, a séparer avec une virgule **sans** espaces, par défaut la liste est vide)
|
||||||
VERBOSE | Affiche plus de messages dans la console [False\|True] (facultatif, par défaut sur False)
|
VERBOSE | Affiche plus de messages dans la console [False\|True] (facultatif, par défaut sur False)
|
||||||
|
|
||||||
Ensuite installe les dépendances avec `pip install -r requirements.txt`.
|
Ensuite installe les dépendances avec `pip install -r requirements.txt`.
|
||||||
|
|
41
main.py
41
main.py
|
@ -13,11 +13,16 @@ def load(variables) -> dict:
|
||||||
load_dotenv() # load .env file
|
load_dotenv() # load .env file
|
||||||
for var in variables:
|
for var in variables:
|
||||||
try:
|
try:
|
||||||
if var == "VERBOSE":
|
if var == "VERBOSE": # check is VERBOSE is set
|
||||||
try:
|
try:
|
||||||
res = bool(environ[var])
|
res = bool(environ[var])
|
||||||
except:
|
except:
|
||||||
res = False
|
res = False # if not its False
|
||||||
|
elif var == "WHITELIST": # check if WHITELIST is set
|
||||||
|
try:
|
||||||
|
res = list(set(environ[var].split(",")) - {""})
|
||||||
|
except:
|
||||||
|
res = [] # if not its an empty list
|
||||||
else:
|
else:
|
||||||
res = environ[var]
|
res = environ[var]
|
||||||
if var == "PSEUDOS":
|
if var == "PSEUDOS":
|
||||||
|
@ -62,7 +67,7 @@ class Listener(StreamListener):
|
||||||
print(f"Raison : {notice['reason']}")
|
print(f"Raison : {notice['reason']}")
|
||||||
|
|
||||||
def on_status(self, status):
|
def on_status(self, status):
|
||||||
if status._json["user"]["id"] in self.listOfFriendsID: # verification of the author of the tweet
|
if status._json["user"]["id"] in self.listOfFriendsID and status._json["user"]["screen_name"] not in keys["WHITELIST"]: # 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
|
||||||
if not hasattr(status, "retweeted_status"): # ignore Retweet
|
if not hasattr(status, "retweeted_status"): # ignore Retweet
|
||||||
if "extended_tweet" in status._json:
|
if "extended_tweet" in status._json:
|
||||||
|
@ -119,9 +124,9 @@ def getFriendsID(api, users: list) -> list:
|
||||||
|
|
||||||
def seniority(date: str) -> bool:
|
def seniority(date: str) -> bool:
|
||||||
"""Return True only if the given string date is less than one day old."""
|
"""Return True only if the given string date is less than one day old."""
|
||||||
datetimeObject = datetime.strptime(date, "%a %b %d %H:%M:%S +0000 %Y") # Convert String format to datetime format
|
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
|
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
|
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
|
return False if age.days >= 1 else True # False if older than a day
|
||||||
|
|
||||||
def permute(array: list) -> list:
|
def permute(array: list) -> list:
|
||||||
|
@ -130,11 +135,11 @@ def permute(array: list) -> list:
|
||||||
|
|
||||||
for text in array: # all element of the list
|
for text in array: # all element of the list
|
||||||
if text.lower() not in quoiListe:
|
if text.lower() not in quoiListe:
|
||||||
quoiListe.append(text.lower())
|
quoiListe.append(text.lower()) # word fully in lowercase
|
||||||
if text.upper() not in quoiListe:
|
if text.upper() not in quoiListe:
|
||||||
quoiListe.append(text.upper())
|
quoiListe.append(text.upper()) # word fully in uppercase
|
||||||
if text.capitalize() not in quoiListe:
|
if text.capitalize() not in quoiListe:
|
||||||
quoiListe.append(text.capitalize())
|
quoiListe.append(text.capitalize()) # word with the first letter in uppercase
|
||||||
return quoiListe
|
return quoiListe
|
||||||
|
|
||||||
def createBaseTrigger(lists) -> list:
|
def createBaseTrigger(lists) -> list:
|
||||||
|
@ -164,6 +169,12 @@ def main(accessToken: str, accessTokenSecret: str, consumerKey: str, consumerSec
|
||||||
exit(1)
|
exit(1)
|
||||||
print(f"@{api.me()._json['screen_name']}.")
|
print(f"@{api.me()._json['screen_name']}.")
|
||||||
|
|
||||||
|
if keys['WHITELIST'] == []:
|
||||||
|
whitelist = "Aucun"
|
||||||
|
else:
|
||||||
|
whitelist = f"@{', @'.join(keys['WHITELIST'])}"
|
||||||
|
print(f"Liste des comptes exclus : {whitelist}.")
|
||||||
|
|
||||||
listener = Listener(api, users)
|
listener = Listener(api, users)
|
||||||
stream = Stream(auth = api.auth, listener = listener)
|
stream = Stream(auth = api.auth, listener = listener)
|
||||||
stream.filter(track = triggerWords, languages = ["fr"], stall_warnings = True, is_async = True)
|
stream.filter(track = triggerWords, languages = ["fr"], stall_warnings = True, is_async = True)
|
||||||
|
@ -179,8 +190,7 @@ if __name__ == "__main__":
|
||||||
"""
|
"""
|
||||||
errorMessage = "Une erreur survient !" # error message
|
errorMessage = "Une erreur survient !" # error message
|
||||||
|
|
||||||
# words to detect in lowercase
|
base = { # words to detect in lowercase
|
||||||
base = {
|
|
||||||
"quoi": ["quoi", "koi", "quoient"],
|
"quoi": ["quoi", "koi", "quoient"],
|
||||||
"oui": ["oui", "ui"],
|
"oui": ["oui", "ui"],
|
||||||
"non": ["non", "nn"],
|
"non": ["non", "nn"],
|
||||||
|
@ -194,8 +204,7 @@ if __name__ == "__main__":
|
||||||
"mais": ["mais", "mé"]
|
"mais": ["mais", "mé"]
|
||||||
}
|
}
|
||||||
|
|
||||||
# creation of answers
|
answers = { # creation of answers
|
||||||
answers = {
|
|
||||||
"quoi": createBaseAnswers("feur") + [
|
"quoi": createBaseAnswers("feur") + [
|
||||||
"https://twitter.com/Myshawii/status/1423219640025722880/video/1",
|
"https://twitter.com/Myshawii/status/1423219640025722880/video/1",
|
||||||
"feur (-isson)",
|
"feur (-isson)",
|
||||||
|
@ -216,12 +225,10 @@ if __name__ == "__main__":
|
||||||
"mais": createBaseAnswers("on")
|
"mais": createBaseAnswers("on")
|
||||||
}
|
}
|
||||||
|
|
||||||
# creation of a list of all the words (only lowercase)
|
universalBase = createBaseTrigger(list(base.values())) # creation of a list of all the words
|
||||||
universalBase = createBaseTrigger(list(base.values()))
|
|
||||||
|
|
||||||
# creation of a list of all the words (upper and lower case)
|
triggerWords = permute(universalBase) # creation of a list of all the words (upper and lower case)
|
||||||
triggerWords = permute(universalBase)
|
|
||||||
|
|
||||||
# loading environment variables and launching the bot
|
# loading environment variables and launching the bot
|
||||||
keys = load(["TOKEN", "TOKEN_SECRET", "CONSUMER_KEY", "CONSUMER_SECRET", "PSEUDOS", "VERBOSE"])
|
keys = load(["TOKEN", "TOKEN_SECRET", "CONSUMER_KEY", "CONSUMER_SECRET", "PSEUDOS", "VERBOSE", "WHITELIST"])
|
||||||
main(keys["TOKEN"], keys["TOKEN_SECRET"], keys["CONSUMER_KEY"], keys["CONSUMER_SECRET"], keys["PSEUDOS"])
|
main(keys["TOKEN"], keys["TOKEN_SECRET"], keys["CONSUMER_KEY"], keys["CONSUMER_SECRET"], keys["PSEUDOS"])
|
||||||
|
|
Reference in a new issue