Automating Mouse and Keyboard Using Python

Learn examples from DataCamp guru:

Twitter — is an online news and social networking service where users post and interact with them. These posts are known as “tweets”. Twitter is known as a social networking site for robots. We can use Python to post tweets without even opening the site. There is a Python library that is used to access the Python API known as tweepy. Here we will use tweepy for the same. 
Tweepy is not a native library. You need to install it before using. Installation is easy when you have a pip. In a terminal or command line, enter the following command to install tweepy.

 sudo install pip tweepy 

If you don`t have pip, install it as an external library. 
Do not forget to change the application type from “Read only” to “Read and write” in the settings. This also gave permission to tweet. 
Then go to https://apps.twitter.com/ . This is used to create a link between your script and Twitter. In the Keys and Access Tokens tab, get the consumer key (API key), consumer secret (API secret), access token, and access token secret.

Post a simple tweet

# module import

import tweepy

 
# personal data

consumer_key = "xxxxxxxxxxxxxxxx"

consumer_secret = "xxxxxxxxxxxxxxxx"

access_token = " xxxxxxxxxxxxxxxx "

access_token_secret = "xxxxxxxx xxxxxxxx "

  
# consumer key and secret authentication

auth = tweepy.OAuthHandler (consumer_key, consumer_secret)

 
# access token and secret authentication
auth.set_access_token (access_token, access_token_secret)

api = tweepy.API (auth)

 
# update status

api.update_status (status = " Hello Everyone! " )

This is a simple method that will set the "Hello everyone" tweet. This is a simple process and doesn`t really matter in real life. It is integrated into large programs for useful work. We can use a loop to tweet a large number of tweets. To maintain a period of time between any two tweets in a loop, we can use the sleep () function from the time module as shown.

time.sleep ( 600 ) # waits 600 seconds

It was all about posting a text tweet. What if we want to post a tweet with a media file, there is a separate method for that.

Post a tweet with a media file

Sometimes a user wants to post a tweet with a media file, which is pretty simple, if we use the interface of the website. Publishing with Python takes some effort. This is the same as posting just a text tweet with two lines of code.

# module import

import tweepy

 
# Personal information * Personal information

consumer_key = "xxxxxxxxxxxxxxxx"

consumer_secret = "xxxxxxxxxxxxxxxx"

access_token = " xxxxxxxxxxxxxxxx "

access_token_secret = "xxxxxxxxxxxxxxxx"

 
# authentication

auth = tweepy.OAuthHandler (consumer_key, consumer_secret)

auth.set_access_token (access_token, access_token_secret)

 

api = tweepy.API (auth)

tweet = "Text part of the tweet" # make

image_path = "path of the image" # make

 
# attach media

status = api.update_with_media (image_path, tweet ) 

api.update_status (status = tweet)

Please remember to change the application type as stated above. Without changing it, we will not be able to access the publication.

This article is provided by Rishabh Bansal . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you`d like to share more information on the topic discussed above.

Learn examples from DataCamp guru: