Twitter bot

Mar. 2nd, 2011 10:25 am
jodi: (Default)
[personal profile] jodi
I decided to start learning Python by creating a Twitter bot. I have an online diary, that I have been keeping for over a decade now, and I decided that I would output lines from it as tweets, so that I could keep in touch with my younger self. If you want to see it in action: Echochild.

Things I found odd to start with in Python:
No semi-colons at the end of lines
Layout actually matters
Having to put colons after if statements
Lists as opposed to arrays

I have been using Python as non-root. This has led me to much confusion about how to install packages. I tried installing virtual pythons, I tried easy install, and other things, and it just made me more and more muddled.

There are quite a few Python to Twitter packages, but in the end, I decided upon Tweepy.
I downloaded:
joshthecoder-tweepy-1.7.1-30-gfcaff74.tar.gz

I then unpacked it:
tar -xvzf joshthecoder-tweepy-1.7.1-30-gfcaff74.tar.gz

I then created the following directories in my home directory:
tweepy
tweepy/lib
tweepy/python2.6
tweepy/python2.6/site-packages

I then put site-packages on my path:
export PYTHONPATH=myhomedir/tweepy/lib/python2.6/site-packages

The site-wide Python is at /usr/pkg/bin/python, so from the joshthecoder-tweepy-fcaff74 directory I then ran:
/usr/pkg/bin/python setup.py install --prefix=myhomedir/tweepy

I formatted the diary entries, using Vi, so that there is just one sentence per line.

I got the appropriate authorisation tokens from Twitter: consumer token, consumer secret, etc.

Due to access restrictions, I am unable to run cron jobs or leave things running in the background, so concluded that being able to tweet just by going to a web page would do. Not quite automatic, but still, will do for now.

I then created a CGI script in the joshthecoder-tweepy-fcaff74 directory.
#!/usr/pkg/bin/python
import tweepy

#twitter authentication
auth = tweepy.OAuthHandler("consumer_token","consumer_secret")
auth.set_access_token("access token key","access token secret")
api = tweepy.API(auth)

fcount=0
diaryentries =[]

#mydiary.txt is the diary file. This is just opening the diary file, reading each line, and if it is the first line,
#and is less than 140 chars, writes it out to twitter.

f = open ('mydiary.txt','r')
for line in f:
     if fcount==0:
         if len(line)<140:
             api.update_status(line) #this line writes it to twitter
         else:
             diaryentries.append(line) #this line is just putting the diary entries back in the list
         fcount = fcount+1
f.close()

# write all the diary entries back out to the file
f = open ('mydiary.txt','w')
f.writelines(diaryentries) #writing the list of diary entries back to the file
f.close()

print "Content-Type: text/html" # HTML is following
print # blank line, end of headers
print "Echochild"

The next thing to do is make it actually reply to people!

on 2011-03-02 12:46 pm (UTC)
Posted by (Anonymous)
Yeah, python doesn't have a ++ operator; from the Zen of Python (import this on a console): "Special cases aren't special enough to break the rules." :)

You may have a bit of trouble getting the CGI script to use the right python version; if you can't get apache to use the virtualenv's own one (which magically sets up the correct site-packages, etc), although I suspect Apache makes that fairly easy to configure. If all else fails, check out the activate_this.py script that virtualenv installes into the virtualenv's bin/, which can set up the magic before you start importing anything.

on 2011-03-02 12:48 pm (UTC)
Posted by [identity profile] nickmurdoch.livejournal.com
Oops, that was from me. I should probably just get a dreamwidth account.

on 2011-03-02 01:46 pm (UTC)
damerell: (computers)
Posted by [personal profile] damerell
Presumably there is some crazy doublethink that makes missing '++' featureful.

April 2014

S M T W T F S
  12345
6789101112
13141516171819
20212223242526
27282930   

Most Popular Tags

Expand Cut Tags

No cut tags