Sun Jun 21 2020Quickly Archive Public Twitter Accounts
I recently discovered that if a tweet link is shared on Telegram, it will stay there forever (possibly) even though the account's long gone or the tweet is deleted.
Since Twitter API allows to access a user's recent 3000 tweets, this gave me an idea to create a Telegram bot to archive other people's tweets.
I used Tweepy's Cursor method to access someone else's timeline, particularly their own tweets by implementing this bit:
username = "ekrem_imamoglu"
# This will get the tweets of <https://twitter.com/ekrem_imamoglu>
user_tweets = tweepy.Cursor(api.user_timeline, id=username).items()
for tweet in user_tweets:
print(tweet.text)
This snippet can eventually be used for storing and analysing information of accounts on Twitter. In this script, I am passing each tweet to the conversation so the Telegram hosts will open up a connection and index the data to their database.
I am putting the full code below, I believe the lines are pretty much self-explanatory, but I added comments where necessary.
import telebot
import time
import tweepy
import os
# Twitter App keys
consumer_key = os.environ.get('CONSUMER_KEY')
consumer_secret = os.environ.get('CONSUMER_SECRET')
# Twitter access keys
access_token = os.environ.get('ACCESS_TOKEN')
access_token_secret = os.environ.get('ACCESS_TOKEN_SECRET')
# Authorize and initiate the Twitter API
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# Bot keys and initialize the bot
api_token = os.environ.get('BOT_TOKEN')
bot = telebot.TeleBot(api_token)
@bot.message_handler(commands=['user'])
def tweet_archive(message):
'''
Listener to extract information from Twitter
The command message needs to be => "/user <TWITTER_USERNAME>"
'''
msg = message.text.split(' ')
if len(msg) > 1 and len(msg) < 3:
username = msg[1]
try:
# Use tweepy cursor to extract data
user_tweets = tweepy.Cursor(api.user_timeline, id=username).items()
i = 0
for tweet in user_tweets:
bot.send_message(message.chat.id, tweet.text)
i += 1
# Ensuring to not get HTTP 429 error by Telegram
# The bot rests in every 75 tweets
if i % 75 == 0:
time.sleep(15)
except Exception as e:
print(e)
bot.send_message(message.chat.id, 'Error happened.')
# Sending an exception log to my account
bot.send_message(86306879, str(e))
finally:
bot.send_message(message.chat.id, 'Archive completed.')
else:
bot.send_message(
message.chat.id, 'The format of this function is like this: "/user <TWITTER_USERNAME>".')
bot.polling()
The repository can be forked here, and the following steps need to be done to create your bot.
- Create a telegram bot by messaging
/newbot
to BotFather.
- Register to Twitter Developer Portal and create an app
- Install libraries
python install -r requirements.txt
- Change the environment variables with your ones
- Run the bot
python archivebot.py
- Send the command to the bot, for example:
/user ekremimamoglu
(this command will download the latest 3000 tweets of the Mayor of Istanbul)
- Profit.
Here is an example of what will happen when you finish and execute the script.

Stay safe and have a nice day!
Stay up-to-date
Subscribe to my newsletter and stay up-to-date. Why not? It's free.