Click here to Skip to main content
15,881,876 members
Articles / Programming Languages / C#
Article

GeoLocation for your Skype

Rate me:
Please Sign up or sign in to vote.
4.77/5 (10 votes)
30 Mar 20073 min read 50.4K   700   60   3
The article describes the retrieval of one's public IP address and geo location and possibly assigning it to one's Skype profile.

Screenshot - SkypeGeoLocation.png

Introduction

It's a joy to work in a multinational company ...

As some of our friends here at work, you may choose to reveal your current location to your Skype buddies. This is nice and easy since you can just put it in your name or description and everyone will see where you are. This poses a problem should you travel frequently; it is more likely than not that your location tag will be out of sync.

As this is a something that happens to them once in a while I found it an interesting concept; fun enough to make it worth solving it.

So how does one establish his location? Short of installing a GPS on your machine, I would suggest checking your IP address and translating it based on one of the available databases.

The thought process goes as follows.

IP address discovery

First things first, how do I establish my IP address? This seems to be trivial enough at first glance, query your network adapters and get the IP, right? But more likely than not, you're behind a NAT, and addresses like 10.0.0.2 or 192.168.0.2 (being private addresses) do not help the slightest bit.

So we need to see what the address is as visible from the net. Interesting enough there is more than one service to tell you, but probably the easiest to consume seems to be http://whatismyip.org/

C#
public static string WhatIsMyIp()
{
    // Create a new 'Uri' object with the specified string.
    Uri myUri = new Uri("<a href="http://www.whatismyip.org/">http://www.whatismyip.org/</a>");

// Create a new request to the above mentioned URL.    
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(myUri);
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; 
    SLCC1; .NET CLR 2.0.50727)";

using (WebResponse webResponse = webRequest.GetResponse())
{
    using (StreamReader reader  = new StreamReader(
        webResponse.GetResponseStream()))
    {
        // the response is actually the ip address the site is seeing
        return reader.ReadLine();
    }
}

return string.Empty;
}

Seems to get the job done.

Now the IP to Geo-location translation

Oddly enough there are few services doing anything like that. Initially I've used the MaxMind's GeoIP Lite City , but pushing 10MB of a database to everyone wanting to use it is not really a nice thing to do. It might be interesting solution for you, and I would encourage you to check it out if distribution file size is not a concern.

I started to look around some more, and interesting enough, found a service that turned out just perfect. GeoIP Tool.

Not only does it auto-detect the IP but it will also mesh it with it's geographical location database. The existing code got quickly replaced by a simple:

C#
public static GeoLocation WhatIsMyGeoLocation()
{
    // Create a new request to the geoiptool.com.
    // You can also retrieve the location 
    // for an arbitrary ip by providing the IP= parametetr
    HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(
        new Uri("<a href="http://geoiptool.com/data.php">http://geoiptool.com/data.php</a>"));
myWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0;
     SLCC1; .NET CLR 2.0.50727)";

//fetch the document 
using(HttpWebResponse webResponse = (HttpWebResponse)(
    myWebRequest.GetResponse()))
{
    using (StreamReader reader = new StreamReader(
        webResponse.GetResponseStream()))
    {

        // parsing time
        XmlDocument document = new XmlDocument();
        document.Load(reader);

        // the marker is not a root so I suppose it is probable that 
        // it could support more than a single location for a given IP
        XmlNodeList nodes = document.GetElementsByTagName("marker");

        if (nodes.Count > 0)
        {
            // let's just assume that the first address 
            // is the most likely one to use.
            XmlElement marker = nodes[0] as XmlElement;

            if (marker != null)
            {
                // create an instance of our location class with the
                // data from the server response
                GeoLocation response = new GeoLocation();
                response.city = marker.GetAttribute("city");
                response.country = marker.GetAttribute("country");
                response.code = marker.GetAttribute("code");
                response.host = marker.GetAttribute("host");
                response.ip = marker.GetAttribute("ip");
                response.latitude = marker.GetAttribute("lat");
                response.lognitude = marker.GetAttribute("lng");
                return response;
            }
        }
    }
}

// this code would only be reached if something went wrong 
// no "marker" node perhaps?
return null;
}

The Skype part

This part is fairly easy and straightforward, all you really need to do is to install Skype (make sure you install the latest version, even some earlier 3.0 versions do not seem to make the app work 100% if at all) and import it's COM interface into Visual Studio. Then all you need is available through: Interop.SKYPE4COMLib to consume it.

You may want to consult the Skype API, but I found it pretty easy to get into even without any really extensive reading done.

C#
using SKYPE4COMLib;

namespace SkypeGeoLocation
{
public partial class MainForm : Form
{
    private static Skype skype = new SkypeClass();

    ...

        public static string SkypeName{
            get
            {
                return skype.CurrentUserProfile.FullName;
            }
            set {
                if (skype.CurrentUserProfile.FullName!= value)
                {
                    skype.CurrentUserProfile.FullName = value;
                }
            }
        }

        public static string SkypeDescription{
            get
            {
                return skype.CurrentUserProfile.MoodText;
            }
            set
            {
                if (skype.CurrentUserProfile.MoodText != value)
                {
                    skype.CurrentUserProfile.MoodText = value;
                }
            }
        }

        // ...

        public static void InitializeStructures(bool retryFailedRequests)
        {
            // ...

            // initialize skype
            if (!skype.Client.IsRunning)
            {
                skype.Client.Start(true, true);
            }

        }
}

The Convergence - The Application

The application helps you maintain your location tag in your name so that whenever you travel to a different town your Skype name/description will reflect it.

Disclaimer: The application uses GeoIP Tool as its source of IP and geo-location. Visit their site for a wide variety of web based geo-location tools.

It does not contain any kind of tracking abilities other than it letting you to maintain your location tag. The application will not even change your location tag by itself but rather it will notify you that your location has changed and will allow you to change your tag with a simple press of a button.

Usage

In your Skype account set your name or description so that it has [] in it or press "Add location tab to..." in the application. Whenever the application will find that it will check if the text in the parenthesis matches your current location. If it does not it will suggest a new one and will highlight the relevant field red. You may set for this operation to be performed on every boot, you will however, only be notified when/if your location actually changed.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Poland Poland
Adam Najmanowicz is a Senior Developer at Cognifide Poland.

His previous experience includes Delphi, .Net as well as Java Development.

Currently working on ASP.NET + MSSql Server(Oracle) solutions for enterprises.

Also developing desktop applications for Stardock Systems.

Comments and Discussions

 
Generalimage Pin
noname-20131-Jan-08 7:24
noname-20131-Jan-08 7:24 
Generalgood idea Pin
wadeblack23-Jul-07 15:23
wadeblack23-Jul-07 15:23 
GeneralBug Pin
bwaide2-Apr-07 1:41
bwaide2-Apr-07 1:41 
That's nice stuff, exactly what I need. But there is a critical bug that prevents the application from working at all: In the class GeoLocation, method whatIsMyGeoLocation() you've forgotten to return the location structure, so the return value is null. Ever. Wink | ;-)
Just add a simple "return response;" and everything works like a charm!

Regards, Björn

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.