DEV Community

Cover image for Python-tweepy: automating a follow back task using windows scheduler
James
James

Posted on • Edited on

Python-tweepy: automating a follow back task using windows scheduler

The Twitter API is a set of programmatic endpoints that can be used to understand or build a Twitter conversation.

This API allows you to search for and retrieve, interact with, or create a variety of resources, including:
Tweets, Users, Spaces, Live, Messages, Lists, Trending, Media, Locations.

Some twitter API endpoints

  • GET /2/tweets/:id/quote_tweets : Returns Quote Tweets for a Tweet specified by the requested Tweet ID.
  • POST /2/users/:id/retweets: Causes the user ID identified in the path parameter to Retweet the target Tweet.
  • DELETE /2/users/:id/retweets/:source_tweet_id Allows a user or authenticated user ID to remove the Retweet of a Tweet.
  • GET /2/tweets/sample/stream Streams about 1% of all Tweets in real-time.
  • POST /2/users/:id/following Allows a user ID to follow another user.If the target user does not have public Tweets, this endpoint will send a follow request.

Things you can do with twitter API:
You can perform various operations related to retrieving, displaying, sorting, and filtering data in Twitter.
For example, you can:

  • Create an app that gives users access to specific types of Tweets
  • Build a website where users can only see and read tweets from specific locations.
  • Start a personal project to explore the sentiment of your own tweets.
  • Curate specific Tweets to create stories and narratives on your site for any purpose. Show trends or show users her engagement with one of her branded hashtags.

Tweepy
Tweepy is a Python package that seamlessly and transparently accesses Twitter developer endpoints. Without Tweepy, the user would have to deal with various low-level details about her HTTP requests, rate limiting, authentication, serialization, etc. Tweepy handles all this mess for you, making your application error prone. Simply put, Tweepy is an open-source her Python package that gives developers the ability to communicate with her Twitter API.
For this task the methods used are supported by tweepy==3.10.0

Creating a twitter app with read and write permissions
https://developer.twitter.com/en/docs/projects/overview

  • Go to app settings -> user authentication settings -> app permissions and enable read/write
  • Under type of app enable public client, then provide callback url and website url
  • New keys will be generated.

Connecting to the twitter API

import tweepy

def twitter_api():
    # input your twitter API credentials
    consumer_key = ""
    consumer_secret = ""
    access_token = ""
    access_token_secret = ""
    # authentication of consumer key and secret
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    # authentication of access token and secret
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth, wait_on_rate_limit=True)
    return api
Enter fullscreen mode Exit fullscreen mode

Create a followback task in python

import get_twitter_api
import pandas as pd


api= get_twitter_api.twitter_api()

friends = api.friends_ids(api.me().id)
followers = api.followers_ids(api.me().id)
# function to followback your friends
def follow_back():
    to_follow=[]
    print("-------retrieving twitter information------")
    print("----------People you follow:---------\n", len(friends))
    print("---------Your Followers:---------\n", len(followers))
    for follower in followers:
        if follower not in friends:
            to_follow.append(follower)
            api.create_friendship(follower)
    print("-----You have followed:",len(to_follow),"More----")

follow_back()
Enter fullscreen mode Exit fullscreen mode

_Scheduling the task in using windows task scheduler
create a bat file that will activate the environment and run the python script

:: batch file to run python_tweepy_cron script every five minutes
:: program will first activate venv then run python script
C:\data_eng\python_tweepy\Scripts\activate.bat && python C:\data_eng\python_tweepy\app.py
Enter fullscreen mode Exit fullscreen mode

To add a task to windows scheduler:
go to start->task scheduler->create a folder (mytask)->create task (tweepy_follow_back)->trigger(repeat after 5 mins)->action(start program-schedule_follow_back.bat)

full code is available onhttps://github.com/James-Wachuka/python_tweepy

Top comments (0)