25
loading...
This website collects cookies to deliver better user experience
The Twitter API provides the tools you need to contribute to, engage with, and analyze the conversation happening on Twitter.
The API class provides access to the entire twitter RESTful API methods. Each method can accept various parameters and return responses. For more information about these methods please refer to API Reference.
pip install tweepy
CONSUMER_KEY = 'YOUR API KEY'
CONSUMER_SECRET = 'YOUR SECRET KEY'
KEY = 'YOUR ACCESS TOKEN'
SECRET = 'YOUR SECRET'
Dotenv is a zero-dependency module that loads environment variables from a .env file into process.env. Storing configuration in the environment separate from code is based on The Twelve-Factor App methodology.
pip install python-dotenv
import os
import tweepy
import time
import logging
from dotenv import load_dotenv
# API key
CONSUMER_KEY = os.getenv('CONSUMER_KEY')
# API secret key
CONSUMER_SECRET = os.getenv('CONSUMER_SECRET')
# Access token
KEY = os.getenv('KEY')
# Access token secret
SECRET = os.getenv('SECRET')
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(KEY, SECRET)
api = tweepy.API(auth)
api.update_status('bot tweeting live!')
import os
import tweepy
import time
import logging
from dotenv import load_dotenv
load_dotenv() # take environment variables from .env.
# API key
CONSUMER_KEY = os.getenv('CONSUMER_KEY')
# API secret key
CONSUMER_SECRET = os.getenv('CONSUMER_SECRET')
# Access token
KEY = os.getenv('KEY')
# Access token secret
SECRET = os.getenv('SECRET')
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(KEY, SECRET)
# Create API object
api = tweepy.API(auth)
# First tweet from your bot account!
api.update_status('bot tweeting live!')
FILE_NAME = 'last_seen.txt'
def read_last_seen(FILE_NAME):
file_read = open(FILE_NAME,'r')
last_seen_id = int(file_read.read().strip())
file_read.close()
return last_seen_id
def store_last_seen(FILE_NAME, last_seen_id):
file_write = open(FILE_NAME,'w')
file_write.write(str(last_seen_id))
file_write.close()
return
api.update_status
again, but now for replying to users with the most recents tweets with the word 'Brazil' with the message: "In brazilian portuguese we don't say foreign we say GRINGO and I think that is beautiful!"def reply():
tweets = api.mentions_timeline(read_last_seen(FILE_NAME), tweet_mode = 'extended')
for tweet in reversed(tweets):
if 'brazil' in tweet.full_text.lower():
api.update_status("@" + tweet.user.screen_name + " In brazilian portuguese we don't say foreign we say GRINGO and I think that is beautiful!", tweet.id)
store_last_seen(FILE_NAME, tweet.id)
reply()
on your python bot.pytime.sleep()
of 900 seconds to run the method api.retweet()
because of the rate limit imposed by Twitter API.def retweet():
for tweet in api.search(q="brazilian", lang="en", count=25):
status = api.get_status(tweet.id, tweet_mode = 'extended')
if not status.retweeted: # Check if Retweet
try:
api.retweet(tweet.id)
except Exception as e:
logger.error("Error on retweet", exc_info=True)
while True:
retweet()
time.sleep(900)
api.create_favorite()
like the following:def favorite():
for tweet in api.search(q="brazilian", lang="en", count=25):
status = api.get_status(tweet.id, tweet_mode = 'extended')
if not status.retweeted: # Check if Retweet
try:
api.create_favorite(tweet.id)
except Exception as e:
logger.error("Error on retweet", exc_info=True)
while True:
favorite()
time.sleep(900)
def retweet(i):
for tweet in api.search(q="brazilian", lang="en", count=12):
status = api.get_status(tweet.id, tweet_mode = 'extended')
if not status.retweeted: # Check if Retweet
try:
i = i + 1
api.update_status("@" + tweet.user.screen_name + " In brazilian portuguese we don't say foreign we say GRINGO and I think that is beautiful! +"+str(i), tweet.id)
api.retweet(tweet.id)
except Exception as e:
logger.error("Error on retweet", exc_info=True)
i = 0
while True:
retweet(i)
time.sleep(900)
time.sleep()
for call the method;