Created
March 10, 2015 09:10
-
-
Save weiland/aaf02ba6ad6092b20cf4 to your computer and use it in GitHub Desktop.
fb controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using Newtonsoft.Json; | |
namespace dlitfass.Data | |
{ | |
public class FacebookController | |
{ | |
private string graphUrl; | |
private string accessToken; | |
System.Net.WebClient wc; | |
public FacebookController(string pAccessToken = null) | |
{ | |
graphUrl = Global.FbGraphUrl; | |
accessToken = Global.FbAccessToken; | |
if (pAccessToken != null) | |
{ | |
accessToken = pAccessToken; | |
} | |
// initialize WebClient | |
wc = new System.Net.WebClient(); | |
} | |
public string GetBiggestImageByPostFBID(string fbid) | |
{ | |
string objectID = fbidRequest(fbid, "object_id"); // Get ObjectID by FBID | |
return getBiggestImage(objectID); | |
} | |
public string GetBiggestImageByPhotoFBID(string fbid) | |
{ | |
//string objectID = fbidRequest(fbid, "object_id"); // Get ObjectID by FBID | |
return getBiggestImage(fbid); | |
} | |
public string GetBiggestImageByEventFBID(string fbid) | |
{ | |
string coverID = fbidRequest(fbid, "cover", "cover_id"); // Get ObjectID by FBID | |
return getBiggestImage(coverID); | |
} | |
public List<String> GetImagesFromAlbum(string fbid) | |
{ | |
string request = fbid + "?fields=photos.fields(images,source,picture,link,name).limit(100)"; | |
string result = apiRequest(request); | |
List<String> retval = new List<string>(); | |
if (result == null) | |
{ | |
return null; | |
} | |
try | |
{ | |
var json = JsonConvert.DeserializeObject<dynamic>(result); | |
var data = JsonConvert.DeserializeObject<dynamic[]>(json["photos"]["data"].ToString()); | |
int i = 0; | |
while (true) | |
{ | |
if (data[i] != null) | |
{ | |
retval.Add(data[i]["images"][0]["source"].ToString()); | |
i++; | |
} else | |
{ | |
break; | |
} | |
} | |
} | |
catch (Exception e) | |
{ | |
Help.Diagnostics.WriteToLogFile(e.ToString(), 1001); | |
} | |
return retval; | |
} | |
#region private methods (helpers) | |
//private void Log(string text) | |
//{ | |
// Help.Diagnostics.WriteStaticInfoToLogFile(text, @"D:\dev\projects\git\dlitfass\dlitfass.Web\test.txt"); | |
// //TappDiag.Log.Diagnostic.WriteInfoLog("1001", "FacebookController->GetImagesFromAlbum", "test "); | |
// //Help.Diagnostics.WriteStaticInfoToLogFile("test3", @"\\chayns1\SlitteRessource\Log\dlitfassContent\26-1-2015-Error.log"); | |
//} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="fbid"></param> | |
/// <param name="property"></param> | |
/// <returns></returns> | |
private string fbidRequest(string fbid, string property, string selectVal = null) | |
{ | |
//string request = fbid + "?fields=" + property; | |
if (property == "cover") | |
fbid = fbid + "?fields=cover"; | |
string result = apiRequest(fbid); | |
if (result == null) | |
{ | |
return null; | |
} | |
var json = JsonConvert.DeserializeObject<dynamic>(result); | |
if (selectVal != null) | |
{ | |
if (json[property] != null) | |
return json[property][selectVal]; | |
return null; | |
} | |
return json[property]; | |
} | |
private string getBiggestImage(string fbid) | |
{ | |
//string request = fbid + "?fields=images"; | |
if (fbid == null) return null; | |
string result = apiRequest(fbid); | |
if (result == null) | |
{ | |
return null; | |
} | |
var json = JsonConvert.DeserializeObject<dynamic>(result); | |
var data = JsonConvert.DeserializeObject<dynamic[]>(json["images"].ToString()); | |
return data[0]["source"]; // TODO is the first image always the biggest? (actually yes) | |
} | |
/// <summary> | |
/// performs the Facebook API requests | |
/// adds graph uri and access token automatically | |
/// </summary> | |
/// <param name="endpoint">endpount uri (without https and host) and GET Query String (and without token)</param> | |
/// <returns>null on error otherwise result string</returns> | |
private string apiRequest(string endpoint) | |
{ | |
if(endpoint == null) | |
{ | |
return null; | |
} | |
string resultString; // TODO is "" == null ? | |
try | |
{ | |
string stringDelimeter = (endpoint.IndexOf("?") > -1) ? "&" : "?"; | |
string accessTokenQueryString = stringDelimeter + "access_token=" + accessToken; | |
string requestUri = graphUrl + endpoint + accessTokenQueryString; | |
resultString = wc.DownloadString(requestUri); | |
return resultString; | |
} | |
catch (Exception e) | |
{ | |
// TODO write Error Log(s) | |
// but not here cause here the fb token is most likely expired | |
return null; | |
} | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment