Analyse Facebook Friends Geography (en-US/C#) [FOR FUN]

After talking with a friend of mine about our facebook friends, i decided to tell him a more precise information about where are my friends in the globe. In order to give him a response tomorrow i make a simple tool to give me that information.

What i am going to show you here is how i made it and maybe incourage you to check on your friends as well. First thing to do is study the facebook C# API : https://github.com/facebook-csharp-sdk/facebook-csharp-sdk

Technologies used:

  • C#
  • Excel
  • Excel PowerView
  • Facebook C# API

After that, i started developing my mini-tool to connect to facebook. This is very simple and it only took me an hour to make it. This tool is a simple windows form app with a webbrowser and a datagrid. Once you login it will show you your friends info (for those with a valid location) and it will save a csv in you c:\temp folder.

Step 0 – Install the facebook api package in your solution:

NUGET: Install-Package Facebook

Step 1 – Register in Facebook App (https://developers.facebook.com/apps)

image

Step 2 – Start coding like a mad man (please do not take this code only as a reference)

It was actually a windows form app, really ugly but it made my purpose. The form class is here:

public partial class Form1 : Form
{
private string _facebookAppId = "your app id – provided by the facebook app registration";
private string _permissions = "user_about_me,user_location,friends_about_me,friends_location";
private FacebookClient _fb = new FacebookClient();
private List<ContactInfo> listaContactos = new List<ContactInfo>();

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

var redirectUrl = "<a href="https://www.facebook.com/connect/login_success.html&quot;;">https://www.facebook.com/connect/login_success.html";</a>

//fb.AppId = facebookAppId;
var loginUrl = _fb.GetLoginUrl(new
{
client_id = _facebookAppId,
redirect_uri = redirectUrl,
scope = _permissions,
display = "popup",
response_type = "token"
});

webBrowser1.Navigated += (s, ev) => {
FacebookOAuthResult result;
if (_fb.TryParseOAuthCallbackUrl(ev.Url, out result))
{
if (result.IsSuccess)
{
var accesstoken = result.AccessToken;

_fb.AccessToken = accesstoken;

GetAllFriendsInsertToSQL();
}
else
{
var errorDescription = result.ErrorDescription;
var errorReason = result.ErrorReason;
}
}

};

webBrowser1.Navigate(loginUrl);

}

public void GetAllFriendsInsertToSQL()
{

var me = _fb.Get("/me/friends");
var results = JObject.Parse(me.ToString());

foreach(var element in results)
{
var children = element.Value;

foreach (var child in children)
{
if (child.Next != null)
{
var id = child.Value<string>("id");

var name = child.Value<string>("name");

var friend = (Facebook.JsonObject)_fb.Get("/" + id);

object location;

friend.TryGetValue("location", out location);

if (location != null)
{
var loc = (Facebook.JsonObject)location;

object city;

loc.TryGetValue("name", out city);

if (city != null)
{
listaContactos.Add(new ContactInfo { Id = id, Name = name, City = city.ToString() });
}
}
}
}
}

dataGridView1.DataSource = listaContactos;

SaveToCSV();
}

public void SaveToCSV()
{
string file=@"C:\Temp\facebookAPI.csv";

if(System.IO.File.Exists(file)){
System.IO.File.Delete(file);
}

foreach (var item in listaContactos)
{
System.IO.File.AppendAllText(file,string.Format("{0},{1},{2}\n",item.Id,item.Name,item.City),Encoding.UTF8);
}
}
}

Once you run my code, you will find a form that looks like the following:

image

Once you sign in it will automatically start downloading your friends info. Notice in the code that i only save the info of my friends that have a location in its data, because my main goal is to check my friends distribution in the world.

image

Step 3 – Save to file

If you paid some atention to my code you could see that at the end i call a method to save my file as csv in c:\temp\FacebookAPI.csv. Once you have that file, open it in excel.

image

After you open it, add a header with names that identify each attributes, then format as a table.

image

Step 4- Add a pivot chart  – Select the entire table and the in excel select Insert->Pivot Table

image

Now you can start making your own analysis, for instance i have 2 friends in France and 1 in Belgium:

image

Step 5 – My Favourite (PowerView)

First save your file as XLSX

image

Then Go To PowerView Tab in Excel:

image

Now maintain the same attributes selected:

image

And finnaly for the expected result click on DESING –> MAP

image

Watch magic happening:

image

Now i can tell my friend the right answer.

 

By the way, if some one wants the too, ask me (ruimachado@live.com.pt) and i will send it. 

 

Thank you,

Rui Machado

Anúncios

Deixe uma Resposta

Preencha os seus detalhes abaixo ou clique num ícone para iniciar sessão:

Logótipo da WordPress.com

Está a comentar usando a sua conta WordPress.com Terminar Sessão / Alterar )

Imagem do Twitter

Está a comentar usando a sua conta Twitter Terminar Sessão / Alterar )

Facebook photo

Está a comentar usando a sua conta Facebook Terminar Sessão / Alterar )

Google+ photo

Está a comentar usando a sua conta Google+ Terminar Sessão / Alterar )

Connecting to %s