Automatic Tweet Liker using Tweepy and Django — Part 1

Ankush Chavan
3 min readApr 24, 2020

--

A complete guide for creating Automatic Tweet Liker using Django and Twitter API

Photo by Yucel Moran on Unsplash

So, I have a frontend ready and I’m going to create a web app that can automatically like tweets by integrating frontend with Django and Twitter API.

Technologies:

We have to work on real twitter data. To work with real twitter data what else can be better than Twitter API. Also, we are using Django(a framework for web app development) that uses Python as a backend language. So, we required a library in python to interact with Twitter API. There is one library in python named Tweepy which provides a way to use services provided by Twitter API.

The concept behind this app:

To use this app user must be required to log in using his/her Twitter account. Once a user is authenticated, this app uses the user’s Twitter account to like the tweets.

Code for authenticating user:

def auth(request):    # start the OAuth process, set up a handler with our details    oauth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)    # direct the user to the authentication url    # if user is logged-in and authorized then transparently goto       the callback URL    auth_url = oauth.get_authorization_url(True)    response = HttpResponseRedirect(auth_url)    # store the request token    request.session['request_token'] = oauth.request_token    return response

Once the user is authenticated, it will be redirected to the callback URL i.e. the home page.

Here, the user required to enter the keyword for which he had wished to like the tweets. It starts searching for the latest tweets that contain the entered keyword and likes the tweets as much as a counter is set. Currently, the counter is set to like the 5 tweets at once. The tweets can be liked by using the favorite() method provided by the Tweepy library.

Code for liking the tweet:

def like(request):    counter = 0    keyword = request.POST['search']    api = get_api(request)
# Getting the user user = api.me() print(user.screen_name) # Adding keyword to search the tweets for search = keyword # Specifying the tweets limit numberOfTweets = 5 # Fetching the tweets and liking them for tweet in tweepy.Cursor(api.search, search).items(numberOfTweets): try: tweet.favorite() print('Tweet Liked!') counter = counter + 1 time.sleep(10) except tweepy.TweepError as e: print(e.reason) except StopIteration: break return render(request, "liked.html", {'counter': counter, 'keyword': search})

After the tweets are liked, the user is redirected to the liked.html page which shows the count of tweets liked for the entered keyword.

Once the user had done using the app, he can logout from the app. This will logout the user and clear all of the session data.

Code for logging out:

def unauth(request):    """    logout and remove all session data    """    if check_key(request):        api = get_api(request)        request.session.clear()        logout(request)    return render(request, "login.html")

Using the AutoTweetLiker app:

To use this app, first of all, you will be required to create a python virtual environment, refer to this. Activate this virtual environment.

All of the app code is at https://github.com/cankush625/AutoTweetLiker.git

Clone the code

git clone https://github.com/cankush625/AutoTweetLiker.git

Here, we need to add CONSUMER_KEY and CONSUMER_SECRET_KEY from the twitter app. To create a twitter app for AutoTweetLiker visit https://developer.twitter.com/en/apps. Once the app is created on the twitter developer site, edit the callback URL as “http://127.0.0.1:8000/callback”. You can find consumer API keys in the “Keys and Tokens” tab inside the twitter app. Now, go to AUTOTWEETLIKER/auto_tweet_liker/utils.py

utils.py

import tweepyimport os#Application keyCONSUMER_KEY = ""CONSUMER_SECRET = ""def get_api(request):    # set up and return a twitter api object    oauth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)    access_key = request.session['access_key_tw']    access_secret = request.session['access_secret_tw']    oauth.set_access_token(access_key, access_secret)    api = tweepy.API(oauth)    return api

Give the API key value to the CONSUMER_KEY and API secret key to the CONSUMER_SECRET in utils.py

After that, you will need to install all of the required packages from requirements.txt file

pip install -r requirements.txt

Now you are all set to run the app.

python manage.py runserver

This will run the site/app at http://127.0.0.1:8000/

I hope you had learned to use Twitter API and how to integrate Twitter API with a Django web app.

Thanks a lot for reading my blog! If you liked this article, please give it a clap. You can also follow me on Twitter at @cankush625 or find me on LinkedIn. I’d love to hear from you if I can help you with Django and Python.

Now continue on to Automatic Tweet Liker using Tweepy and Django -Part 2!

--

--

Ankush Chavan
Ankush Chavan

Written by Ankush Chavan

Software Engineer, Tech Blogger More about me at: https://ankushchavan.com

No responses yet