Using the API with Python: SelfProfile

1 post Page 1 of 1
Contributors
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Hello and welcome to the first Python tutorial of the newly created Python subforum! clapper;

The API tutorials posted here are very comprehensive. However, they target VB.NET mainly, so I've decided to post a few tutorials that will help you get the API working with your Python script.

This tutorial in particular (and all others that will focus on the API) assumes basic knowledge of Python and how it works. Don't worry, though, tutorials on Python itself will be coming soon ;)

Now, let's get started:

Start off by launching your favourite Notepad application and saving the file with a .py extension. We won't be messing around with any GUIs, so we'll simply be making a command-line app.

We'll need to use two modules to make this script work: urllib2, which will allow us to make HTTP requests, as well as json, which, as you might have guessed by the name, handles JSON data.

To use the modules, we must import them at the beginning of the script:
Code: Select all
import urllib2
import json
Next, we have to prompt the user for their username and secret key (if you don't know where to get it, look in the top right corner of this page. We'll do this by assigning the result of raw_input instructions to two seperate variables:
Code: Select all
username=raw_input("Enter username:")
skey=raw_input("Enter secret key:")
That's all we need to call the API for this specific mode, so let's move on:
Code: Select all
raw_data=urllib2.urlopen("http://www.codenstuff.com/forum/api/LiveAPIJsonXML.php?mode=SelfProfile&username="+username+"&skey="+skey)
json_data=json.load(raw_data)
This bit of code calls the API, supplying the username and secret ket we collected earlier. It assigns the returned JSON data to a variable, then we load that into a second variable called json_data

The JSON data returned contains a list, and within that list there is a dictionnary of values. In this case, the list contains only one element, so we'll avoid having to write too much code and assign that element to our json_data variable:
Code: Select all
json_data=json_data[0]
The next bit of code is very simple, now that we have all our data loaded, we can assign it to different variables, like so:
Code: Select all
userid=json_data["userid"]
userip=json_data["userip"]
regdate=json_data["regdate"]
email=json_data["email"]
birthday=json_data["birthday"]
lastvisit=json_data["lastvisit"]
lastpost=json_data["lastpost"]
lastpage=json_data["lastpage"]
warnings=json_data["warnings"]
posts=json_data["posts"]
newpm=json_data["newpm"]
unreadpm=json_data["unreadpm"]
avatar=json_data["avatar"]
wherefrom=json_data["from"]
icq=json_data["icq"]
aim=json_data["aim"]
yahoo=json_data["yahoo"]
msn=json_data["msn"]
website=json_data["website"]
occupation=json_data["occupation"]
interests=json_data["interests"]
reputation=json_data["reputation"]
color=json_data["color"]
lotsocredits=json_data["credits"]
The last step is to print everything to the screen. Now, you don't have to do it exactly in the same format as me, but just as an example, here's how I did it:
Code: Select all
print "Hello",username
print "Your user ID is",userid,"and you registered with the following IP address:",userip,"on",regdate+"."
print "You registered with the following email address:",email+"."
print "I see you were born on",birthday+"."
print "The last time you visited the website was on",lastvisit,". You last posted on",lastpost,"and the last page you saw was",lastpage+"."
print "You have received",warnings,"warnings."
print "You have made",posts,"posts."
print "You have",newpm,"new private messages, and",unreadpm,"unread messages."
print "Your avatar is located at the following URL:",avatar+"."
print "Apparently, you are from",wherefrom+". Where the heck is that?"
print "Users can contact you through the following accounts:"
print "-ICQ:",icq
print "-AIM",aim
print "-Yahoo!:",yahoo
print "-MSN:",msn
print "You own the following website:",website
print "This is what you do with your life:",occupation+"."
print "You are interested in things such as these:",interests+"."
print "Your reputation score is",reputation+"."
print "Your username color is",color+"."
print "Finally, you have",lotsocredits,"code credits!"
Which gives me the following text (sensitive information censored):
Image

That's it for now, you can start using some of the site's API's features in your script cooll;
1 post Page 1 of 1
Return to “Tutorials”