Patience, Young Padawan – More Code

Today, I am going to explore farther or deeper into what we can do with what Twitter API calls returns to us. I will make the whole post a step-by-step tutorial so that anyone interested in trying the codes out can do so with ease. So, let us get started.


#Always remember to use comments when writing code
#since we are using Python as our programming language
#of choice, we will make sure to import the necessary
#modules or libraries into our file

import urllib2
import json


After importing the modules or libraries that we will need during our coding adventure, we can now start writing some code. Remember to exercise self-documenting programming. Use variable names that describe what the variables store!

#Create a variable to store the url – twitter api link
#Make the api call and store the returned value in a variable
#I am querying twitter for “nbcbetty” – Off Their Rockers!

url =””
tweeter_data = urllib2.urlopen(url)
parsed_data = json.load(tweeter_data)

After making our API call to twitter, we get JSON data back but we cannot do much with it considering how messy it is. Using JSON’s load method, we convert that data into easy to use information (dictionaries). Now let us do some cool stuff!


#Now let us perform some magic here with the ‘parsed_data’
#Think up something cool? I have you covered!
#let us make some sentences from tweets by English speakers
#only about #NBCBETTY. Good idea right?

i = 0 #define a counter variable and assign 0
while i < len(parsed_data[‘results’]):
if parsed_data[‘results’][i][‘iso_language_code’] == ‘en’:
print “@%s tweeted: %s on %s about Off Their Rockers Show!” %
i += 1
#I used line breaks because the space was too small! Sorry.
#If you run that code – assuming you copied it correctly,
#you should get easy to read lines of sentences(tweets)
#saying @username tweeted something on [date] about Off Their


This is what you get depending on what your query is:

@Carmalva tweeted: @NBCBetty wish all the victims would give us feedback on their reaction after seeing themselves on tonites episodes…lmfao on Wed, 09 Jan 2013 02:35:35 +0000 about Off Their Rockers!
@Shannon Dawn Tripp: tweeted I just love Betty White! #GetGlue @NBCBetty on Wed, 09 Jan 2013 02:34:55 +0000 about Off Their Rockers!
@Sue Troupe tweeted: I’m watching Betty White’s Off Their Rockers (3693 others checked-in) #GetGlue @NBCBetty on Wed, 09 Jan 2013 02:33:30 +0000 about Off Their Rockers!


#Putting all together!
#grab all the pieces and put them together into a script that you
#yes, you could run from your command line!
#you can make this code more elegant than this – try!
import urllib2
import json

url = ‘’ #q=karma ??
parsed_data = ”
tweeter_data = urllib2.urlopen(url) #make the call safely
parsed_data = json.load(tweeter_data) #assign to parsed_data
except urllib2.URLError, e: #catch any exception
print e.reason

i = 0 #create counter variable

while i < len(parsed_data[‘results’]): #iterate through data
if parsed_data[‘results’][i][‘iso_language_code’] == ‘en’:
print “@%s tweeted: %s on %s about Off Their Rockers!” %
i += 1
#that should do it!
#Study that script closely and look at what you received from Twitter
#print out the keys, compare them with what you see here and
#try more! Search for the craziest thing you can think of


Possible ideas using Twitter APIs and a programming language of choice:

  • Make an API call to Twitter with a search query q=[your own city]
  • Try a query with q=:)  – this is an emoticon (sad or happy face)
  • Try your favorite word, and see where it is commonly used.
  • Create a script that makes calls every five minutes and displays the top 20 latest tweets.
  • There are more things to think about, so, go do!

I hope this post helped you learn something new. Next time, I will be doing something else, building upon what I did today. If you have any question, please do not hesitate to ask. Use the comments section and I will be more than happy to help.

Thanks and see you soon!

Be The First To Get New Posts From Simple Developer…

Written By Elisha Chirchir

Elisha Chirchir is a software developer. He is also the founder of Simple Developer and co-founder of Instinctive Software Solutions. On any given day, he works on both Android and Web Development. During his 'free time', he offers training to those interested in learning how to code in php, java, python, javaScript etc. You can easily find him on StackOverflow Android chatroom or on Twitter @Eenvincible

Leave a Reply

Your email address will not be published. Required fields are marked *