Check If Yahoo! ID Is Online Or Not Using Python

Now is almost 1 PM, and I still cannot sleep. Suddenly I get the idea to write something in Python: a script to check whether a Yahoo! ID is online or not.

The basic principle is to use this Yahoo! API:

http://presence.msg.yahoo.com/online?u=<Yahoo! ID>

Now let’s say your Yahoo! ID is john.doe. Open this URL in your browser:

http://presence.msg.yahoo.com/online?u=john.doe

If that ID is online, you’ll see this picture:

Otherwise, you’ll see this:

To sum it up, this is the algo:

  1. Call the URL, and save the result as a file.
  2. Calculate the MD5 sum of the file (of course you are not limited to MD5, you can use any checksum algo)
  3. See the ‘online’ image above? It’s MD5 sum is 9e31b63daa88647ead982d2c7598e634. Now calculate the MD5 sum of the saved file. If both MD5 sums are match, it means that the Yahoo! ID is online. Otherwise, it’s offline.
And this is the Python script.

#!/usr/bin/python2

# A simple script to check whether a Yahoo! ID is online or not
# Usage: python2 ycheck.py
# Example: python2 ycheck.py john.doe

import pycurl
import StringIO
import md5
import sys

ONLINE_VALUE = '9e31b63daa88647ead982d2c7598e634'
buffer = StringIO.StringIO()

# Call the API via curl
# and save the result into file called 'img'
user = sys.argv[1]
curl = pycurl.Curl()
curl.setopt(curl.URL, 'http://presence.msg.yahoo.com/online?u='+user)
curl.setopt(curl.WRITEFUNCTION, buffer.write)
curl.perform()
tmpfile = open('img', 'wb')
tmpfile.write(buffer.getvalue())
tmpfile.close()

# Calculate the MD5 of 'img'
tmpfile = open('img','rb')
md5_result = md5.new(tmpfile.read()).hexdigest()
tmpfile.close()

# Compare it with the predefined value above
if (md5_result == ONLINE_VALUE):
 print'%s is online' % (user)
else:
 print '%s is offline' % (user)

P.S: I make NO claims that the above code is robust/efficient/etc. It’s just a quick n’ dirty script, anyway:mrgreen:

BTW, the above code is only tested with Python 2, because the pycurl available in the GNU/Linux distro I’m using is not compatible with Python 3.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s