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; }
/// <summary> /// return with id,name,picture,link,category,website,founded,likes,talking_about_count /// </summary> #region insert post public void get_post(string pageID, string access_token, string since, string until) { //until = "2014-06-08"; int limit = maxLimit; string url = "https://graph.facebook.com/" + pageID + "/feed?access_token=" + access_token + "&limit=" + limit.ToString(); if (since != null) { url += "&since=" + since; } if (until != null) { url += "&until=" + until; } //showMessage.AppendText(url + "\n"); FBGraph api = new FBGraph(); JSONObject fbJSON = api.FBGraphGet(url); do { fbJSON = resultCheck(fbJSON, url, limit); //while (true) //{ // if (fbJSON.Dictionary == null || fbJSON.Dictionary.ContainsKey("error_msg") || fbJSON.Dictionary.ContainsKey("error")) // { // string errorMessage = ""; // string errorNumber = ""; // if (fbJSON.Dictionary == null) // { // errorMessage = "An unknown error occurred"; // errorNumber = "1"; // } // else if (fbJSON.Dictionary.ContainsKey("error_msg")) // { // errorMessage = fbJSON.Dictionary["error_msg"].String; // errorNumber = fbJSON.Dictionary["error_code"].String; // } // else // { // errorMessage = fbJSON.Dictionary["error"].Dictionary["message"].String; // errorNumber = fbJSON.Dictionary["error"].Dictionary["code"].String; // } // messageLog(errorMessage); // //showMessage.AppendText("@" + DateTime.Now.ToString() + ":" + errorMessage + "\n"); // if (errorNumber.Equals("4")) // { // //(#4)為超出limit sleep 10秒後再繼續 // Thread.Sleep(10 * 1000); // fbJSON = api.FBGraphGet(url); // } // else if (errorNumber.Equals("1")) // { // if (limit <= 25) // { // return; // } // limit = limit / 2; // if (limit <= 25) // limit = 25; // url = "https://graph.facebook.com/" + pageID + "/feed?access_token=" + access_token + "&limit=" + limit.ToString(); // if (since != null) // { // url += "&since=" + since; // } // if (until != null) // { // url += "&until=" + until; // } // Thread.Sleep(10 * 1000); // fbJSON = api.FBGraphGet(url); // } // else // { // return; // } // } // else // { // break; // } // //try // //{ // // string errorMessage = fbJSON.Dictionary["error_msg"].String; // // string errorNumber = fbJSON.Dictionary["error_code"].String; // // messageLog(errorMessage); // // //showMessage.AppendText("@" + DateTime.Now.ToString() + ":" + errorMessage + "\n"); // // if (errorNumber.Equals("4")) // // { // // //(#4)為超出limit sleep 10秒後再繼續 // // Thread.Sleep(10 * 1000); // // fbJSON = api.FBGraphGet(url); // // } // // else if (errorNumber.Equals("1")) // // { // // if (limit <= 25) // // { // // return; // // } // // limit = limit / 2; // // if (limit <= 25) // // limit = 25; // // url = "https://graph.facebook.com/" + pageID + "/feed?access_token=" + access_token + "&limit=" + limit.ToString(); // // if (since != null) // // { // // url += "&since=" + since; // // } // // if (until != null) // // { // // url += "&until=" + until; // // } // // Thread.Sleep(10 * 1000); // // fbJSON = api.FBGraphGet(url); // // } // // else // // { // // return; // // } // //} // //catch { break; } //} DBConnection conn = new DBConnection(dbInfo); conn.MySqlConnect(); try { foreach (JSONObject post in fbJSON.Dictionary["data"].Array) { string postID = " "; string fromID = " "; string fromName = " "; string message = " "; string picture = " "; string link = " "; string name = " "; string caption = " "; string source = " "; string icon = " "; string type = " "; string objectID = " "; string description = " "; string likes = "0"; string comments = "0"; string shares = "0"; DateTime postCreatedTime = DateTime.Now; DateTime postUpdateTime = DateTime.Now; //--------------------------// int datatype=1; if (post.Dictionary.ContainsKey("id")) { postID = post.Dictionary["id"].String; } if (post.Dictionary.ContainsKey("created_time")) { postCreatedTime = fbDatetimeToDatetime(post.Dictionary["created_time"].String); } if (post.Dictionary.ContainsKey("updated_time")) { postUpdateTime = fbDatetimeToDatetime(post.Dictionary["updated_time"].String); } if (post.Dictionary.ContainsKey("from")) { if (post.Dictionary["from"].Dictionary.ContainsKey("id")) fromID = post.Dictionary["from"].Dictionary["id"].String; if (post.Dictionary["from"].Dictionary.ContainsKey("name")) fromName = post.Dictionary["from"].Dictionary["name"].String; } if (post.Dictionary.ContainsKey("message")) { message = post.Dictionary["message"].String; } if (post.Dictionary.ContainsKey("picture")) { picture = post.Dictionary["picture"].String; } if (post.Dictionary.ContainsKey("link")) { link = post.Dictionary["link"].String; } if (post.Dictionary.ContainsKey("name")) { name = post.Dictionary["name"].String; } if (post.Dictionary.ContainsKey("caption")) { caption = post.Dictionary["caption"].String; } if (post.Dictionary.ContainsKey("source")) { source = post.Dictionary["source"].String; } if (post.Dictionary.ContainsKey("icon")) { icon = post.Dictionary["icon"].String; } if (post.Dictionary.ContainsKey("type")) { type = post.Dictionary["type"].String; } if (post.Dictionary.ContainsKey("object_id")) { objectID = post.Dictionary["object_id"].String; } if (post.Dictionary.ContainsKey("description")) { description = post.Dictionary["description"].String; } if (post.Dictionary.ContainsKey("likes")) { likes = get_likes(pageID, postID, access_token, post.Dictionary["likes"], postCreatedTime, "").ToString(); } if (post.Dictionary.ContainsKey("comments")) { comments = get_post_comment(pageID, postID, access_token, post.Dictionary["comments"], postCreatedTime, "").ToString(); } if (post.Dictionary.ContainsKey("share")) { shares = post.Dictionary["shares"].Dictionary["count"].String; } string 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 (" + "\"" + postID + "\",\"" + datatype + "\",\"" + fromID + "\",?fname,?message,?picture,?link,?name,?caption,?source,?icon,?type,?object_id,?description," + likes + ",?created_time,?update_time," + comments + "," + shares + ",?updatetime) " + "ON DUPLICATE KEY UPDATE message =?message, likes=" + likes + ",comments=" + comments + ",shares=" + shares + ",updated_time=?update_time,updatetime=?updatetime"; //showMessage.AppendText(sql + "\n"); for (int loop = 0; loop < 2; loop++) { try { MySqlCommand thecmd = new MySqlCommand(sql, conn.conn); thecmd.Parameters.AddWithValue("?fname", fromName); thecmd.Parameters.AddWithValue("?message", message); thecmd.Parameters.AddWithValue("?picture", picture); thecmd.Parameters.AddWithValue("?link", link); thecmd.Parameters.AddWithValue("?name", name); thecmd.Parameters.AddWithValue("?caption", caption); thecmd.Parameters.AddWithValue("?source", source); thecmd.Parameters.AddWithValue("?icon", icon); thecmd.Parameters.AddWithValue("?type", type); thecmd.Parameters.AddWithValue("?object_id", objectID); thecmd.Parameters.AddWithValue("?description", description); thecmd.Parameters.AddWithValue("?created_time", postCreatedTime); thecmd.Parameters.AddWithValue("?update_time", postUpdateTime); thecmd.Parameters.AddWithValue("?updatetime", DateTime.Now); MySqlDataReader themyData = thecmd.ExecuteReader(); themyData.Close(); thecmd.Parameters.Clear(); break; //showMessage.AppendText("Insert complete!!\n"); } catch (Exception sqlE) { Console.WriteLine(sqlE.StackTrace); conn.closeMySqlConnection(); conn.MySqlConnect(); } } } } catch { break; } conn.closeMySqlConnection(); //get next page data try { url = fbJSON.Dictionary["paging"].Dictionary["next"].String; } catch { return; } if (since != null) url += "&since=" + since; url += "&limit=" + limit.ToString(); fbJSON = api.FBGraphGet(url); fbJSON = resultCheck(fbJSON, url, limit); //while (true) //{ // if (fbJSON.Dictionary == null || fbJSON.Dictionary.ContainsKey("error") || fbJSON.Dictionary.ContainsKey("error_msg")) // { // string errorMessage = ""; // string errorNumber = ""; // if (fbJSON.Dictionary == null) // { // errorMessage = "An unknown error occurred"; // errorNumber = "1"; // } // else if (fbJSON.Dictionary.ContainsKey("error_msg")) // { // errorMessage = fbJSON.Dictionary["error_msg"].String; // errorNumber = fbJSON.Dictionary["error_code"].String; // } // else // { // errorMessage = fbJSON.Dictionary["error"].Dictionary["message"].String; // errorNumber = fbJSON.Dictionary["error"].Dictionary["code"].String; // } // if (errorNumber.Equals("4")) // { // //(#4)為超出limit sleep 10秒後再繼續 // Thread.Sleep(10 * 1000); // fbJSON = api.FBGraphGet(url); // } // else { break; } // } // else { break; } //} try { if (fbJSON.Dictionary["data"].Array.Length > 0) { } else { break; } } catch { break; } } while (fbJSON.Dictionary["data"].Array.Length > 0); }
/// <summary> /// return with id,name,picture,link,category,website,founded,likes,talking_about_count /// </summary> #region get pages information public List<string> pages_info(string url) { FBGraph api = new FBGraph(); JSONObject fbJSON = api.FBGraphGet(url); while (true) { try { if (fbJSON.Dictionary.ContainsKey("error_msg") || fbJSON.Dictionary.ContainsKey("error")) { string errorMessage = ""; string errorNumber = ""; if (fbJSON.Dictionary.ContainsKey("error_msg")) { errorMessage = fbJSON.Dictionary["error_msg"].String; errorNumber = fbJSON.Dictionary["error_code"].String; } else { errorMessage = fbJSON.Dictionary["error"].Dictionary["message"].String; errorNumber = fbJSON.Dictionary["error"].Dictionary["code"].String; } if (errorNumber.Equals("4")) { //(#4)為超出limit sleep 10秒後再繼續 Thread.Sleep(10 * 1000); fbJSON = api.FBGraphGet(url); } else { messageLog(errorMessage); //showMessage.AppendText(errorMessage + "\n"); return null; } } else { break; } } catch { break; } } List<string> allData = new List<string>(); string id = "0"; string name = " "; string picture = " "; string link = " "; string category = " "; string website = " "; string founded = " "; string info = " "; string likes = "0"; string talking_about_count = "0"; try { id = fbJSON.Dictionary["id"].String; } catch { } try { name = fbJSON.Dictionary["name"].String; } catch { } try { picture = fbJSON.Dictionary["picture"].String; } catch { } try { link = fbJSON.Dictionary["link"].String; } catch { } try { category = fbJSON.Dictionary["category"].String; } catch { } try { website = fbJSON.Dictionary["website"].String; } catch { } try { founded = fbJSON.Dictionary["founded"].String; } catch { } try { info = fbJSON.Dictionary["general_info"].String; } catch { } try { likes = fbJSON.Dictionary["likes"].String; } catch { } try { talking_about_count = fbJSON.Dictionary["talking_about_count"].String; } catch { } allData.Add(id); allData.Add(name); allData.Add(picture); allData.Add(link); allData.Add(category); allData.Add(website); allData.Add(founded); allData.Add(info); allData.Add(likes); allData.Add(talking_about_count); if (id.Equals("0")) Console.WriteLine("the url is " + url + "\n" + fbJSON.String); return allData; }
private void insertPageID_Click(object sender, EventArgs e) { if (searchResultList.Items.Count == 0) { MessageBox.Show("請先搜尋粉絲頁"); return; } DBConnection conn = new DBConnection(dbInfo); conn.MySqlConnect(); string sql = ""; string IDString = ""; int insertCount = 0; foreach (ListViewItem lvi in searchResultList.Items) { if (!lvi.Checked) continue; IDString += lvi.Text + ","; insertCount++; } if (insertCount == 0) { MessageBox.Show("請先勾選要新增的粉絲頁名單"); return; } IDString = IDString.Substring(0, IDString.Length - 1); string url = "https://api.facebook.com/method/fql.query?query=select page_id,name,pic_small,page_url,website,founded,fan_count,general_info,type from page where page_id in (" + IDString + ")&access_token=" + accessTokenBox.Text + "&format=json"; FBGraph api = new FBGraph(); JSONObject result = api.FBGraphGet(url); result = resultCheck(result, url, 0); //try //{ // int erro_count = 0; // while (true) // { // if (result.Dictionary.ContainsKey("error_msg") || result.Dictionary.ContainsKey("error")) // { // //若發生error 檢查是何種error 若無error再透過exception離開迴圈 // string errorMessage = ""; // string errorNumber = ""; // 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; // } // //Console.WriteLine("Error message(" + errorNumber + ") is : " + errorMessage); // if (errorNumber.Equals("602")) // { // MessageBox.Show(errorMessage); // return; // } // else // { // Thread.Sleep(1000); // erro_count++; // if (erro_count > 30) // { // MessageBox.Show(errorMessage); // return; // } // } // } // else // { // break; // } // } //} //catch { } foreach (JSONObject pageData in result.Array) { sql = "insert ignore into pages_info (id,name,picture,link,category,website,founded,info,likes,talking_about_count,updatetime,feeds,comments,new_id) values (" + "?id,?name,?picture,?link,?category,?website,?founded,?info,?likes,?talking_about_count,?updatetime,0,0,1)"; //page_id,name,pic_small,page_url,website,founded,fan_count,general_info,type while (true) { try { MySqlCommand thecmd = new MySqlCommand(sql, conn.conn); thecmd.Parameters.AddWithValue("?id", pageData.Dictionary["page_id"].String); thecmd.Parameters.AddWithValue("?name", pageData.Dictionary["name"].String); thecmd.Parameters.AddWithValue("?picture", pageData.Dictionary["pic_small"].String); thecmd.Parameters.AddWithValue("?link", pageData.Dictionary["page_url"].String); thecmd.Parameters.AddWithValue("?category", pageData.Dictionary["type"].String); thecmd.Parameters.AddWithValue("?website", pageData.Dictionary["website"].String); thecmd.Parameters.AddWithValue("?founded", pageData.Dictionary["founded"].String); thecmd.Parameters.AddWithValue("?info", pageData.Dictionary["general_info"].String); thecmd.Parameters.AddWithValue("?likes", pageData.Dictionary["fan_count"].String); thecmd.Parameters.AddWithValue("?talking_about_count", "0"); thecmd.Parameters.AddWithValue("?updatetime", DateTime.Now); MySqlDataReader themyData = thecmd.ExecuteReader(); themyData.Close(); thecmd.Parameters.Clear(); break; } catch (Exception ee) { Console.WriteLine(ee.StackTrace); conn.closeMySqlConnection(); conn.MySqlConnect(); } } } conn.closeMySqlConnection(); updatePageIDList(); checkCanUpdate(); MessageBox.Show(result.Array.Length.ToString() + "筆資料新增"); }
private void update_user_info_by_ids(StringBuilder sb, DBConnection conn) { Thread.Sleep(5 * 1000); string sql = ""; string url = "https://graph.facebook.com/fql?q=select uid,username,sex from user where uid in (" + sb.ToString() + ")&access_token=" + accessTokenBox.Text; //showMessage.AppendText(url); //return; FBGraph api = new FBGraph(); JSONObject result = api.FBGraphGet(url); result = resultCheck(result, url, 0); if (result == null) { return; } //try //{ // int erro_count = 0; // while (true) // { // if (result.Dictionary.ContainsKey("error_msg") || result.Dictionary.ContainsKey("error")) // { // //若發生error 檢查是何種error 若無error再透過exception離開迴圈 // string errorMessage = ""; // string errorNumber = ""; // 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; // } // //Console.WriteLine("Error message(" + errorNumber + ") is : " + errorMessage); // if (errorNumber.Equals("602")) // { // messageLog(errorMessage); // //showMessage.AppendText(errorMessage + "\n"); // return; // } // else // { // Thread.Sleep(1000); // erro_count++; // if (erro_count > 30) // { // messageLog(errorMessage); // //showMessage.AppendText(errorMessage + "\n"); // return; // } // } // } // else // { // break; // } // } //} //catch { } sb.Clear(); Dictionary<string, string[]> uInfo = new Dictionary<string, string[]>(); if (result.Dictionary.ContainsKey("data") && result.Dictionary["data"].Array.Length > 0) { foreach (JSONObject pageData in result.Dictionary["data"].Array) { string[] tmpString = { pageData.Dictionary["username"].String, pageData.Dictionary["sex"].String }; uInfo.Add(pageData.Dictionary["uid"].String, tmpString); sb.Append(pageData.Dictionary["uid"].String + ","); } string allIDs = sb.ToString(); allIDs = allIDs.Remove(allIDs.Length - 1); sb.Clear(); sb.Append("update user_info set username = case id "); foreach (KeyValuePair<string, string[]> kvp in uInfo) { sb.Append(" when " + kvp.Key + " then '" + kvp.Value[0] + "' "); } sb.Append(" end, gender = case id "); foreach (KeyValuePair<string, string[]> kvp in uInfo) { sb.Append(" when " + kvp.Key + " then '" + kvp.Value[1] + "' "); } sb.Append(" end where id in (" + allIDs + ")"); sql = sb.ToString(); conn.mysql_query(sql).Close(); sql = "update user_info set updatetime = ?updatetime where id in (" + allIDs + ")"; MySqlCommand cmd = new MySqlCommand(sql, conn.conn); cmd.Parameters.AddWithValue("?updatetime", DateTime.Now); cmd.ExecuteReader().Close(); cmd.Parameters.Clear(); } }
private void searchButton_Click(object sender, EventArgs e) { if (searchKeyword.Text.Length == 0) { MessageBox.Show("請先輸入搜尋關鍵詞"); return; } searchButton.Text = "搜尋中…"; searchButton.Enabled = false; searchResultList.Items.Clear(); //string url = "https://graph.facebook.com/search?q=" + searchKeyword.Text + "&type=page&access_token=" + accessTokenBox.Text + "&limit=" + searchLimit.Value.ToString(); string url = "https://graph.facebook.com/search?q=" + searchKeyword.Text + "&type=page&access_token=" + accessTokenBox.Text + "&limit=500"; //showMessage.AppendText(url + "\n"); FBGraph api = new FBGraph(); JSONObject result = api.FBGraphGet(url); try { if (!result.Dictionary.ContainsKey("data") || result.Dictionary["data"].Array.Length == 0) { MessageBox.Show("使用『" + searchKeyword.Text + "』查無資料!"); searchButton.Enabled = true; searchButton.Text = "粉絲頁搜尋"; return; } } catch (Exception graphE) { MessageBox.Show("錯誤訊息:" + graphE.ToString()); searchButton.Enabled = true; searchButton.Text = "粉絲頁搜尋"; return; } Dictionary<string, string[]> resultPage = new Dictionary<string, string[]>(); string IDstring = ""; foreach (JSONObject pageData in result.Dictionary["data"].Array) { string[] data = new string[3]; data[0] = pageData.Dictionary["name"].String; data[2] = pageData.Dictionary["category"].String; data[1] = "0"; resultPage.Add(pageData.Dictionary["id"].String, data); IDstring += pageData.Dictionary["id"].String + ","; } IDstring = IDstring.Substring(0, IDstring.Length - 1); DateTime start = DateTime.Now; url = "https://api.facebook.com/method/fql.query?query=select page_id, fan_count from page where page_id in (" + IDstring + ")&access_token=" + accessTokenBox.Text + "&format=json"; //showMessage.AppendText(url + "\n"); result = api.FBGraphGet(url); //showMessage.AppendText(DateTime.Now.Subtract(start).TotalSeconds.ToString(".00") + " s\n"); if (result.Dictionary != null && (result.Dictionary.ContainsKey("error_msg") || result.Dictionary.ContainsKey("error"))) { int erro_count = 0; while (true) { //若發生error 檢查是何種error 若無error再透過exception離開迴圈 string errorMessage = ""; string errorNumber = ""; 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; } //Console.WriteLine("Error message(" + errorNumber + ") is : " + errorMessage); if (errorNumber.Equals("602")) { MessageBox.Show(errorMessage); searchButton.Enabled = true; searchButton.Text = "粉絲頁搜尋"; return; } else { Thread.Sleep(1000); erro_count++; if (erro_count > 30) { MessageBox.Show(errorMessage); searchButton.Enabled = true; searchButton.Text = "粉絲頁搜尋"; return; } } } } foreach (JSONObject pageData in result.Array) { resultPage[pageData.Dictionary["page_id"].String][1] = pageData.Dictionary["fan_count"].String; } int listCount = 0; foreach (KeyValuePair<string, string[]> kvp in resultPage) { ListViewItem item = new ListViewItem(kvp.Key); for (int i = 0; i < kvp.Value.Length; i++) { item.SubItems.Add(kvp.Value[i]); } if (kvp.Value[0].Contains(searchKeyword.Text)) { searchResultList.Items.Add(item); listCount++; if (listCount > (int)searchLimit.Value) break; } } searchButton.Enabled = true; searchButton.Text = "粉絲頁搜尋"; }