-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTwitter_Bot_2.py
More file actions
51 lines (40 loc) · 1.88 KB
/
Copy pathTwitter_Bot_2.py
File metadata and controls
51 lines (40 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from TextoTwitter_2 import corpo2
from ImageMatriz import tirar_screenshot_objeto
import os
from dotenv import load_dotenv
from pathlib import Path
import tweepy
def twitt2():
# Carrega variáveis de ambiente do arquivo .env
load_dotenv()
# Obtém as credenciais de autenticação do Twitter a partir das variáveis de ambiente
CONSUMER_KEY = os.environ.get('CONSUMER_KEY_2')
CONSUMER_SECRET = os.environ.get('CONSUMER_SECRET_2')
ACESS_TOKEN = os.environ.get('ACESS_TOKEN_2')
ACESS_TOKEN_SECRET = os.environ.get('ACESS_TOKEN_SECRET_2')
BEARER_TOKEN = os.environ.get('BEARER_TOKEN_2')
# Cria um cliente do Tweepy para interação com a API do Twitter usando credenciais OAuth2
client = tweepy.Client(
consumer_key = CONSUMER_KEY,
consumer_secret = CONSUMER_SECRET,
access_token = ACESS_TOKEN,
access_token_secret = ACESS_TOKEN_SECRET,
bearer_token = BEARER_TOKEN
)
# Configura o método de autenticação OAuth1 para usar a API do Twitter
auth = tweepy.OAuth1UserHandler(CONSUMER_KEY, CONSUMER_SECRET, ACESS_TOKEN, ACESS_TOKEN_SECRET)
# Cria uma instância da API do Tweepy com o método de autenticação configurado
api = tweepy.API(auth)
# Obtém o texto do tweet chamando a função corpo
texto = corpo2()
# Captura uma imagem chamando a função tirar_screenshot_objeto
imagem = Path.cwd() / "screenshot_objeto_recortado.png"
# Faz o upload da imagem para o Twitter
media = api.media_upload(imagem)
# Cria e posta o tweet com o texto e a imagem carregada
client.create_tweet(text=texto, media_ids=[media.media_id])
# Imprime uma mensagem de confirmação no console
print("Tweet postado com sucesso.")
print('\n==================================================================================================\n')
if __name__ == "__main__":
twitt2()