Twitter autofollow and DM using python-twitter, imaplib and gmail

The script will:

  1. connect via imap to gmail
  2. select the imap directory TwitterFollow (you should filter twitter follow notifications and label them with TwitterFollow)
  3. fetch the headers of all unread mails
  4. retrieve the twitter user screen_name that is following you from the headers
  5. attempt to create a friendship aka autofollow. If an error occurs is probably due to the fact you previously followed the user
  6. Send a direct message to thank for the following
  7. Flag the message as read so it won’t be fetched next time script runs

You could also check Twitter autofollow and direct message using python mechanize libgmail which is basically achieving the same goal but operate differently using reverse engineering on gmail web site.

3 Responses to “Twitter autofollow and DM using python-twitter, imaplib and gmail”
Martin Geber Posted on April 25, 2009 at 10:22 pm


“if friendship_created is True:” isn’t really pythonic :) Better write: “if friendship_created:”

But thanks for that inspiration!


Radu Boncea Posted on April 25, 2009 at 10:28 pm

the whole script isn’t quite pythonic and lacks introspection, so I tried to make it readable also for those not knowing python, just for documentation. Many lines could have been condensed into one, etc.

rouli Posted on September 12, 2009 at 6:19 pm

There are some minor fixes that I see are already implemented in the version hosted by Snipt ( but not over here. Mainly – there should be a slash before the w in the regular expression, and a slash before the Seen in order to mark a message as read.
Thought that future readers should now about it…

Post a Comment