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 ="http://search.twitter.com/search.json?q=nbcbetty" 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!" % (parsed_data['results'][i]['from_user_name'], parsed_data['results'][i]['text'], parsed_data['results'][i]['created_at']) 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 #Rockers
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! http://t.co/WmaGmKuj #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) http://t.co/esqNxT11 #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 = 'http://search.twitter.com/search.json?q=nbcbetty' #q=karma ?? parsed_data = '' try: 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 raise 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!" % (parsed_data['results'][i]['from_user_name'], parsed_data['results'][i]['text'], parsed_data['results'][i]['created_at']) 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!