4

I was bored, and I wanted to play with the API, so I came up with this little script:

It's coded in Python and has only a single image dependency.

Here is a screenshot:

Screenshot

Here's the source code (it's tiny, so I have no reason to host it on an external site):

import urllib, gzip, cStringIO, simplejson, pynotify, gtk

url = 'http://api.stackoverflow.com/1.0/users/464744' # I would change this, unless you want to monitor my account ;)
connection = urllib.urlopen(url)
response = connection.read()
connection.close()

info = simplejson.load(gzip.GzipFile(fileobj=cStringIO.StringIO(response)))['users'][0]

pynotify.init("PyOverflow")

message = pynotify.Notification("Stack Overflow", "Reputation: " + str(info['reputation']) + "\n" +
                                                  "Number of questions: " + str(info['question_count']) + "\n" +
                                                  "Number of answers: " + str(info['answer_count']) + "\n" +
                                                  "Number of up votes: " + str(info['up_vote_count']) + "\n" +
                                                  "Number of down votes: " + str(info['down_vote_count']) + "\n" +
                                                  "Accept rate: " + str(info['accept_rate']) + "%")

message.set_icon_from_pixbuf(gtk.gdk.pixbuf_new_from_file('icon.png'))
message.show()

And here's the image the script would be looking for:

The icon

As it took me a while to figure out the JSON and API part, I might make a Python API for this, just for fun.

Any suggestions are welcome (don't worry, I will make this thing a bit more versatile)!

5
  • 2
    You can pull the logo dynamically as icon_url via stackauth.com/1.0/sites (though you should cache it). Commented Dec 1, 2010 at 6:57
  • Thanks! I'll try that, but I'm not sure if I can pass it as an argument directly into the script. I think I'll have to download it first...
    – Blender
    Commented Dec 1, 2010 at 15:09
  • 2
    Try taking a look at StackApplet to see how I implemented image caching in Python using the API. Commented Dec 2, 2010 at 1:42
  • Dumb question but why do you need this ... gzip.GzipFile(fileobj=cStringIO.StringIO(response))
    – PhilGo20
    Commented Jan 15, 2011 at 20:39
  • It's a bit hard to explain, but the response is sent compressed. It's compressed with Gzip, and a quick one-liner is to treat the output as a pseudo-file and "unzip" it. It took me a while to figure out...
    – Blender
    Commented Jan 17, 2011 at 5:04

0

You must log in to answer this question.

Browse other questions tagged .