How to make a real-time Twitter header with Python?

How to make a real-time Twitter header with Python?

Show the latest followers on your Twitter header

·

4 min read

If you go to my YouTube channel and Twitter profile, you will see these.

Youtube: image.png

Twitter: image.png

If you also want to achieve something similar, continue with the article.

Step 1: Twitter Developer Account

Firstly, we will need to apply for a Twitter developer account so that we can get access to Twitter API. When applying for this tell them your genuine reasons for this application. It might take one or two days to get it approved.

Apply here: developer.twitter.com/en/apply-for-access

Once it's approved log into your developer account and go to the developer portal.

Create a new app on this portal. Then you will be able to get the API key, API secret, Access token, and Access secret. When creating the access token make sure you enable the read, write access. Otherwise, you will not be able to update your Twitter profile with these keys.

image.png

Documentation: developer.twitter.com/en/docs/twitter-api/g..

Step 2: Writing Python Script

For this, we are going to use tweepy library. Tweepy makes things even easier. Install tweepy with pip

pip install tweepy

Let's write a simple code to update Twitter profile name

import tweepy

# twitter keys
api_key = <api_key>
api_secret = <api_secret_key>
access_token = <access_token>
access_token_secret = <access_token_secret>

# authentication
auth = tweepy.OAuthHandler(api_key, api_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)


def updateName():
    name = "A Little Coding"
    api.update_profile(name=name)


if __name__ == '__main__':
    updateName()

Let's update the header (banner) too

In the above script itself, you can just add this function too. Put a image file in the same folder with the script. Make sure you update the correct image file name in the script.

def updateBanner():
    api.update_profile_banner('banner.png')

Full script:

import tweepy

# twitter keys
api_key = <api_key>
api_secret = <api_secret_key>
access_token = <access_token>
access_token_secret = <access_token_secret>

# authentication
auth = tweepy.OAuthHandler(api_key, api_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)


def updateName():
    name = "A Little Coding"
    api.update_profile(name=name)


def updateBanner():
    api.update_profile_banner('banner.png')


if __name__ == '__main__':
    updateName()
    updateBanner()

Step 3: Create the real-time banner

Now, this step is totally up to you. Let me give you a broad idea of what I did. I wanted to show the youtube subscribers count, so I used google's youtube API to get the count. I would suggest first trying with Twitter followers count as you already have the Twitter API. Getting the followers count is as simple as this

def getTwitterCount(username):
    user = api.get_user(username)
    return user.followers_count

Creating the Text-based banner

In place of Twitter followers count you can take anything you like. For example, you may want to show your hashnode followers count. then you can use hashnode API to get the count. Now our goal is to create a banner image using that number. I used the Pillow library for this. Again you can simply use pip to install it.

pip install Pillow

I somewhat created a template image and saved it as background.png in the same folder. Now I can simply add my count to it and create a new banner.png

banner_before_after.jpg

For this, the code will be like this

from PIL import Image, ImageDraw, ImageFont


def createUpdatedImage(twitterCount):
    image = Image.open('background.png')
    draw = ImageDraw.Draw(image)
    font = ImageFont.truetype("arial.ttf", size=35)
    color = 'rgb(0, 0, 0)' # black color
    position = (100, 100)
    message = str(twitterCount) + " Followers"
    draw.text(position, message, fill=color, font=font)

    image.save('banner.png')

You can pass the Twitter follower count that we got from Twitter API. or you can use any other count or message.

You can check my complete code for this on Github.

GitHub Repo: github.com/Google987/update-twitter-account..

Final Step:

Now we can update the Twitter profile anytime with this script. But we can't just sit the whole day and run the script again and again. So we need to schedule our script. If you have a server or AWS free tier account that would be the best for this. On the server (Linux) you can easily schedule script with Cron. And on windows, you can use Task Scheduler. Personally, I've done it with Cron job. To schedule on Cron, you can simply type the command crontab -e This will open a file. At the end of the file, you can add your cron pattern and the command. It'll look something like this

0 * * * python /path/to/the/script.py

Adding this line will mean you want to run this command every hour. Now our python script will run every hour and the profile will get updated automatically.

You can also check out my videos on youtube on this same topic.

  • Cron Job image.png

  • How to Update Twitter Profile with Twitter API: image.png

Did you find this article valuable?

Support Arif Khan by becoming a sponsor. Any amount is appreciated!