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!
From:
Anonymous (will be screened)
OpenID (will be screened if not validated)
Identity URL: 
User
Account name:
Password:
If you don't have an account you can create one now.
Subject:
HTML doesn't work in the subject.

Message:

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org


 
Notice: This account is set to log the IP addresses of everyone who comments.
Links will be displayed as unclickable URLs to help prevent spam.

April 2014

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

Expand Cut Tags

No cut tags