/// <summary> /// Download profile pictures of the users /// </summary> /// <param name="friends">Friend list</param> /// <param name="dir">Destination directory</param> /// <param name="worker">To report progress</param> private static void DownloadPictures(FriendList friends, string dir, BackgroundWorker worker = null) { int i = 0; foreach (Friend f in friends) { if (worker != null && worker.CancellationPending) { throw new InterruptedException(); } if (File.Exists(dir + "/" + f.id + ".jpg")) { if (worker != null) { worker.ReportProgress((++i * 100) / friends.Count, i); } continue; } string url = GraphAPI.GetData(f.id + "?fields=picture").SelectToken("picture") .SelectToken("data") .SelectToken("url") .ToString(); DownloadFile(url, dir + "/" + f.id + ".jpg"); if (worker != null) { worker.ReportProgress((++i * 100) / friends.Count, i); } } }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); // first, we show the login dialog Application.Run(new LoginForm()); // if we haven't managed to retrieve the access token (user didnt sing in or didnt allow the app) // we have nothing to do if (Config.ACCESS_TOKEN == null || Config.ACCESS_TOKEN.Length == 0) { return; } // if we have, we find info about the signed user Friend user = null; try { user = GraphAPI.GetData <Friend>("me?fields=id,name"); } catch (GraphAPIException) { MessageBox.Show("Couldn't authorize app with your account. Either you didn't log in successfully or you haven't confirmed the app request."); return; } Config.USER_ID = user.id; Config.USER_NAME = user.name; // Firing the main window (with settings) Application.Run(new MainForm()); }
/// <summary> /// Main generating method which prepares all the data and generates the graph. /// </summary> /// <param name="worker">For reporting progress outside as this method runs a loong time (due to downloading)</param> public static void GenerateGraph(BackgroundWorker worker = null) { // load friendlist of our user FriendList friends = GraphAPI.GetData <FriendList>(Config.USER_ID + "/friends"); // report that loading was fine worker.ReportProgress(0, friends.Count); // make sure the report made it through and updated GUI // this is necessary because the System.Threading.Thread.Yield(); // create destination directories if (!File.Exists("cache")) { System.IO.Directory.CreateDirectory("cache"); } string dir = "cache/" + Config.USER_ID; if (!File.Exists(dir)) { System.IO.Directory.CreateDirectory(dir); } if (!File.Exists(dir + "/photos")) { System.IO.Directory.CreateDirectory(dir + "/photos"); } // load all mutual friends and the whole graph Graph graph = new Graph(); // if we had loaded the friend information earlier we dont need to redownload it if (File.Exists(dir + "/data.json")) { graph.LoadConnections(dir + "/data.json", worker); } else { graph.LoadConnections(friends, worker); graph.SaveConnections(dir + "/data.json"); } // download profile pictures of the users DownloadPictures(friends, dir + "/photos", worker); // now we generate the graph (vertices positions) // gets map of type: Person -> (x,y) graph.Calculate(worker); // and we draw it to a file Bitmap bmp = DrawGraph(graph.Data, dir, worker); bmp.Save(dir + "/graph.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); bmp.Dispose(); worker.ReportProgress(100, dir + "/graph.jpg"); }
/// <summary> /// Loads all mutual friends of all friends and saves them into a graph /// </summary> public void LoadConnections(FriendList fl, BackgroundWorker worker = null) { data = new FriendGraph(); friends = new Dictionary <string, Friend>(); // creates id->friend addresser foreach (Friend f in fl) { friends.Add(f.id, f); data.Add(f, new List <Friend>()); } // downloades mutual friends of every friend in the list int i = 0; foreach (KeyValuePair <string, Friend> pair in friends) { if (worker != null && worker.CancellationPending) { throw new InterruptedException(); } FriendList mutualFriends = GraphAPI.GetData <FriendList>(pair.Key + "/mutualfriends"); foreach (Friend f in mutualFriends) { if (!data[pair.Value].Contains(f)) { data[pair.Value].Add(f); } } // reporting progress if (worker != null) { worker.ReportProgress((++i * 100) / friends.Count, i); } } }
/// <summary> /// Alias for GetData<T> when no need for a parsing into a specific class. /// </summary> /// <param name="query">Facebook Graph API command (URL)</param> /// <param name="addToken">Should we add the user access_token to the request?</param> /// <returns>Parsed recieved data as a object. Can be cast to JObject.</returns> static public JObject GetData(string query, bool addToken = true) { return(GraphAPI.GetData <JObject>(query, addToken)); }