private JSONObject FBJsonCheck(JSONObject result) { return null; }
private JSONObject resultCheck(JSONObject result, string url, int limit) { FBGraph api = new FBGraph(); int erro_count = 0; int sleepTime = 30 * 1000; int retryTIme = 10 * 1000; while (true) { if (result != null && result.IsDictionary == false) break; if (result == null || result.Dictionary.ContainsKey("error_msg") || result.Dictionary.ContainsKey("error")) { //若發生error 檢查是何種error 若無error再透過exception離開迴圈 string errorMessage = ""; string errorNumber = ""; if (result == null) { errorNumber = "1"; } else if (result.Dictionary.ContainsKey("error_msg")) { errorMessage = result.Dictionary["error_msg"].String; errorNumber = result.Dictionary["error_code"].String; } else { errorMessage = result.Dictionary["error"].Dictionary["message"].String; errorNumber = result.Dictionary["error"].Dictionary["code"].String; } switch (errorNumber) { case "1": case "2": if (limit <= 25) { messageLog(errorMessage); return null; } limit = limit / 2; if (limit > 0) { url += "&limit=" + limit.ToString(); } Thread.Sleep(retryTIme); break; case "4": messageLog(errorMessage); erro_count++; if (erro_count > 30) { return null; } Thread.Sleep(sleepTime); break; case "602": messageLog(errorMessage); return null; } result = api.FBGraphGet(url); } else { break; } } return result; }
public int get_likes(string pageID, string objectID, string access_token, JSONObject likeJSON, DateTime postCreatedTime, string nextUrl) { int totoalCount = 0; //string url = "https://graph.facebook.com/" + postID + "/comments?access_token=" + access_token + "&limit=500"; string url = "https://graph.facebook.com/" + objectID + "/likes?access_token=" + access_token + "&limit=" + likeLimit.ToString() + "&summary=1"; if (nextUrl.Length > 0) url = nextUrl + "&limit=" + likeLimit.ToString(); likeJSON = new FBGraph().FBGraphGet(url); likeJSON = resultCheck(likeJSON, url, likeLimit); if (likeJSON == null) return totoalCount; //while (true) //{ // if (likeJSON.Dictionary == null || likeJSON.Dictionary.ContainsKey("error") || likeJSON.Dictionary.ContainsKey("error_msg")) // { // string errorMessage = ""; // string errorNumber = ""; // if (likeJSON.Dictionary == null) // { // errorMessage = "An unknown error occurred"; // errorNumber = "1"; // } // else if (likeJSON.Dictionary.ContainsKey("error_msg")) // { // errorMessage = likeJSON.Dictionary["error_msg"].String; // errorNumber = likeJSON.Dictionary["error_code"].String; // } // else // { // errorMessage = likeJSON.Dictionary["error"].Dictionary["message"].String; // errorNumber = likeJSON.Dictionary["error"].Dictionary["code"].String; // } // messageLog(errorMessage); // //showMessage.AppendText("@" + DateTime.Now.ToString() + ":" + errorMessage + "\n"); // if (errorNumber.Equals("4") || errorNumber.Equals("2")) // { // //(#4)為超出limit sleep 30秒後再繼續 // Thread.Sleep(30 * 1000); // likeJSON = new FBGraph().FBGraphGet(url); // } // else // { // return totoalCount; // } // } // else { break; } //} Dictionary<string, string> fansName = new Dictionary<string, string>(); //bool hasNaxt = false; DBConnection conn = new DBConnection(dbInfo); conn.MySqlConnect(); string sql = ""; try { totoalCount = Convert.ToInt32(likeJSON.Dictionary["summary"].Dictionary["total_count"].String); } catch { } if (likeJSON.Dictionary["data"].Array.Length > 0) { foreach (JSONObject ljson in likeJSON.Dictionary["data"].Array) { string fromID = ""; string fromName = ""; if (ljson.Dictionary.ContainsKey("id")) { fromID = ljson.Dictionary["id"].String; if (ljson.Dictionary.ContainsKey("name")) fromName = ljson.Dictionary["name"].String; if (!fansName.ContainsKey(fromID)) { fansName.Add(fromID, fromName); } } else { Console.WriteLine("In Comment fetch exception with id & name\n"); messageLog("In Comment fetch exception with id & name\n"); continue; } } if (fansName.Count > 0) { insert_pages_fans(fansName, pageID); insert_posts_likes(fansName, pageID, objectID, postCreatedTime); } conn.closeMySqlConnection(); //string after = ""; if (likeJSON.Dictionary.ContainsKey("paging")) { if (likeJSON.Dictionary["paging"].Dictionary.ContainsKey("next")) { url = likeJSON.Dictionary["paging"].Dictionary["next"].String; get_likes(pageID, objectID, access_token, null, postCreatedTime, url); } //hasNaxt = true; } } return totoalCount; }
public int get_post_comment(string pageID, string postID, string access_token, JSONObject commJSON, DateTime postCreatedTime, string nextUrl) { int totoalCount = 0; string url = "https://graph.facebook.com/" + postID + "/comments?access_token=" + access_token + "&limit=" + commentLimit.ToString() + "&summary=1"; if (nextUrl.Length > 0) url = nextUrl + "&limit=" + commentLimit.ToString(); commJSON = new FBGraph().FBGraphGet(url); commJSON = resultCheck(commJSON, url, commentLimit); if (commJSON == null) return totoalCount; //while (true) //{ // if (commJSON.Dictionary == null || commJSON.Dictionary.ContainsKey("error") || commJSON.Dictionary.ContainsKey("error_msg")) // { // string errorMessage = ""; // string errorNumber = ""; // if (commJSON.Dictionary == null) // { // errorMessage = "An unknown error occurred"; // errorNumber = "1"; // } // else if (commJSON.Dictionary.ContainsKey("error_msg")) // { // errorMessage = commJSON.Dictionary["error_msg"].String; // errorNumber = commJSON.Dictionary["error_code"].String; // } // else // { // errorMessage = commJSON.Dictionary["error"].Dictionary["message"].String; // errorNumber = commJSON.Dictionary["error"].Dictionary["code"].String; // } // messageLog(errorMessage); // //showMessage.AppendText("@" + DateTime.Now.ToString() + ":" + errorMessage + "\n"); // if (errorNumber.Equals("4") || errorNumber.Equals("2")) // { // //(#4)為超出limit sleep 30秒後再繼續 // Thread.Sleep(30 * 1000); // commJSON = new FBGraph().FBGraphGet(url); // } // else // { // return totoalCount; // } // } // else { break; } //} Dictionary<string, string> fansName = new Dictionary<string, string>(); //bool hasNaxt = false; DBConnection conn = new DBConnection(dbInfo); conn.MySqlConnect(); string sql = ""; try { totoalCount = Convert.ToInt32(commJSON.Dictionary["summary"].Dictionary["total_count"].String); } catch { } if (commJSON.Dictionary["data"].Array.Length > 0) { foreach (JSONObject comment in commJSON.Dictionary["data"].Array) { string fromID = ""; string fromName = ""; string commID = ""; string message = ""; string likes = "0"; //----------------// int datatype=2; string picture=" "; string link=" "; string name=" "; string caption=" "; string icon=" "; string type=" "; string object_id=" "; string description=" "; string source=" "; int comments=0; int shares=0; DateTime created_time = DateTime.Now; if (comment.Dictionary.ContainsKey("id")) { commID = comment.Dictionary["id"].String; } else { Console.WriteLine("In Comment fetch exception with comment id\n"); continue; } if (comment.Dictionary.ContainsKey("from")) { if (comment.Dictionary["from"].Dictionary.ContainsKey("id")) fromID = comment.Dictionary["from"].Dictionary["id"].String; if (comment.Dictionary["from"].Dictionary.ContainsKey("name")) fromName = comment.Dictionary["from"].Dictionary["name"].String; if (!fansName.ContainsKey(fromID)) { fansName.Add(fromID, fromName); } } else { Console.WriteLine("In Comment fetch exception with id & name\n"); continue; } if (comment.Dictionary.ContainsKey("message")) { message = comment.Dictionary["message"].String; } else { Console.WriteLine("In Comment fetch exception with comment message\n"); } if (comment.Dictionary.ContainsKey("like_count")) { likes = comment.Dictionary["like_count"].String; } if (comment.Dictionary.ContainsKey("created_time")) { created_time = fbDatetimeToDatetime(comment.Dictionary["created_time"].String); } else { Console.WriteLine("In Comment fetch exception with comment created_time \n"); } /* sql = "insert ignore into pages_posts_comments (comment_id,page_id,post_id,from_id,from_name,message,created_time,post_created_time,likes,updatetime) values (" + "?comment_id,?page_id,?post_id,?from_id,?from_name,?message,?created_time,?post_created_time,?likes,?updatetime) " + "ON DUPLICATE KEY UPDATE likes=?likes,updatetime=?updatetime";*/ sql = "insert into allinone (post_id,datatype,author_id,author,message,picture,url,name,caption,source,icon,type,object_id,description,likes,created_time,updated_time,comments,shares,updatetime) values (" + "?post_id,?datatype,?from_id,?from_name,?message,?picture,?link,?name,?caption,?source,?icon,?type,?object_id,?description,?likes,?created_time,?update_time,?comments,?shares,?updatetime)" ; /* while (true) { try { MySqlCommand cmd = new MySqlCommand(sql, conn.conn); cmd.Parameters.AddWithValue("?comment_id", commID); cmd.Parameters.AddWithValue("?page_id", pageID); cmd.Parameters.AddWithValue("?post_id", postID); cmd.Parameters.AddWithValue("?from_id", fromID); cmd.Parameters.AddWithValue("?from_name", fromName); cmd.Parameters.AddWithValue("?message", message); cmd.Parameters.AddWithValue("?created_time", created_time); cmd.Parameters.AddWithValue("?post_created_time", postCreatedTime); cmd.Parameters.AddWithValue("?likes", likes); cmd.Parameters.AddWithValue("?updatetime", DateTime.Now); cmd.ExecuteReader().Close(); //totoalCount++; break; } catch (Exception ce) { Console.WriteLine(ce.StackTrace); conn.closeMySqlConnection(); conn.MySqlConnect(); } }*/ while (true) { try { MySqlCommand cmd = new MySqlCommand(sql, conn.conn); cmd.Parameters.AddWithValue("?post_id", commID); // cmd.Parameters.AddWithValue("?page_id", pageID); cmd.Parameters.AddWithValue("?datatype", datatype); cmd.Parameters.AddWithValue("?from_id", fromID); cmd.Parameters.AddWithValue("?from_name", fromName); cmd.Parameters.AddWithValue("?message", message); cmd.Parameters.AddWithValue("?picture", picture); cmd.Parameters.AddWithValue("?link", link); cmd.Parameters.AddWithValue("?name", name); cmd.Parameters.AddWithValue("?caption", caption); cmd.Parameters.AddWithValue("?source", source); cmd.Parameters.AddWithValue("?icon", icon); cmd.Parameters.AddWithValue("?type", type); cmd.Parameters.AddWithValue("?object_id", object_id); cmd.Parameters.AddWithValue("?description", description); cmd.Parameters.AddWithValue("?likes", likes); cmd.Parameters.AddWithValue("?created_time", postCreatedTime); cmd.Parameters.AddWithValue("?update_time", DateTime.Now); cmd.Parameters.AddWithValue("?comments", comments); cmd.Parameters.AddWithValue("?shares", shares); cmd.Parameters.AddWithValue("?updatetime", DateTime.Now); // cmd.Parameters.AddWithValue("?post_created_time", postCreatedTime); cmd.ExecuteReader().Close(); //totoalCount++; break; } catch (Exception ce) { Console.WriteLine(ce.StackTrace); conn.closeMySqlConnection(); conn.MySqlConnect(); } } } if (fansName.Count > 0) { insert_pages_fans(fansName, pageID); } //string after = ""; conn.closeMySqlConnection(); if (commJSON.Dictionary.ContainsKey("paging")) { if (commJSON.Dictionary["paging"].Dictionary.ContainsKey("next")) { url = commJSON.Dictionary["paging"].Dictionary["next"].String; get_post_comment(pageID, postID, access_token, null, postCreatedTime, url); } //hasNaxt = true; } } else { conn.closeMySqlConnection(); } return totoalCount; }
private static void RecursiveDictionaryToString(JSONObject obj, StringBuilder sb, int level) { foreach (KeyValuePair<string, JSONObject> kvp in obj.Dictionary) { sb.Append(' ', level * 2); sb.Append(kvp.Key); sb.Append(" => "); RecursiveObjectToString(kvp.Value, sb, level); sb.AppendLine(); } }
private static void RecursiveObjectToString(JSONObject obj, StringBuilder sb, int level) { if (obj.IsDictionary) { sb.AppendLine(); RecursiveDictionaryToString(obj, sb, level + 1); } else if (obj.IsArray) { foreach (JSONObject o in obj.Array) { RecursiveObjectToString(o, sb, level); sb.AppendLine(); } } else // some sort of scalar value { sb.Append(obj.String); } }
/// <summary> /// Recursively constructs this JSONObject /// </summary> private static JSONObject Create(object o) { JSONObject obj = new JSONObject(); if (o is object[]) { object[] objArray = o as object[]; obj._arrayData = new JSONObject[objArray.Length]; for (int i = 0; i < obj._arrayData.Length; ++i) { obj._arrayData[i] = Create(objArray[i]); } } else if (o is Dictionary<string, object>) { obj._dictData = new Dictionary<string, JSONObject>(); Dictionary<string, object> dict = o as Dictionary<string, object>; foreach (string key in dict.Keys) { obj._dictData[key] = Create(dict[key]); } } else if (o != null) // o is a scalar { obj._stringData = o.ToString(); } return obj; }