We noticed that one person we follow on Twitter posts very regularly and some of his posts appear to be repeats. This guy runs a news site and is always trying to get people to view news through his portal. So we wondered if he was perhaps using an automated system to post to Twitter.
I looked into doing it and it was pretty easy, within an hour I had my own bot Tweeting for me while I was watching “The Stoned Age.”
Here is the code
from time import sleep
from numpy import random
CONSUMER_KEY = 'get when you create an app'
CONSUMER_SECRET = 'get when you create an app'
ACCESS_TOKEN_KEY = 'get from Oauth, specific to a user'
ACCESS_TOKEN_SECRET = 'get from Oauth, specific to a user'
api = twitter.Api(consumer_key=CONSUMER_KEY,
tweetList = ['Too many markets not enough time',
'sorry to be negative but G.I. Joe the movie was awful',
'#Dell totally screwed up my Dads order',
'Using http://runzalot.com to track my running']
for tweet in tweetList:
status = api.PostUpdate(tweet)
print 'just posted: %s' % status.text
print "all Done master"
You first have to register an application with Twitter, do that here: http://dev.twitter.com/, don’t worry there is no approval process it’s very quick. Twitter lists a number of libraries for your language of choice but since we all know Python is the greatest language for prototyping an idea I decided to use Python. I used the intuitively named: python-twitter project. The documentation lists three dependencies but I ended up installing four the fourth one was setuptools.
After all the dependencies are installed you can install twitter-python. Before you start tweeting you need two sets of security credentials. The first one is consumer_key and consumer_secret, which you get from the Twitter Dev site when you register your applications. The second set access_token_key, and access_token_secret are specific to a twitter user. To get these you need to check out the get_access_token.py file that comes with the the twitter-python install. This is a command line python script that uses Oauth to tell Twitter that this application has the right to post on behalf of this user. Once that is done you can put those values into the code above and get moving.
I just have a time-randomized list of tweets to send out. You could do a lot more with this API like get a list of your friends and get a list of their tweets. If you wanted to generate a lot of tweets you could simply randomly pick a friend and then randomly pick a tweet and retweet it. Or you could search people outside of your network. If you are into generate a lot of tweets.
Back to our friend who was posting the same stuff over and over. Twitter will not allow you to post duplicate tweets. If you were interested in posting the same stuff over (I am not) and over say cycling between five tweets you would need to make them different. You could add a random character to the end of each tweet, or you could use synonyms.