Linux Format

Enter the following code in the new file:

-

import os import time from markovbot import MarkovBot ##### # INITIALISE # Initialise a MarkovBot instance tweetbot = MarkovBot() # Get the current directory’s path di rn ame=os.path.di rn ame(os. path. abs path (__ file __)) # Construct the path to the book book=os. path. join(di rn ame, u‘p g1661.txt ’) # Make your bot read the book! tweet bot. read( book) ##### # TWITTER # The MarkovBot uses @sixohsix’ Python Twitter Tools, which is a Python wrapper # for the Twitter API. Find it on GitHub: https://github.com/ sixohsix/twitter # ALL YOUR SECRET STUFF! # Make sure to replace the ‘’s below with your own values, or try to find # a more secure way of dealing with your keys and access tokens. Be warned # that it is NOT SAFE to put your keys and tokens in a plaintext script! # Consumer Key (API Key) cons_ key =‘ your consumer key here’ # Consumer Secret (API Secret) cons_ secret =‘ your consumer secret here’ # Access Token access_ token =‘ your access token here’ # Access Token Secret access_ token_ secret =‘ your access token secret here’ # Log in to Twitter tweet bot. twitter_ login( cons_ key, cons_ secret, access_ token, access_ token_ secret) # Start periodical­ly tweeting. This will post a tweet every minute. # (You’re free to choose your own interval, but please don’t use it to # spam other people. Nobody likes spammers and trolls.) # This function operates in a Thread in the background, so your code will not # block by calling it. tweet bot. twitter_ tweeting_ start( days =0, hours =0, minutes =1, keywords= None, prefix= None, suffix =‘# I am Sherlock ed ’) Once the code has been entered, you need to use your arrow keys to navigate to‘ your consumer key here ’, ‘your consumer secret here ’,‘ your access token here’ and ‘your access token secret here’ and replace these with the values from Twitter’s website that you noted down earlier. Note that you need to leave the quotation marks in place when replacing the values. You’ll also notice the filename pg661.txt under the words Construct the path to the book , so you also need to change the filename to your own text file. Scroll to the bottom of the code and note that, by default, this code will tweet every minute. Change this if you wish – for example, to once a day: days=1, hours=0, minutes=0 . Finally, you may wish to change the suffix placed after every tweet to something else or to None . Press Ctrl+X when done, then Y, then Return to save. You can run your script at any time with the following command: sudo python sherlock1.py Be sure to substitute sherlock1.py with the actual name of your file. The terminal will show the tweets, but you can also check it on the website.

There are many more possible Python projects you can do with Twitter, including responding to keywords, automatic retweets and even grabbing values from web pages, such as stock market prices. Head over to www.raspberryp­i.org/ blog/tag/python to see some of the exciting projects that are done with Python.

Newspapers in English

Newspapers from Australia