Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 11, 2022 10:13 am GMT

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_idAllows a user or authenticated user ID to remove the Retweet of a Tweet.
  • GET /2/tweets/sample/streamStreams about 1% of all Tweets in real-time.
  • POST /2/users/:id/followingAllows 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 tweepydef 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

Create a followback task in python

import get_twitter_apiimport pandas as pdapi= get_twitter_api.twitter_api()friends = api.friends_ids(api.me().id)followers = api.followers_ids(api.me().id)# function to followback your friendsdef follow_back():    to_follow=[]    print("-------retrieving twitter information------")    print("----------People you follow:---------
", len(friends)) print("---------Your Followers:---------
", 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()

_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 scriptC:\data_eng\python_tweepy\Scripts\activate.bat && python C:\data_eng\python_tweepy\app.py

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


Original Link: https://dev.to/wachuka_james/python-tweepy-automating-a-follow-back-task-using-windows-scheduler-4fm0

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To