Tweet random quotes using Python
The Idea came into my mind while I was messing with Twython Tool . The Idea is very simple . You can run a simple Python program and a random quote will be automatically posted to your Twitter account. Add this to your Virtual Private Server’s Cron Job list and you will get some automated cool quotes update in Twitter! I was using it on my Ubuntu 14.04 Machine but it will work in any other Linux distribution as well.
Steps to Tweet Random Quotes using Python in Ubuntu 14.04/14.10/15.10 :
First, use this shortcut key combination “CTRL+ALT+T” to open your Terminal. Now we will install first the Twython script using simple Apt-get command.
sudo apt-get install python-pip
sudo pip install twython
Now to automatically tweet in your twitter account you will need to get Keys and Secret of your Twitter app. Simply create a new twitter application from this link. If you don’t know how to create a twitter app and how to get the keys and secrets please search it on Google. There are plenty of tutorials there. Just remember you will need these four values Consumer Key (API Key), Consumer Secret (API Secret) , Access Token , Access Token Secret .
Next we will create a new folder in our Desktop. Type the following in your Terminal.
Now change your directory to this newly created folder
We will now create a hidden file on this folder and give all four keys and secrets of twitter apps what you have found in earlier steps i.e. Consumer Key (API Key), Consumer Secret (API Secret) , Access Token , Access Token Secret .
Create a new file to save these credentials
Now you have to write all four values in this file , one value in each line. After that press CTRL+x to exit and press “y” to save changes to your file and press “Enter”.
Now we will create a test Python file using name “test.py”
Now you can copy paste the following code in this window:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
from twython import Twython import linecache import os import sys import random quotes = [ '"The path to success is to take massive, determined action." ~ Tony Robbins', '"A minute\'s success pays the failure of years." ~ Robert Browning', '"The perfect is the enemy of the good." ~ Voltaire', '"Words may show a man\'s wit but actions his meaning." ~ Benjamin Franklin', '"If you love life, don\'t waste time, for time is what life is made up of." ~ Bruce Lee', '"If you spend too much time thinking about a thing, you\'ll never get it done. " ~ Bruce Lee', '"The less effort, the faster and more powerful you will be. " ~ Bruce Lee', '"When you want something, all the universe conspires in helping you to achieve it." ~ Paulo Coelho', '"The chief danger in life is that you may take too many precautions." ~ Alfred Adler', '"We all have our time machines. Some take us back, they\'re called memories. Some take us forward, they\'re called dreams." ~ Jeremy Irons', ] HOME = os.path.expanduser('~/') passfile= HOME + '.passfile.text' APP_KEY = linecache.getline(passfile,1).strip() APP_SECRET = linecache.getline(passfile,2).strip() OAUTH_TOKEN = linecache.getline(passfile,3).strip() OAUTH_TOKEN_SECRET = linecache.getline(passfile,4).strip() twitter = Twython(APP_KEY, APP_SECRET) auth = twitter.get_authentication_tokens() print(auth) twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) credentials = twitter.verify_credentials() twitter.update_status(status=random.choice(quotes)) print("Successful!")
In this code as you can see I have only included only 10 quotes. You can add as many as you want. You can also create a file called quotes.txt and save all quotes there. But then you will need to modify the Python code as per this requirement.
Now everything is ready , just we need to run our Python Program. In your terminal type the following command
Voila! You can now post random quotes from a list by just running this Python Program. Thanks for reading my first tutorial about Python.
Last but not the least, if you find any typo or error please use the comment section. Your feedback is welcome!