This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
feurBot/main.py

92 lines
3.9 KiB
Python
Raw Normal View History

2021-08-03 14:17:00 +02:00
from dotenv import load_dotenv
from os import environ
from tweepy import OAuthHandler, API, StreamListener, Stream
from re import sub
2021-08-03 19:19:30 +02:00
from random import choice
2021-08-03 20:27:06 +02:00
from datetime import datetime
2021-08-03 19:59:32 +02:00
from pytz import timezone
2021-08-03 14:17:00 +02:00
def load(variables):
"""Load env variables."""
keys = {}
load_dotenv() # load .env file
for var in variables:
try:
keys[var] = environ[var]
except KeyError:
print(f"Please set the environment variable {var} (.env file supported)")
exit(1)
return keys
class Listener(StreamListener):
2021-08-03 23:49:55 +02:00
def __init__(self, api = None, user = None):
2021-08-03 14:17:00 +02:00
super(Listener, self).__init__()
self.api = api
2021-08-03 23:49:55 +02:00
self.listOfFriendsID = api.friends_ids(user)
2021-08-03 14:17:00 +02:00
def on_status(self, status):
2021-08-03 19:59:32 +02:00
"""Answer to tweets."""
if seniority(status._json["created_at"]):
2021-08-03 20:53:32 +02:00
tweetText = sub(r'https?:\/\/\S+| +?\?|\?| +?\!| ?\!', '', status._json["text"])
2021-08-03 19:59:32 +02:00
if tweetText.endswith(tuple(quoi)):
2021-08-03 23:49:55 +02:00
if status._json["user"]["id"] in self.listOfFriendsID:
2021-08-03 19:59:32 +02:00
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}")
2021-08-03 23:33:53 +02:00
pass
2021-08-03 19:59:32 +02:00
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
2021-08-03 14:17:00 +02:00
def permute(array: list):
2021-08-03 23:33:53 +02:00
quoiListe = []
for text in array: # all element of the list
n = len(text)
mx = 1 << n # Number of permutations is 2^n
text = text.lower() # Converting string to lower case
for i in range(mx): # Using all subsequences and permuting them
combination = [k for k in text]
for j in range(n):
if (((i >> j) & 1) == 1): # If j-th bit is set, we convert it to upper case
combination[j] = text[j].upper()
temp = ""
for i in combination:
temp += i
2021-08-03 23:33:53 +02:00
quoiListe.append(temp)
return quoiListe
2021-08-03 19:19:30 +02:00
def main(accessToken, accessTokenSecret, consumerKey, consumerSecret, user):
2021-08-03 14:17:00 +02:00
"""Main method."""
auth = OAuthHandler(consumerKey, consumerSecret)
auth.set_access_token(accessToken, accessTokenSecret)
2021-08-03 23:11:17 +02:00
api = API(auth_handler = auth, wait_on_rate_limit = True)
2021-08-03 14:17:00 +02:00
2021-08-03 23:49:55 +02:00
listener = Listener(api, user)
2021-08-03 19:19:30 +02:00
stream = Stream(auth = api.auth, listener = listener)
2021-08-03 20:26:47 +02:00
print(f"Scroll sur Twitter avec les abonnements de @{user}...")
2021-08-03 19:59:32 +02:00
stream.filter(track = quoi, languages = ["fr"], is_async = True)
2021-08-03 14:17:00 +02:00
if __name__ == '__main__':
"""
2021-08-03 19:19:30 +02:00
TOKEN is the Access Token available in the Authentication Tokens section under Access Token and Secret sub-heading.
TOKEN_SECRET is the Access Token Secret available in the Authentication Tokens section under Access Token and Secret sub-heading.
CONSUMER_KEY is the API Key available in the Consumer Keys section.
CONSUMER_SECRET is the API Secret Key available in the Consumer Keys section.
2021-08-03 14:17:00 +02:00
--
2021-08-03 23:11:17 +02:00
PSEUDO is the PSEUDO of the account you want to listen to snipe.
2021-08-03 14:17:00 +02:00
"""
quoi = permute(["quoi", "koi"])
2021-08-03 23:33:53 +02:00
feur = ["feur", "(feur)", "FEUR", "feur lol", "https://twitter.com/shukuzi62/status/1422611919538724868/video/1"]
2021-08-03 19:19:30 +02:00
keys = load(["TOKEN", "TOKEN_SECRET", "CONSUMER_KEY", "CONSUMER_SECRET", "PSEUDO"])
main(keys["TOKEN"], keys["TOKEN_SECRET"], keys["CONSUMER_KEY"], keys["CONSUMER_SECRET"], keys["PSEUDO"])