Patience, Young Padawan [Jan. 27, 2012, 1:44 p.m.]
Guest what? We have all the pieces we need now to assemble the code into a single script.
debugging tips: in the instructions below we're going to slowly build up a script. do each line at a time and make sure it works, print out the results as you go.
script file
running the script file.
start w api call code from above
take the json_string and convert it to a native data object as you did in task 4
take that data object and extract the 'results' item (data['results']). iterate over each item in the results list using a for loop, just like you did in task 3.
for each result, pull out some specific items of iterest. for example, within the for loop, if i was interested in the username of the person sending the tweet, i would get access that item with the code
result[from_user'].
the easiest way to do this is by storing it in a variable:
from_user = result['from_user']
inside your for-loop, do this for however many other items interest you. for each item, save it in a variable, and start off by just printing out that variable name.
puts from_user (ruby)
print from_user (python)
now we will use some string formatting. instead of printing out raw data objects, let's make a nice sentence that is more appropriate for human consumption. Both Ruby and Python have ways to construct format strings. A format string is a string with variables dynamically inserted into the string at runtime. Here is an example in both languages:
Ruby:
name = "Cat woman"
occupation = "supervillain"
puts "my name is #{name} and my occupation is #{occupation}."
output: "my name is Cat Woman and my occupation is supervillain."
Python:
name = "Superman"
occupation = "superhero"
print "my name is %s and my occupation is %s" % (name, occupation)
output: "my name is Superman and my occupation is superhero."