My First Twitter Bot

CitiBike NYC racks in Manhattan, by Margaret Bedore (CC 3.0)

CitiBike NYC racks in Manhattan, by Margaret Bedore (CC 3.0) from this article

Inspired by the legion of Twitter bots created by Allison Parish to disseminate all kinds of lovely linguistic mayhem, I decided to build my own bot. While I love what she’s put together (my personal favorites are The Ephemerides and Deep Question Bot), I knew that generating text algorithmically wouldn’t be my thing. I’d played with Citibike data, particularly the live station data when Jeff Ferzoco and I built Corridorscope. For some odd reason, the Citibike Status Twitter account is maintained manually. Why would someone do that when there’s such a rich source of data in the station JSON feed? So I decided to build one.

The recipe was fairly simple. I download the stations JSON, count the number of available bikes in active stations, total docks in active stations, and number of active stations.

That part was easy. The next step came in creating the Twitter bot. After creating the account, I first tried following the advice from several blogs that suggested using the “tweepy” package, but my account wouldn’t verify and I couldn’t post. After searching for an answer, I landed on this post suggesting twython instead, which worked beautifully. Not only did I not have the authentication error, the authentication process was amazingly easy.

Compare the way (from this post):

With the method:

Getting it all working was easy after I cleared that hurdle. I published my first tweet:

(It is a bot, right?)

After testing the code on my computer and tweaking the text, I deployed the code onto an AWS EC2 instance. This is my go-to method for automated scripts, even ones I run manually. I spun up an Amazon Linux t2.micro and got it going. At some point between when I did this last (probably in the fall to help out with the Rudin Center Job Access project), Amazon added pip, the Python package manager, to the base installation of python-devel. When I tried to installing it after the fact according to the pip docs, it opened up a can of misery when I tried to run “sudo pip”:

Traceback (most recent call last):
File “/usr/bin/pip”, line 5, in <module> from pkg_resources import load_entry_point
File “/usr/lib/python2.7/dist-packages/pkg_resources/”, line 3020, in <module> working_set = WorkingSet._build_master()
File “/usr/lib/python2.7/dist-packages/pkg_resources/”, line 616, in _build_master return cls._build_from_requirements(__requires__)
File “/usr/lib/python2.7/dist-packages/pkg_resources/”, line 629, in _build_from_requirements dists = ws.resolve(reqs, Environment())
File “/usr/lib/python2.7/dist-packages/pkg_resources/”, line 807, in resolve raise DistributionNotFound(req) pkg_resources.DistributionNotFound: pip==6.1.1

After scouring the web until my eyes bled, I went ahead and terminated the instance and started over. This time, I thought I’d check and found pip installed after I’d run “yum install python-devel”. From there, it was a cake to get everything running in a Linux screen session.

So the lessons learned from this exercise?

  • Twitter bots are easy with the right package.
  • Check before you install something.
  • Don’t be afraid to start over when the all-knowing, all-seeing Google isn’t providing answers.
  • A Twitter bot army isn’t built overnight, but one-by-one.

2 thoughts on “My First Twitter Bot

  1. Pingback: The Art of Writing Automatic Tweets | Datapolitan

  2. Pingback: Installing matplotlib and pandas on Amazon EC2 | Datapolitan

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s