One of the most undeniably thrilling things about the web today is the endless opportunity it affords for interacting with complete strangers. One downside is that it’s often pretty difficult to tell whether that stranger is a human or a robot, particularly when those online interactions are limited strictly to text, as they often are. People fall in love with robots on dating sites all the time, or have eleven-hour conversations with chatbots on Instant Messenger. In an art context, the history of bots goes back as far as the theatrical automata of Leonardo Do Vinci up to recent blogs by Cory Arcangel, one of which endlessly apologizes for not posting enough. To help prevent you from neglecting your blogging duties, we’re going to show you how to make your very own blogging robot.
The first thing you will need for this tutorial is a data stream. A database of information or a good API is key to the creation of a good bot. Without the data that represents the way our robot will think, we are at a total loss. This tutorial will use YouTube as a data stream aka our robot’s range of interest.
Our robot will be a Tumblr blog that celebrates the popular internet meme of “planking.” The methodologies introduced in this tutorial can be applied to make a variety of other robots or blogs, thematically. This tutorial assumes you’re on Mac OS X and will require the use of a plain text editor (we’re big fans of TextMate, TextWrangler, or Smultron).
First, lets take a look at the YouTube Data API. For our purposes, the usage of this API will be very straightforward. To retrieve a series of videos from YouTube’s Data API, we must simply send a request to a URL.
https://gdata.youtube.com/feeds/api/videos
Entering the above URL into your browser will return a large amount of XML from YouTube. While this is a whole lot of data, it is useless for us in its current state because we are not specifying what we want. To specify a search term from YouTube and narrow down our results, we’ll have to add some parameters to our query. You can add a parameter to a URL by appending a question mark, followed by the parameter itself. Let’s start by asking for JSON instead of XML. We prefer working with JSON because it’s lightweight and has become much more popular than XML in open source circles in the last few years. You can read more about JSON at https://json.org. Our URL that asks for a JSON response now looks like this:
https://gdata.youtube.com/feeds/api/videos?alt=json
Now lets add a few more parameters to specify exactly what it is we want. In this case, we want the most recently uploaded video about planking. These two parameters are “q”, which is short for “query” and “orderby”, which instructs YouTube to return results ordered by the videos most recently published, rather than relevance. So our final query will be:
https://gdata.youtube.com/feeds/api/videos?alt=json&q=planking&orderby=published
From here on out, we should have a pretty solid data source to feed our robot. Our bot will be written in Ruby, a popular programming language created by Yukihiro Matsumoto. Let’s create a file on your desktop called robot.rb. This is where we will put all our code. First, we will need to require specific parts of the Ruby language to use in our program.
require ‘open-uri’
require ‘uri’
require ‘json’
Next, we need to access our YouTube endpoint from our program.
@raw_data = open(‘https://gdata.youtube.com/feeds/api/videos?alt=json&q=planking&orderby=published’).read
Then, we parse the data using the JSON.parse method.
@parsed_data = JSON.parse(@raw_data)
Finally, we grab the link to the most recent video.
@link = @parsed_data[‘feed’][‘entry’][0][‘link’][‘href’]
This will provide us with a link to the most recent YouTube video featuring planking. Our very last step is actually posting the video to Tumblr. We can do this with the following code.
Net::HTTP.post_form(URI.parse(‘https://www.tumblr.com/api/write’),
{ ’email’ => ‘THE_EMAIL_YOU_LOG_INTO_TUMBLR_WITH’,
‘password’ => ‘YOUR_TUMBLR_PASSWORD’,
‘type’ => ‘video’,
’embed’ => @link
})
To run the program, open your terminal and type
ruby ~/Desktop/robot.rb
This will execute our program and post to Tumblr. Our bot lives!