public override bool Update(string connectionString, BasicInfo info) { // 沒有資料就離開 if (info == null) return false; if (info.GameStates == "X") return false; string Sql = "UPDATE [IceHockeySchedules] WITH(ROWLOCK)" + "\r\n" + " SET [RunsA] = @RunsA" + "\r\n" + " ,[RunsB] = @RunsB" + "\r\n" + " ,[StatusText] = @StatusText" + "\r\n" + " ,[GameStates] = @GameStates" + "\r\n" + " ,[ChangeCount] = [ChangeCount] + 1" + "\r\n" + " WHERE ([GameType] = @GameType)" + "\r\n" + " AND ([WebID] = @WebID)" + " AND ([CtrlStates] = 2)" + "\r\n" + " AND ([IsDeleted] = 0)"; SqlConnection conn = null; SqlCommand cmd = null; bool result = false; // 錯誤處理 try { conn = new SqlConnection(connectionString); cmd = new SqlCommand(Sql, conn); // 參數 cmd.Parameters.Add(new SqlParameter("@GameType", info.GameType)); cmd.Parameters.Add(new SqlParameter("@GameStates", info.GameStates)); cmd.Parameters.Add(new SqlParameter("@WebID", info.WebID)); cmd.Parameters.Add(new SqlParameter("@RunsA", DBNull.Value)); cmd.Parameters.Add(new SqlParameter("@RunsB", DBNull.Value)); cmd.Parameters.Add(new SqlParameter("@StatusText", DBNull.Value)); cmd.Parameters.Add(new SqlParameter("@Record", DBNull.Value)); // 分數 (主客交換) if (!string.IsNullOrEmpty(info.AwayPoint) && !string.IsNullOrEmpty(info.HomePoint)) { cmd.Parameters["@RunsA"].Value = BasicInfo.ConvertBoard((info.AcH) ? (info.HomeBoard) : (info.AwayBoard)); cmd.Parameters["@RunsB"].Value = BasicInfo.ConvertBoard((info.AcH) ? (info.AwayBoard) : (info.HomeBoard)); } // 狀態 if (!string.IsNullOrEmpty(info.Status)) { cmd.Parameters["@StatusText"].Value = info.Status; } // 開啟 conn.Open(); // 執行 result = (cmd.ExecuteNonQuery() > 0); conn.Close(); } catch { } // 沒有關閉資料庫連接就關閉連接 if (conn != null && conn.State == ConnectionState.Open) { conn.Close(); } // 傳回 return result; }
public override bool Update(string connectionString, BasicInfo info) { // 沒有資料就離開 if (info == null) return false; if (info.Status == null) return false; bool result = UpdateData(connectionString, info); // 記錄: 有更新資料才寫紀錄 if (!string.IsNullOrEmpty(info.Record) && info.Record.Length > 20) { this.Logs.Update("\r\n" + info.Record); } // 傳回 return result; }
public override bool Update(string connectionString, BasicInfo info) { // 以隊伍名稱當作更新的依據 //return this.Update2(connectionString, info); return this.Update3(connectionString, info);//昨天跟今天的隊伍比對 }
/// <summary> /// 取得資料。(Asiascore) /// </summary> /// <param name="data">資料</param> /// <param name="playMinute">比賽的時間長度</param> protected Dictionary<string, BasicInfo> GetDataByAsiaScore(IHDownload download, int playMinute) { string html = download.Data; // 沒有資料就離開 if (string.IsNullOrEmpty(html)) return null; Dictionary<string, BasicInfo> result = new Dictionary<string, BasicInfo>(); BasicInfo gameInfo = null; // 判斷資料 string[] all = html.Split(new string[] { "¬~ZA÷" }, StringSplitOptions.RemoveEmptyEntries); // 聯盟 (第一筆是多餘的) int orderBy = 1; for (int allianceIndex = 1; allianceIndex < all.Length; allianceIndex++) { // 比賽集合 string[] games = ("ZA÷" + all[allianceIndex]).Split(new string[] { "~" }, StringSplitOptions.RemoveEmptyEntries); string allianceName = null; string trackerText = null; // 聯盟資料 // 比賽資料 for (int gameIndex = 0; gameIndex < games.Length; gameIndex++) { Dictionary<string, string> info = new Dictionary<string, string>(); #region 取出資料 string[] data = games[gameIndex].Split(new string[] { "¬" }, StringSplitOptions.RemoveEmptyEntries); // 資料 foreach (string d in data) { string[] txt = d.Split(new string[] { "÷" }, StringSplitOptions.RemoveEmptyEntries); // 記錄 info[txt[0]] = txt[1]; } #endregion #region 第一筆是聯盟 if (gameIndex == 0) { allianceName = info["ZA"]; if (info["ZF"] == "2") { trackerText = "只顯示最終比分"; } continue; } else { // 沒有編號就往下處理 if (!info.ContainsKey("AA")) continue; } #endregion // 沒有隊伍就往下處理 if (!info.ContainsKey("AE") || !info.ContainsKey("AF")) continue; // 時間是 1970 年加上 Ti DateTime gameTime = DateTime.Parse("1970/1/1 00:00:00").AddTicks(long.Parse(info["AD"]) * 10000000); // 轉成台灣時間 UTC+8 gameTime = gameTime.AddHours(8); // 建立比賽資料 gameInfo = null; gameInfo = new BasicInfo(this.AllianceID, this.GameType, gameTime, info["AA"]); gameInfo.OverDayGame = false; gameInfo.Home = info["AE"].Replace("GOAL", "").Replace("'", "''"); gameInfo.Away = info["AF"].Replace("GOAL", "").Replace("'", "''"); // 2014/04/18, 新增聯盟(含賽別) gameInfo.AllianceName = allianceName.Replace("'", "''"); gameInfo.AwayPoint = "0"; gameInfo.HomePoint = "0"; gameInfo.TrackerText = ""; #region 分數 if (info.ContainsKey("AG")) { string[] nums = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" }; for (int i = 0; i < nums.Length; i += 2) { // 沒有資料就離開 if (!info.ContainsKey("B" + nums[i])) break; // 分數 gameInfo.HomeBoard.Add(info["B" + nums[i]]); gameInfo.AwayBoard.Add(info["B" + nums[i + 1]]); } gameInfo.AwayPoint = info["AH"]; gameInfo.HomePoint = info["AG"]; } #endregion #region 比賽狀態 switch (info["AB"]) { case "2": gameInfo.GameStates = "S"; // 狀態 if (info.ContainsKey("AC") && (info["AC"] == "38" || info["AC"] == "46")) { gameInfo.Status = "中場休息"; } if (info.ContainsKey("BX")) { // 剩餘時間 gameInfo.Status = (playMinute - int.Parse(info["BX"])).ToString(); } break; case "3": if (info.ContainsKey("AC")) { switch (info["AC"]) { case "4": case "5": case "36": gameInfo.GameStates = "P"; gameInfo.Status = "中止"; break; default: gameInfo.GameStates = "E"; gameInfo.Status = "結束"; break; } } else { gameInfo.GameStates = "E"; gameInfo.Status = "結束"; } break; } //最後修正只顯示最終比分的賽事狀態,有些結束賽事的ZF字段也為2,需要排除這種結束賽事 if (trackerText != null && info["AB"] != "3" && info["AC"] != "3") { gameInfo.TrackerText = trackerText; } #endregion //if (gameInfo.GameStates == "S") //{ // int num = 0; // // 加時 // if (gameInfo.Quarter == 4) // { // if (!string.IsNullOrEmpty(gameInfo.Status) && // int.TryParse(gameInfo.Status, out num)) // { // num = 20 - num; // 還原時間 // gameInfo.Status = (5 - num).ToString(); // 經過時間 // } // } //} // 加入 #region 排序 gameInfo.OrderBy = orderBy; #endregion gameInfo.Display = 1; result[gameInfo.WebID] = gameInfo; } orderBy++; } // 傳回 if (result.Count > 0) { DateTime maxGameTime = result.Values.Max(p => p.GameTime);//取最大开赛时间 作为比赛日期 download.GameDate = maxGameTime.Date; List<BasicInfo> overDayGames = result.Values.Where(p => p.GameTime.Date < maxGameTime.Date).ToList();//比赛日期小于最大开赛时间的日期的为跨天赛事 StringBuilder sb = new StringBuilder("OverDayGame:\r\n"); overDayGames.ForEach(p => { p.OverDayGame = true; sb.Append(p.ToString() + "\r\n"); }); if (overDayGames.Count > 0) { this.Logs.GameInfo(sb.ToString()); } PrintGame(result, download.GameDate.ToString(DATE_STRING_FORMAT), "DownGame",true); } return result; }
public override int Follow() { // 沒有資料就離開 if (string.IsNullOrEmpty(this.DownHome.Data)) return 0; int result = 0; BasicInfo gameInfo = null; HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument(); string xPath = "/html[1]/body[1]/div[1]/div[3]/div[1]/div[3]/div[1]/div[1]/table[1]"; // 載入資料 document.LoadHtml(this.DownHome.Data); // 資料位置 HtmlAgilityPack.HtmlNode nodeGames = document.DocumentNode.SelectSingleNode(xPath); // 判斷資料 if (nodeGames != null && nodeGames.ChildNodes != null) { DateTime gameDate = this.GameDate; DateTime gameTime = this.GameDate; // 資料 foreach (HtmlAgilityPack.HtmlNode game in nodeGames.ChildNodes) { // 不是資料就往下處理 if (game.Name != "tr") continue; string webID = game.ChildNodes[3].Id; string doc = game.ChildNodes[1].InnerText; string[] data = null; string[] team = null; #region 跟盤 ID // 取代文字 doc = doc.Replace("\r", ""); doc = doc.Replace("\n", ""); doc = doc.Replace("\t", ""); // 分割字串 data = doc.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); // 資料是錯的就往下處理 if (data.Length != 4) { continue; } team = data[2].Split(new string[] { "vs." }, StringSplitOptions.RemoveEmptyEntries); // 資料是錯的就往下處理 if (team.Length != 2) { continue; } // 刪除無用資料 if (data[0].IndexOf("(") != -1) { data[0] = data[0].Substring(0, data[0].IndexOf("(")).Trim(); } data[0].Replace("月", "/"); data[0].Replace("日", "/"); // 日期是錯的就往下處理 if (!DateTime.TryParse(data[0] + " " + data[1], out gameTime)) { continue; } #endregion // 不是今天就往下處理 if (gameTime.Date != this.GameDate.Date) continue; // 建立比賽資料 gameInfo = null; gameInfo = new BasicInfo(this.AllianceID, this.GameType, gameTime, webID); gameInfo.AcH = true; // 第一個資料是 home 的,所以要交換隊伍資料。 gameInfo.Away = team[1].Trim(); gameInfo.Home = team[0].Trim(); #region 下載比賽資料,比賽時間 10 分鐘前就開始處理 if (!this.DownReal.ContainsKey(webID) && GetUtcJp(DateTime.Now) >= gameTime.AddMinutes(-10)) { this.DownReal.Add(webID, new string[] { DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), null }); SendHttpWedRequest2(webID); } #endregion #region 處理比賽資料 if (this.DownReal.ContainsKey(webID) && !string.IsNullOrEmpty(this.DownReal[webID][1])) { // 錯誤處理 try { JObject json = JObject.Parse(this.DownReal[webID][1]); // 總分 gameInfo.AwayPoint = json["game_score"]["away_point"].ToString(); gameInfo.HomePoint = json["game_score"]["home_point"].ToString(); // 分數 if (json["game_score"]["point"] != null && !string.IsNullOrEmpty(json["game_score"]["point"].ToString().Trim())) { JArray board = JArray.Parse(json["game_score"]["point"].ToString()); // 資料 for (int i = 0; i < board.Count; i++) { gameInfo.AwayBoard.Add(board[i]["away_point"].ToString()); gameInfo.HomeBoard.Add(board[i]["home_point"].ToString()); } } // 判斷比賽狀態 0 = 比賽未開始 if (json["game_score"]["quarter"].ToString() != "0") { // Status if (json["game_score"]["game_status"].ToString() == "end") { gameInfo.GameStates = "E"; gameInfo.Status = null; } else { gameInfo.GameStates = "S"; gameInfo.Status = json["game_score"]["quarter_remaining_minutes"].ToString() + ":" + json["game_score"]["quarter_remaining_seconds"].ToString(); // 判斷 if (gameInfo.Status == "00:00") { gameInfo.Status = "結束"; } } } } catch { } } #endregion // 加入 this.GameData[gameInfo.WebID] = gameInfo; // 累計 result++; } } // 傳回 return result; }
public override bool Update(string connectionString, BasicInfo info) { // 以隊伍名稱當作更新的依據 //return this.Update2(connectionString, info); // 改用多來源跟分 return this.Update4(connectionString, info); }
public override int Follow() { // 沒有資料就離開 if (string.IsNullOrEmpty(this.DownHome.Data)) return 0; int result = 0; BasicInfo gameInfo = null; HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument(); string xPath = "/html[1]/body[1]/center[1]/table[2]/tr[1]/td[2]/div[1]/table[3]/tbody[1]"; // table[3] 改 p[1] 就是年月 // 載入資料 document.LoadHtml(this.DownHome.Data); // 資料位置 HtmlAgilityPack.HtmlNode nodeGames = document.DocumentNode.SelectSingleNode(xPath); // 判斷資料 if (nodeGames == null) { xPath = "/html[1]/body[1]/center[1]/table[2]/tr[1]/td[1]/td[1]/div[1]/table[3]/tbody[1]"; // table[3] 改 p[1] 就是年月 nodeGames = document.DocumentNode.SelectSingleNode(xPath); } if (nodeGames != null && nodeGames.ChildNodes != null) { DateTime gameDate = this.GameDate; DateTime gameTime = this.GameDate; string gameDateStr = null; // 資料 foreach (HtmlAgilityPack.HtmlNode game in nodeGames.ChildNodes) { // 不是資料就往下處理 if (game.Name != "tr") continue; string webID = null; string webUrl = null; int index = 0; #region 取得日期 if (game.ChildNodes.Count == 9) { gameDateStr = game.ChildNodes[1].InnerText.Replace("월", "/").Replace("일", "").Replace(" ", ""); // 截斷日期 if (gameDateStr.IndexOf("(") != -1) gameDateStr = DateTime.Now.ToString("yyyy") + "/" + gameDateStr.Substring(0, gameDateStr.IndexOf("(")).Trim(); // 判斷日期,失敗就往下處理 if (!DateTime.TryParse(gameDateStr, out gameDate)) { gameDateStr = null; continue; } index = 2; } // 沒有日期就往下處理 if (string.IsNullOrEmpty(gameDateStr)) continue; #endregion #region 取得時間 // 判斷日期,錯誤就離開 if (!DateTime.TryParse(gameDate.ToString("yyyy/MM/dd") + " " + game.ChildNodes[index + 3].InnerText, out gameTime)) continue; // 判斷是否為今天,不是今天就往下處理 if (this.GameDate.Date != gameTime.Date) { gameDateStr = null; continue; } #endregion #region 跟盤 ID webID = game.ChildNodes[index + 5].ChildNodes[1].GetAttributeValue("href", ""); Uri uri = null; HttpRequest req = null; // 錯誤處理 try { uri = new Uri(@"http://www.a.com" + webID); // 判斷是否有資料 if (uri.Query != null && !string.IsNullOrEmpty(uri.Query)) { req = new HttpRequest("", uri.AbsoluteUri, uri.Query.Substring(1)); // 判斷資料 if (req["gameid"] != null && !string.IsNullOrEmpty(req["gameid"].Trim())) { webID = req["gameid"]; webUrl = @"http://sportsdata.naver.com/data//kbl_game/" + webID.Substring(0, 4) + "/" + webID.Substring(4, 2) + "/" + webID + ".nsd"; } } } catch { webID = null; } // 錯誤,清除跟盤 ID // 沒有跟盤 ID 就往下處理 if (webID == null || string.IsNullOrEmpty(webID.Trim())) { gameDateStr = null; continue; } #endregion // 建立比賽資料 gameInfo = null; gameInfo = new BasicInfo(this.AllianceID, this.GameType, gameTime, webID); gameInfo.AcH = true; string[] teamName = game.ChildNodes[index + 1].InnerText.Split(new string[] { "vs" }, StringSplitOptions.RemoveEmptyEntries); // 隊伍名稱 if (teamName.Length == 2) { gameInfo.Away = teamName[0].Trim(); gameInfo.Home = teamName[1].Trim(); } #region 下載比賽資料,比賽時間 10 分鐘前就開始處理 if (!this.DownReal.ContainsKey(webID) && GetUtcKr(DateTime.Now) >= gameTime.AddMinutes(-10)) { this.DownReal[webID] = new BasicDownload(this.Sport, webUrl, Encoding.GetEncoding(949), webID); this.DownReal[webID].DownloadString(); } #endregion #region 處理比賽資料 if (this.DownReal.ContainsKey(webID) && !string.IsNullOrEmpty(this.DownReal[webID].Data)) { string[] data = this.DownReal[webID].Data.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries); // 判斷數量 if (data != null && data.Length == 7 && !string.IsNullOrEmpty(data[3])) { string strJson = (data[3].IndexOf("{") != -1) ? (data[3].Substring(data[3].IndexOf("{"))) : (data[3]); // 錯誤處理 try { JObject json = JObject.Parse(strJson); string home_team = json["home_team"].ToString(); string away_team = json["away_team"].ToString(); // 隊伍名稱 gameInfo.Away = json["team_avg_rec"][away_team]["short_name"].ToString(); gameInfo.Home = json["team_avg_rec"][home_team]["short_name"].ToString(); gameInfo.Status = json["live_text"]["time"].ToString().Replace("경기전", "賽前"); #region 分數 for (int i = 1; i < 5; i++) { if (json["quarter_score"][home_team].SelectToken("Q" + i.ToString()) != null) { gameInfo.AwayBoard.Add(json["quarter_score"][away_team]["Q" + i.ToString()].ToString()); gameInfo.HomeBoard.Add(json["quarter_score"][home_team]["Q" + i.ToString()].ToString()); } } #region 延長賽 bool hasOT = false; int home_OT = 0; int away_OT = 0; // 延長賽分數 for (int i = 1; i < 5; i++) { if (json["quarter_score"][home_team].SelectToken("X" + i.ToString()) != null) { hasOT = true; away_OT += int.Parse(json["quarter_score"][away_team]["X" + i.ToString()].ToString()); home_OT += int.Parse(json["quarter_score"][home_team]["X" + i.ToString()].ToString()); } } // 判斷 if (hasOT) { gameInfo.AwayBoard.Add(away_OT.ToString()); gameInfo.HomeBoard.Add(home_OT.ToString()); } #endregion // 總分 gameInfo.AwayPoint = json["quarter_score"][away_team]["total_score"].ToString(); gameInfo.HomePoint = json["quarter_score"][home_team]["total_score"].ToString(); #endregion #region 取得比賽中的時間 if (json["live_text"][json["live_text"]["quarter"].ToString()] != null) { string time = null; // 錯誤處理 try { // 取到最後的時間 foreach (JProperty obj in json["live_text"][json["live_text"]["quarter"].ToString()]) { time = obj.Name; } } catch { time = null; } // 加入狀態 if (time != null && !string.IsNullOrEmpty(time)) { // 轉換 DateTime oldTime = DateTime.Parse("00:" + time); DateTime newTime = DateTime.Parse("00:" + "10:00").AddSeconds(0 - (oldTime.Second + 60 * oldTime.Minute)); // 設定 gameInfo.Status = newTime.ToString("mm:ss"); } } else { if (gameInfo.Status != "賽前") { gameInfo.Status = null; } } #endregion #region 比賽結束 if (json["live_text"]["quarter"].ToString().ToLower() == "end") { gameInfo.GameStates = "E"; gameInfo.Status = "結束"; } else { // 有分數才是比賽開始 if (gameInfo.Quarter > 0) { gameInfo.GameStates = "S"; } } #endregion } catch { } } } #endregion // 加入 this.GameData[gameInfo.WebID] = gameInfo; // 累計 result++; } } // 傳回 return result; }
private BasicInfo ProcessGameData(HtmlAgilityPack.HtmlNode game, DateTime gameDate, string webID) { // 建立比賽資料,時間以台灣為主 BasicInfo gameInfo = new BasicInfo(this.AllianceID, this.GameType, gameDate, webID); gameInfo.Away = game.SelectSingleNode("div/div[@class='team visitor']/div[@class='team-capsule']/p[@class='team-name']/span/a").InnerText; gameInfo.Home = game.SelectSingleNode("div/div[@class='team home']/div[@class='team-capsule']/p[@class='team-name']/span/a").InnerText; gameInfo.Status = game.SelectSingleNode("div/div[@class='game-header']/div/p").InnerText; gameInfo.Status = gameInfo.Status.ToLower(); // 取代文字 gameInfo.Status = gameInfo.Status.Replace("halftime", "中場休息"); gameInfo.Status = gameInfo.Status.Replace("half", "中場休息"); gameInfo.Status = gameInfo.Status.Replace("qtr", ""); gameInfo.Status = gameInfo.Status.Replace("1st", ""); gameInfo.Status = gameInfo.Status.Replace("2nd", ""); gameInfo.Status = gameInfo.Status.Replace("3rd", ""); gameInfo.Status = gameInfo.Status.Replace("4th", ""); gameInfo.Status = gameInfo.Status.Replace("ot", ""); gameInfo.Status = gameInfo.Status.Replace("2ot", ""); gameInfo.Status = gameInfo.Status.Replace("of", ""); gameInfo.Status = gameInfo.Status.Replace("end", "結束"); gameInfo.Status = gameInfo.Status.Replace(" ", "");//移除空白字元 gameInfo.Status = gameInfo.Status.Trim(); if (gameInfo.Status == "00:00") gameInfo.Status = "結束"; //目前有顯示的分數欄位 HtmlAgilityPack.HtmlNode scoreHeader = game.SelectSingleNode("div/div[@class='game-header']/ul[@class='score header']"); if (scoreHeader != null)//是否已經有比分 { string linescoreHeader = game.SelectSingleNode("div/div[@class='game-header']/ul[@class='score header']").InnerText; if (linescoreHeader.Length > 7)//資料不完整,缺乏前面局數比分 2 3 4 OT OT T { //第1個數字才是有顯示的比分 int lossInning = 1; int.TryParse(linescoreHeader.Substring(0, 1), out lossInning); if (lossInning > 1) { lossInning--; GetGameScore(gameInfo, lossInning);//無暫存取得遺漏比分 } } List<int> FullAwayBoard = new List<int>();//完整比分 List<int> FullHomeBoard = new List<int>();//完整比分 if (this.DownScore.ContainsKey(webID) && !string.IsNullOrEmpty(this.DownScore[webID].Data)) { HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(this.DownScore[webID].Data); HtmlAgilityPack.HtmlNodeCollection HomeAway = doc.GetElementbyId("gamepackageTop").SelectNodes("div[@class='line-score clear']/div[@class='line-score-container']/table/tr"); if (HomeAway != null && HomeAway.Count == 3) { foreach (HtmlAgilityPack.HtmlNode scores in HomeAway) { if (scores.Attributes["class"] != null)//忽略periods continue; HtmlAgilityPack.HtmlNodeCollection score = scores.SelectNodes("td"); if (score != null) { bool isAway = false; if (FullAwayBoard.Count == 0) isAway = true; int period = score.Count - 1; for (int i = 1; i < period; i++)//第0個值為隊名 { string tmpNum = score[i].InnerHtml.Replace("\n", "").Replace("\t", "").Trim(); int tryInt = 0; int.TryParse(tmpNum, out tryInt); if (isAway) FullAwayBoard.Add(tryInt); else FullHomeBoard.Add(tryInt); } } } } } //首頁比分計算 foreach (HtmlAgilityPack.HtmlNode numDoc in game.SelectNodes("div/div[@class='team visitor']/ul[@class='score']/li")) { int num = 0; if (int.TryParse(numDoc.InnerText, out num)) { if (gameInfo.AwayBoard.Count < 4)//沒有OT gameInfo.AwayBoard.Add(numDoc.InnerText); else { int otDiff = FullAwayBoard.Count - gameInfo.AwayBoard.Count;//OT數差別 if (FullAwayBoard.Count == 0 || otDiff == 0) gameInfo.AwayBoard.Add(numDoc.InnerText); else { int sum = 0; for (int i = 0; i < otDiff - 1; i++) { sum += FullAwayBoard[4 + i];//最後一個OT不累加 gameInfo.AwayBoard.Add(FullAwayBoard[4 + i].ToString());//OT1 OT2 OT3 ... } gameInfo.AwayBoard.Add((num - sum).ToString());//剩下的OT分數 } } } } foreach (HtmlAgilityPack.HtmlNode numDoc in game.SelectNodes("div/div[@class='team home']/ul[@class='score']/li")) { int num = 0; if (int.TryParse(numDoc.InnerText, out num)) { if (gameInfo.HomeBoard.Count < 4)//沒有OT gameInfo.HomeBoard.Add(numDoc.InnerText); else { int otDiff = FullHomeBoard.Count - gameInfo.HomeBoard.Count;//OT數差別 if (FullHomeBoard.Count == 0 || otDiff == 0)//尚未取得完整比分資料 或者是最後一個比分則為總分 gameInfo.HomeBoard.Add(numDoc.InnerText); else { int sum = 0; for (int i = 0; i < otDiff - 1; i++) { sum += FullHomeBoard[4 + i];//最後一個OT不累加 gameInfo.HomeBoard.Add(FullHomeBoard[4 + i].ToString());//OT1 OT2 OT3 ... } gameInfo.HomeBoard.Add((num - sum).ToString());//剩下的OT分數 } } } } // 總分 if (gameInfo.AwayBoard.Count > 0) { gameInfo.AwayPoint = gameInfo.AwayBoard[gameInfo.AwayBoard.Count - 1]; gameInfo.AwayBoard.RemoveAt(gameInfo.AwayBoard.Count - 1); gameInfo.HomePoint = gameInfo.HomeBoard[gameInfo.HomeBoard.Count - 1]; gameInfo.HomeBoard.RemoveAt(gameInfo.HomeBoard.Count - 1); } } if (gameInfo.Status.IndexOf("final") != -1) { gameInfo.GameStates = "E"; } else if (gameInfo.Quarter > 0) { gameInfo.GameStates = "S"; } return gameInfo; }
/// <summary> /// 更新資料。 /// </summary> /// <param name="info">比賽資料</param> public virtual bool Update(string connectionString, BasicInfo info) { return false; }
/// <summary> /// 取得資料。(Milb)(賽程資料) /// </summary> /// <param name="data">資料</param> protected Dictionary<string, BasicInfo> GetDataByMilbForSchedules(string html) { // 沒有資料就離開 if (string.IsNullOrEmpty(html)) return null; Dictionary<string, BasicInfo> result = new Dictionary<string, BasicInfo>(); BasicInfo gameInfo = null; DateTime gameTime = DateTime.Now; JObject jsonSchedules = JObject.Parse(html); // 判斷資料 if (jsonSchedules["schedule_vw_complete"] != null && jsonSchedules["schedule_vw_complete"]["queryResults"] != null && jsonSchedules["schedule_vw_complete"]["queryResults"]["row"] != null) { JArray games = new JArray(); // 判斷是 Arraj OR Object if (jsonSchedules["schedule_vw_complete"]["queryResults"]["row"] is JArray) { games = (JArray)jsonSchedules["schedule_vw_complete"]["queryResults"]["row"]; } else { games.Add(jsonSchedules["schedule_vw_complete"]["queryResults"]["row"]); } // 資料 for (int index = 0; index < games.Count; index++) { JObject game = (JObject)games[index]; // 判斷時間 if (DateTime.TryParse(game["game_time_local"].ToString(), out gameTime)) { string webID = "gid_" + game["game_id"].ToString().Replace("/", "_").Replace("-", "_"); // 建立比賽資料 gameInfo = null; gameInfo = new BasicInfo(this.AllianceID, this.GameType, gameTime, webID); gameInfo.IsBall = true; gameInfo.Away = game["away_team_short"].ToString(); gameInfo.Home = game["home_team_short"].ToString(); // 加入 result[gameInfo.WebID] = gameInfo; } } } // 傳回 return result; }
/// <summary> /// 取得資料。(Milb)(比賽資料) /// </summary> /// <param name="data">資料</param> protected BasicInfo GetDataByMilbForGame(string html, string webID) { // 沒有資料就離開 if (string.IsNullOrEmpty(html)) return null; BasicInfo gameInfo = null; JObject json = JObject.Parse(html); // 判斷資料 if (json["data"] != null && json["data"]["game"] != null) { json = (JObject)json["data"]["game"]; gameInfo = new BasicInfo(this.AllianceID, this.GameType, this.GameDate, webID); gameInfo.Status = json["status"].ToString(); // 熱身賽或賽前就離開不處理 if (!string.IsNullOrEmpty(gameInfo.Status) && (gameInfo.Status.ToLower() == "warmup" || gameInfo.Status.ToLower() == "pre-game")) return null; #region 分數 if (json["linescore"] != null) { if (json["linescore"] is JObject) { if (json["linescore"]["away_inning_runs"] != null) gameInfo.AwayBoard.Add(json["linescore"]["away_inning_runs"].ToString()); if (json["linescore"]["home_inning_runs"] != null) gameInfo.HomeBoard.Add(json["linescore"]["home_inning_runs"].ToString()); } else { foreach (JObject board in (JArray)json["linescore"]) { if (board["away_inning_runs"] != null) gameInfo.AwayBoard.Add(board["away_inning_runs"].ToString()); if (board["home_inning_runs"] != null) gameInfo.HomeBoard.Add(board["home_inning_runs"].ToString()); } } } #endregion #region RHE if (json["away_team_runs"] != null && json["home_team_runs"] != null) { gameInfo.AwayPoint = json["away_team_runs"].ToString(); gameInfo.AwayH = json["away_team_hits"].ToString(); gameInfo.AwayE = json["away_team_errors"].ToString(); gameInfo.HomePoint = json["home_team_runs"].ToString(); gameInfo.HomeH = json["home_team_hits"].ToString(); gameInfo.HomeE = json["home_team_errors"].ToString(); } #endregion #region BSOB int bsob = 0; // Bases //if (json["runner_on_base_status"] != null && int.TryParse(json["runner_on_base_status"].ToString(), out bsob)) gameInfo.Bases = bsob; if (json["runner_on_1b"] != null) bsob += 1; if (json["runner_on_2b"] != null) bsob += 2; if (json["runner_on_3b"] != null) bsob += 4; gameInfo.Bases = bsob; // BSO if (json["balls"] != null && int.TryParse(json["balls"].ToString(), out bsob)) gameInfo.BallB = bsob; if (json["strikes"] != null && int.TryParse(json["strikes"].ToString(), out bsob)) gameInfo.BallS = bsob; if (json["outs"] != null && int.TryParse(json["outs"].ToString(), out bsob)) gameInfo.BallO = bsob; #endregion #region 比賽狀態 switch (json["status"].ToString().ToLower()) { case "final": case "game over": case "completed early": // 結束 gameInfo.GameStates = "E"; gameInfo.Status = "結束"; gameInfo.BallB = 0; gameInfo.BallS = 0; gameInfo.BallO = 0; gameInfo.Bases = 0; // 補上 X if (gameInfo.AwayBoard.Count > gameInfo.HomeBoard.Count) gameInfo.HomeBoard.Add("X"); else if (string.IsNullOrEmpty(gameInfo.HomeBoard[gameInfo.HomeBoard.Count - 1])) gameInfo.HomeBoard[gameInfo.HomeBoard.Count - 1] = "X"; // 抓取訊息顯示 if (json["reason"] != null) gameInfo.TrackerText = json["reason"].ToString(); break; case "postponed": // 中止 gameInfo.GameStates = "P"; gameInfo.Status = "中止"; gameInfo.BallB = 0; gameInfo.BallS = 0; gameInfo.BallO = 0; gameInfo.Bases = 0; // 抓取訊息顯示 if (json["reason"] != null) gameInfo.TrackerText = json["reason"].ToString(); break; case "cancelled": // 取消 gameInfo.GameStates = "C"; gameInfo.Status = "取消"; gameInfo.BallB = 0; gameInfo.BallS = 0; gameInfo.BallO = 0; gameInfo.Bases = 0; // 抓取訊息顯示 if (json["reason"] != null) gameInfo.TrackerText = json["reason"].ToString(); break; case "suspended": // 暫停 case "delayed": // 延遲 gameInfo.GameStates = "D"; gameInfo.Status = "延遲"; // 抓取訊息顯示 if (json["reason"] != null) gameInfo.TrackerText = json["reason"].ToString(); break; case "delayed start": // 延遲 gameInfo.GameStates = "D"; gameInfo.Status = "延遲"; gameInfo.BallB = 0; gameInfo.BallS = 0; gameInfo.BallO = 0; gameInfo.Bases = 0; // 抓取訊息顯示 if (json["reason"] != null) gameInfo.TrackerText = json["reason"].ToString(); break; case "warmup": //熱身 gameInfo.GameStates = "W"; gameInfo.Status = "準備開賽"; gameInfo.BallB = 0; gameInfo.BallS = 0; gameInfo.BallO = 0; gameInfo.Bases = 0; // 抓取訊息顯示 if (json["reason"] != null) gameInfo.TrackerText = json["reason"].ToString(); break; default: // 有分數才是比賽開始 if (gameInfo.Quarter > 0) { gameInfo.GameStates = "S"; // 分數判斷 if (string.IsNullOrEmpty(gameInfo.HomeBoard[gameInfo.HomeBoard.Count - 1])) { gameInfo.HomeBoard[gameInfo.HomeBoard.Count - 1] = "0"; } if (string.IsNullOrEmpty(gameInfo.AwayBoard[gameInfo.AwayBoard.Count - 1])) { gameInfo.AwayBoard[gameInfo.AwayBoard.Count - 1] = "0"; } // 移除下半局 if (json["inning_state"] != null && json["inning_state"].ToString().ToLower() == "top") { if (gameInfo.AwayBoard.Count == gameInfo.HomeBoard.Count) { gameInfo.HomeBoard.RemoveAt(gameInfo.HomeBoard.Count - 1); } } // 其它判斷 if (gameInfo.BallB > 3) gameInfo.BallB = 3; if (gameInfo.BallS > 2) gameInfo.BallS = 2; if (gameInfo.BallO > 2) gameInfo.BallO = 2; } break; } #endregion } // 傳回 return gameInfo; }
/// <summary> /// 取得資料。(奧訊) /// </summary> /// <param name="html">下載的內容</param> /// <param name="find1">關鍵字1</param> /// <param name="find2">關鍵字2</param> /// <param name="isAcH">是否主客互換</param> /// <returns></returns> protected Dictionary<string, BasicInfo> GetDataByBet007Basketball(string html, string find1 = null, string find2 = null, bool isAcH = false, string url = null) { // 沒有資料就離開 if (string.IsNullOrEmpty(html)) { return null; } Dictionary<string, BasicInfo> result = new Dictionary<string, BasicInfo>(); BasicInfo gameInfo = null; DateTime gameDate = this.GameDate; XmlAdapter xmlAdapter = null; try { xmlAdapter = new XmlAdapter(html, false); } catch { string msg = String.Format("解析網頁資料錯誤。{0}Url: {1}{0}Content: {2}{0}", Environment.NewLine, (url ?? String.Empty), html); //throw new Exception(msg, e); this.Logs.Error(msg); return null; } if (xmlAdapter == null) { return null; } xmlAdapter.GoToNode("c"); // 所有比賽集合 List<string> gameRecord = xmlAdapter.GetAllSubColumns("h"); // 目標比賽集合 List<string> targetGames = new List<string>(); foreach (string gameRow in gameRecord) { if (!String.IsNullOrEmpty(find1) || !String.IsNullOrEmpty(find2)) { if (gameRow.IndexOf(find1) >= 0 || gameRow.IndexOf(find2) >= 0) { targetGames.Add(gameRow); } } else { // 沒有關鍵字 取全部資料 targetGames.Add(gameRow); } } // 有找到目標比賽才繼續執行 if (targetGames.Count > 0) { foreach (string game in targetGames) { #region 取出資料 // 0:賽事ID/3:聯盟(簡,繁)/4:分幾節進行/6:開賽時間/7:狀態/8:小節剩餘時間/10:主隊名(簡,繁,英)/12:客隊名(簡,繁,英)/15:主隊總分/16:客隊總分 // 17:主隊1節得分/18:客隊1節得分/19:主隊2節得分/20:客隊2節得分/21:主隊3節得分/22:客隊3節得分/23:主隊4節得分/24:客隊4節得分 // 26:主隊ot1得分/27:客隊ot1得分/28:主隊ot2得分/29:客隊ot2得分/30:主隊ot3得分/31:客隊ot3得分 string[] data = game.Split('^'); #endregion DateTime gameTime = DateTime.Parse(data[6].Replace("<br>", " ")); gameInfo = null; gameInfo = new BasicInfo(this.AllianceID, this.GameType, gameTime, data[0], true); gameInfo.AllianceName = data[3].Split(',')[1]; gameInfo.Away = GetBet007Team(data[12]); gameInfo.Home = GetBet007Team(data[10]); gameInfo.AcH = isAcH; #region 比賽狀態 // 局數 int innings = 0; // 比賽狀態 string state = data[7]; // 剩餘時間 string remainingTime = data[8].Trim(); // 分節數 ( 2: 上下半場, 4: 4 小節 ) int classType = Int32.Parse(data[4]); switch (state) { case "1": case "2": case "3": case "4": case "5": case "6": case "7": innings = Int32.Parse(state); innings = (innings > 4) ? 4 : innings; // 超過4局, 表示 OT, 取4局 gameInfo.Status = "0".Equals(remainingTime) ? "結束" : remainingTime; // 剩餘時間 gameInfo.GameStates = "S"; gameInfo.StateValue = Convert.ToInt32(state); break; case "50": // 中場 (上下半場: 1局, 4小節: 2局) innings = (classType == 2) ? 1 : 2; gameInfo.Status = "中場休息"; gameInfo.GameStates = "S"; break; case "-1": innings = 4; gameInfo.Status = "結束"; gameInfo.GameStates = "E"; gameInfo.StateValue = 8; break; case "-2": // 待定 gameInfo.Status = ""; gameInfo.TrackerText = "只顯示最終比分"; gameInfo.Record = "只顯示最終比分"; gameInfo.GameStates = "X"; break; case "-3": innings = 4; gameInfo.Status = "中止"; gameInfo.GameStates = "P"; break; case "-4": innings = 4; gameInfo.Status = "取消"; gameInfo.GameStates = "C"; break; case "-5": innings = 4; gameInfo.Status = "延遲"; gameInfo.GameStates = "D"; break; } #endregion #region 分數 // 算四小節分數 for (int i = 0; i < innings; i++) { string ptHome = data[17 + 2 * i].Trim(); string ptAway = data[18 + 2 * i].Trim(); if (!String.IsNullOrEmpty(ptAway) && !String.IsNullOrEmpty(ptHome)) { gameInfo.AwayBoard.Add(ptAway); gameInfo.HomeBoard.Add(ptHome); } } // 取得 OT 數 string otCount = data[25].Trim(); if (!String.IsNullOrEmpty(otCount)) { // 取得 OT 比分 int inningOT = Int32.Parse(otCount); for (int i = 0; i < inningOT; i++) { string otHome = StringHelper.IsNullOrEmptyToZero(data[26 + 2 * i]); string otAway = StringHelper.IsNullOrEmptyToZero(data[27 + 2 * i]); gameInfo.AwayBoard.Add(otAway); gameInfo.HomeBoard.Add(otHome); } } // 總分 gameInfo.AwayPoint = StringHelper.IsNullOrEmptyToZero(data[16]); gameInfo.HomePoint = StringHelper.IsNullOrEmptyToZero(data[15]); #endregion gameInfo.Display = 1; // 加入 result[gameInfo.WebID] = gameInfo; } } else return null; return result; }
/// <summary> /// 轉換 Json 資料 /// </summary> /// <param name="data"></param> /// <returns></returns> private Dictionary<string, BasicInfo> JsonToGameList(string data) { Dictionary<string, BasicInfo> infoList = new Dictionary<string, BasicInfo>(); if (!String.IsNullOrEmpty(data)) { JObject json = JObject.Parse(data); foreach (JProperty prop in json.Children()) { // 聯盟 string alliance = prop.Name; foreach (JProperty game in prop.Values()) { string webID = game.Name; JObject detail = game.Value as JObject; DateTime gameTime = Convert.ToDateTime(String.Format("{0} {1}", detail["GameDate"], detail["GameTime"])); string state = detail["GameStates"].ToString(); string statusText = (detail["StatusText"] ?? String.Empty).ToString(); string trackerText = (detail["trackerText"] ?? String.Empty).ToString(); string record = (detail["Record"] ?? String.Empty).ToString(); string teamA = detail["TeamA"].ToString(); string teamB = detail["TeamB"].ToString(); string[] runsA = detail["RunsA"].ToString().Trim().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); string[] runsB = detail["RunsB"].ToString().Trim().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); string ptAway = detail["RA"].ToString(); string ptHome = detail["RB"].ToString(); BasicInfo info = new BasicInfo(AllianceID, GameType, gameTime, webID); info.AllianceName = alliance; info.GameStates = state; info.Status = statusText; info.TrackerText = trackerText; info.Record = record; info.AcH = AcH; // 主隊互換 if (!AcH) { //info.Away = teamB; //info.Home = teamA; //info.AwayBoard.AddRange(runsB); //info.HomeBoard.AddRange(runsA); //info.AwayPoint = ptHome; //info.HomePoint = ptAway; } else { info.Away = teamA; info.Home = teamB; info.AwayBoard.AddRange(runsA); info.HomeBoard.AddRange(runsB); info.AwayPoint = ptAway; info.HomePoint = ptHome; } infoList[info.WebID] = info; } } } return infoList; }
//private bool CheckGame(BasicInfo oldInfo, BasicInfo newInfo) //{ // bool isLegal = true; // //先检查新旧资料是否相同,不相同再比较比赛状态、分数 // if (!newInfo.ToString().Equals(oldInfo.ToString())) // { // //旧资料是未开赛,还没有比分,数据合法 // if (oldInfo.GameStates == "X")// && newInfo.GameStates == "X") // { // isLegal = true; // } // //旧资料是未开赛,还没有比分,数据合法 // //else if (newInfo.GameStates != "X") // //{ // // isLegal = false; // //} // else // { // if (int.Parse(oldInfo.AwayPoint) > int.Parse(newInfo.AwayPoint)) // { // isLegal = false; // } // else // { // if (int.Parse(oldInfo.HomePoint) > int.Parse(newInfo.HomePoint)) // { // isLegal = false; // } // } // } // } // return isLegal; //} /// <summary> /// 是否正常比分 /// 如果新的分数小于旧的分 就返回false (不正常的分数)并且把旧的分数赋值给新的 /// </summary> /// <param name="oldInfo">旧的资料</param> /// <param name="newInfo">新的资料</param> /// <returns></returns> private bool IsNormalScore(BasicInfo oldInfo, BasicInfo newInfo) { bool ret = true; if (!newInfo.ToString().Equals(oldInfo.ToString())) { if (oldInfo.HomeBoard != null && oldInfo.AwayBoard != null && newInfo.AwayBoard != null && newInfo.HomeBoard != null) { int oldHomeCount = 0, oldAwayCount = 0, newHomeCount = 0, newAwayCount = 0; oldHomeCount = oldInfo.HomeBoard.Select<string, int>(q => Convert.ToInt32(q)).Sum(); oldAwayCount = oldInfo.AwayBoard.Select<string, int>(q => Convert.ToInt32(q)).Sum(); newHomeCount = newInfo.HomeBoard.Select<string, int>(q => Convert.ToInt32(q)).Sum(); newAwayCount = newInfo.AwayBoard.Select<string, int>(q => Convert.ToInt32(q)).Sum(); //foreach (string item in oldInfo.HomeBoard) //{ // int o=0; // int.TryParse(item, out o); // oldHomeCount += o; //} if (newHomeCount < oldHomeCount || newAwayCount < oldAwayCount) { newInfo.HomeBoard = oldInfo.HomeBoard; newInfo.AwayBoard = oldInfo.AwayBoard; ret = false; } } } else { ret = false; } return ret; }
//取得数据库中的今日比赛资料 private void GetOldGames() { DateTime gameDate = this.GameDate; BasicInfo info = new BasicInfo(AllianceID, GameType, gameDate, gameDate.ToString(DATE_STRING_FORMAT)); info.Away = "BF篮球"; info.Home = "跟盤"; info.Status = this.GetData(frmMain.ConnectionString, info); this.GameData[info.WebID] = info; }
public override bool Update(string connectionString, BasicInfo info) { // 多來源跟分 return this.Update4(connectionString, info); }
//取得遺漏比分 private void GetGameScore(BasicInfo game, int inning) { List<string> AwayBoard = new List<string>(); List<string> HomeBoard = new List<string>(); if (this.GameData.ContainsKey(game.WebID)) { AwayBoard = this.GameData[game.WebID].AwayBoard; HomeBoard = this.GameData[game.WebID].HomeBoard; } else { DataTable dt = GetGameDBInfo(game.WebID);//從資料庫取得舊資料 if (dt != null && dt.Rows.Count == 1) { if (dt.Rows[0]["RunsA"] != null && dt.Rows[0]["RunsB"] != null) { string RunsA = dt.Rows[0]["RunsA"].ToString(); AwayBoard = new List<string>(RunsA.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)); string RunsB = dt.Rows[0]["RunsB"].ToString(); HomeBoard = new List<string>(RunsB.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)); } } } if (AwayBoard.Count > 0 && HomeBoard.Count > 0) { for (int i = 0; i < inning; i++) { string oldscore = AwayBoard[i]; if (!string.IsNullOrEmpty(oldscore)) game.AwayBoard.Add(oldscore); oldscore = HomeBoard[i]; if (!string.IsNullOrEmpty(oldscore)) game.HomeBoard.Add(oldscore); } } }
/// <summary> /// 取得即時比分資料。(奧訊) /// </summary> /// <param name="html">下載的內容</param> /// <returns></returns> protected Dictionary<string, BasicInfo> GetChangeByBet007Basketball(string html, string url = null) { // 沒有資料就離開 if (string.IsNullOrEmpty(html)) return null; Dictionary<string, BasicInfo> result = new Dictionary<string, BasicInfo>(); BasicInfo gameInfo = null; DateTime gameDate = this.GameDate; XmlAdapter xmlAdapter = null; try { xmlAdapter = new XmlAdapter(html, false); } catch (Exception e) { string msg = String.Format("解析網頁資料錯誤。{0}Url: {1}{0}Content: {2}{0}", Environment.NewLine, (url ?? String.Empty), html); throw new Exception(msg, e); } if (xmlAdapter == null) { return null; } xmlAdapter.GoToNode("c"); // 所有比賽集合 List<string> gameRecord = xmlAdapter.GetAllSubColumns("h"); foreach (string game in gameRecord) { #region 取出資料 // 0:賽事ID/1:狀態/2:小節剩餘時間/3:主隊總分/4:客隊總分 // 5:主隊1節得分/6:客隊1節得分/7:主隊2節得分/8:客隊2節得分/9:主隊3節得分/10:客隊3節得分/11:主隊4節得分/12:客隊4節得分/13:加時數 // 15:分節數/16:主隊ot1得分/17:客隊ot1得分/18:主隊ot2得分/19:客隊ot2得分/20:主隊ot3得分/21:客隊ot3得分 string[] data = game.Split('^'); #endregion gameInfo = null; gameInfo = new BasicInfo(this.AllianceID, this.GameType, gameDate, data[0], true); #region 比賽狀態 // 局數 int innings = 0; // 比賽狀態 string state = data[1]; // 剩餘時間 string remainingTime = data[2].Trim(); // 分節數 ( 2: 上下半場, 4: 4 小節 ) int classType = Int32.Parse(data[15]); switch (state) { case "1": case "2": case "3": case "4": case "5": case "6": case "7": innings = Int32.Parse(state); innings = (innings > 4) ? 4 : innings; // 超過4局, 表示 OT, 取4局 gameInfo.Status = "0".Equals(remainingTime) ? "結束" : remainingTime; // 剩餘時間 gameInfo.GameStates = "S"; break; case "50": // 中場 (上下半場: 1局, 4小節: 2局) innings = (classType == 2) ? 1 : 2; gameInfo.Status = "中場休息"; gameInfo.GameStates = "S"; break; case "-1": innings = 4; gameInfo.Status = "結束"; gameInfo.GameStates = "E"; break; case "-2": // 待定 gameInfo.Status = ""; gameInfo.TrackerText = "只顯示最終比分"; gameInfo.Record = "只顯示最終比分"; gameInfo.GameStates = "X"; break; case "-3": innings = 4; gameInfo.Status = "中止"; gameInfo.GameStates = "P"; break; case "-4": innings = 4; gameInfo.Status = "取消"; gameInfo.GameStates = "C"; break; case "-5": innings = 4; gameInfo.Status = "延遲"; gameInfo.GameStates = "D"; break; } #endregion #region 分數 // 算四小節分數 for (int i = 0; i < innings; i++) { string ptHome = data[5 + 2 * i].Trim(); string ptAway = data[6 + 2 * i].Trim(); if (!String.IsNullOrEmpty(ptAway) && !String.IsNullOrEmpty(ptHome)) { gameInfo.AwayBoard.Add(ptAway); gameInfo.HomeBoard.Add(ptHome); } } // 取得 OT 數 string otCount = data[13].Trim(); if (!String.IsNullOrEmpty(otCount)) { // 取得 OT 比分 int inningOT = Int32.Parse(otCount); for (int i = 0; i < inningOT; i++) { string otHome = StringHelper.IsNullOrEmptyToZero(data[16 + 2 * i]); string otAway = StringHelper.IsNullOrEmptyToZero(data[17 + 2 * i]); gameInfo.AwayBoard.Add(otAway); gameInfo.HomeBoard.Add(otHome); } } // 總分 gameInfo.AwayPoint = StringHelper.IsNullOrEmptyToZero(data[4]); gameInfo.HomePoint = StringHelper.IsNullOrEmptyToZero(data[3]); #endregion gameInfo.Display = 1; // 加入 result[gameInfo.WebID] = gameInfo; } return result; }
public override int Follow() { // 沒有資料就離開 if (this.DownWeb.Document == null || this.DownWeb.Document.Body == null) return 0; // 不是 Live 就離開 if (!this.FollowByWebIsLive) return 0; int result = 0; BasicInfo gameInfo = null; HtmlElementCollection divDoc = this.DownWeb.Document.Body.GetElementsByTagName("span"); // 資料 foreach (HtmlElement div in divDoc) { if (div.Id != null && div.Id.IndexOf("board") == 0) { // 跟盤 ID string webID = div.Id.Replace("board", "").Trim(); List<List<string>> teamBoard = this.FollowByWebTeamBoard(webID); // 判斷分數 if (teamBoard != null && teamBoard.Count > 0 && teamBoard[1].Count > 0) { // 建立比賽資料 gameInfo = null; gameInfo = new BasicInfo(this.AllianceID, this.GameType, this.GameDate, webID); gameInfo.Away = "Away"; gameInfo.Home = "Home"; #region 分數 // 客隊 for (int i = 0; i < teamBoard[1].Count; i++) { gameInfo.AwayBoard.Add(teamBoard[1][i]); } // 總分 gameInfo.AwayPoint = teamBoard[1][teamBoard[1].Count - 1]; gameInfo.AwayBoard.RemoveAt(gameInfo.AwayBoard.Count - 1); // 主隊 for (int i = 0; i < teamBoard[2].Count; i++) { gameInfo.HomeBoard.Add(teamBoard[2][i]); } // 總分 gameInfo.HomePoint = teamBoard[2][teamBoard[2].Count - 1]; gameInfo.HomeBoard.RemoveAt(gameInfo.HomeBoard.Count - 1); #endregion #region 比賽狀態 if (teamBoard[0][0].ToLower().IndexOf("final") != -1) { gameInfo.GameStates = "E"; gameInfo.Status = "結束"; } /* else if (teamBoard[0][0].ToLower().IndexOf("postponed") != -1) { gameInfo.GameStates = "P"; gameInfo.Status = "中止"; } else if (teamBoard[0][0].ToLower().IndexOf("delay") != -1) { gameInfo.GameStates = "D"; gameInfo.Status = "DELAY"; } else if (gameInfo.Quarter > 0) { DateTime dtTimer = DateTime.Now; // 轉換成時間 if (DateTime.TryParse(teamBoard[0][0], out dtTimer)) { gameInfo.GameStates = "S"; gameInfo.Status = teamBoard[0][0]; } else { // 比賽未開始 gameInfo.AwayBoard.Clear(); gameInfo.AwayPoint = ""; gameInfo.HomeBoard.Clear(); gameInfo.HomePoint = ""; } }*/ else { gameInfo.GameStates = "S"; gameInfo.Status = teamBoard[0][0]; } #endregion // 加入 this.GameData[gameInfo.WebID] = gameInfo; // 累計 result++; } } } // 傳回 return result; }
protected Dictionary<string, BasicInfo> GetDataBKBF(string html, ref DateTime dateTime, bool AcH = false) { // 沒有資料就離開 if (string.IsNullOrEmpty(html)) return null; Dictionary<string, BasicInfo> result = new Dictionary<string, BasicInfo>(); BasicInfo gameInfo = null; // 判斷資料 string[] all = html.Split(new string[] { "¬~ZA÷" }, StringSplitOptions.RemoveEmptyEntries); // 聯盟 (第一筆是多餘的) for (int allianceIndex = 1; allianceIndex < all.Length; allianceIndex++) { // 比賽集合 string[] games = ("ZA÷" + all[allianceIndex]).Split(new string[] { "~" }, StringSplitOptions.RemoveEmptyEntries); string allianceName = null; // 聯盟資料 // 比賽資料 for (int gameIndex = 0; gameIndex < games.Length; gameIndex++) { Dictionary<string, string> info = new Dictionary<string, string>(); #region 取出資料 string[] data = games[gameIndex].Split(new string[] { "¬" }, StringSplitOptions.RemoveEmptyEntries); // 資料 foreach (string d in data) { string[] txt = d.Split(new string[] { "÷" }, StringSplitOptions.RemoveEmptyEntries); // 記錄 info[txt[0]] = txt[1]; } #endregion #region 第一筆是聯盟 if (gameIndex == 0) { allianceName = info["ZA"]; continue; } else { // 沒有編號就往下處理 if (!info.ContainsKey("AA")) continue; } #endregion // 不是正確的聯盟就離開 //if (allianceName.ToLower().IndexOf(find1) == -1 || // allianceName.ToLower().IndexOf(find2) == -1) // break; // 沒有隊伍就往下處理 if (!info.ContainsKey("AE") || !info.ContainsKey("AF")) continue; // 時間是 1970 年加上 Ti DateTime gameTime = DateTime.Parse("1970/1/1 00:00:00").AddTicks(long.Parse(info["AD"]) * 10000000); // 轉成台灣時間 UTC+8 gameTime = gameTime.AddHours(8); dateTime = gameTime.Date; // 建立比賽資料 gameInfo = null; gameInfo = new BasicInfo(this.AllianceID, this.GameType, gameTime, info["AA"]); gameInfo.Away = info["AE"].Replace("GOAL", ""); gameInfo.Home = info["AF"].Replace("GOAL", ""); // 2014/04/18, 新增聯盟(含賽別) gameInfo.AllianceName = allianceName; #region 分數 if (info.ContainsKey("AG")) { string[] nums = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" }; for (int i = 0; i < nums.Length; i += 2) { // 沒有資料就離開 if (!info.ContainsKey("B" + nums[i])) break; // 分數 gameInfo.AwayBoard.Add(info["B" + nums[i]]); gameInfo.HomeBoard.Add(info["B" + nums[i + 1]]); } //if (gameInfo.Away == "Houston Rockets" || gameInfo.Home == "Houston Rockets") //{ // string str = "gameInfo"; //} gameInfo.AwayPoint = info["AG"]; gameInfo.HomePoint = info["AH"]; } #endregion #region 比賽狀態 switch (info["AB"]) { //只显示最后结果的赛事修改成速报要抓取到讯息“只显示最终比分”,状态要为“结束” case "1": if (DateTime.Compare(gameTime, DateTime.Now) <= 0)//比賽時間到了才顯示已結束 { gameInfo.TrackerText = "只顯示最終比分"; gameInfo.GameStates = "E"; } break; case "2": gameInfo.GameStates = "S"; // 狀態 if (info.ContainsKey("AC") && (info["AC"] == "38" || info["AC"] == "46")) { gameInfo.Status = "中場休息"; } if (info.ContainsKey("BX")) { // 剩餘時間 int playMinute = 10; //NBA、CBA、NBA發展聯盟、Philippine Cup比賽12分鐘,WCBA10分鐘 if (gameInfo.AllianceName.IndexOf("NBA") >= 0 || (gameInfo.AllianceName.IndexOf("CBA") >= 0 && gameInfo.AllianceName.IndexOf("WCBA") == -1) || gameInfo.AllianceName.IndexOf("Philippine Cup") >= 0) { playMinute = 12; } gameInfo.Status = (playMinute - int.Parse(info["BX"])).ToString(); } break; case "3": if (info.ContainsKey("AC")) { switch (info["AC"]) { case "4": case "5": gameInfo.GameStates = "P"; gameInfo.Status = "中止"; break; default: gameInfo.GameStates = "E"; gameInfo.Status = "結束"; break; } } else { gameInfo.GameStates = "E"; gameInfo.Status = "結束"; } break; } if (info["AB"] != "1")//防止一开始没有确定有没有比分源,所以备注fro,开赛前确定后就把备注去掉了的情况发生 { gameInfo.TrackerText = ""; } if (gameInfo.GameStates == "X") { gameInfo.HomePoint = "0"; gameInfo.AwayPoint = "0"; } gameInfo.AcH = AcH; #endregion // 加入 result[gameInfo.WebID] = gameInfo; } } // 傳回 return result; }
public override int Follow() { // 沒有資料就離開 if (string.IsNullOrEmpty(this.DownHome.Data)) return 0; if (!this.DownHome.HasChanged) return 0; int result = 0; BasicInfo gameInfo = null; string[] javasctript = this.DownHome.Data.Replace("\r\n", "").Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries); string[] data = null; string webID = null; DateTime gameTime = DateTime.Now; for (int index = 1; index < javasctript.Length; index++) { // 找到正確的比賽 if (javasctript[index].IndexOf("韓甲") == -1) continue; // 取出資料 webID = javasctript[index].Substring(0, javasctript[index].IndexOf("=")); data = javasctript[index].Substring(javasctript[index].IndexOf("=") + 2).Replace("'", "").Split(','); // 正確的跟盤 ID webID = webID.Replace("bDt", "").Replace("[", "").Replace("]", ""); // 取出時間 if (DateTime.TryParse(data[1] + "/" + data[2] + "/" + data[3] + " " + data[4] + ":" + data[5] + ":" + data[6], out gameTime)) { // 建立比賽資料 gameInfo = null; gameInfo = new BasicInfo(this.AllianceID, this.GameType, gameTime, webID); //gameInfo.AcH = true; gameInfo.Away = data[11]; gameInfo.Home = data[13]; // 比賽狀態 if (data[0] != "0") { // 分數 // Away for (var i = 15; i < 20; i++) { gameInfo.AwayBoard.Add(data[i]); } gameInfo.AwayPoint = data[21]; // Home for (var i = 22; i < 27; i++) { gameInfo.HomeBoard.Add(data[i]); } gameInfo.HomePoint = data[28]; // 判斷狀態 switch (data[0]) { case "9": // 比賽結束 gameInfo.GameStates = "E"; // 移除其它分數 gameInfo.AwayBoard.RemoveAt(gameInfo.AwayBoard.Count - 1); gameInfo.HomeBoard.RemoveAt(gameInfo.HomeBoard.Count - 1); gameInfo.Status = "結束"; break; case "11": // 比賽結束(加時) gameInfo.GameStates = "E"; gameInfo.Status = "結束"; break; case "12": // 中斷 gameInfo.GameStates = "P"; break; case "13": // 取消 gameInfo.GameStates = "C"; break; case "10": // 加時 gameInfo.GameStates = "S"; break; case "1": // 第一節 case "2": gameInfo.GameStates = "S"; // 移除其它分數 gameInfo.AwayBoard.RemoveRange(1, gameInfo.AwayBoard.Count - 1); gameInfo.HomeBoard.RemoveRange(1, gameInfo.HomeBoard.Count - 1); gameInfo.Status = data[0] == "2" ? "結束" : ""; break; case "3": // 第二節 case "4": gameInfo.GameStates = "S"; // 移除其它分數 gameInfo.AwayBoard.RemoveRange(2, gameInfo.AwayBoard.Count - 2); gameInfo.HomeBoard.RemoveRange(2, gameInfo.HomeBoard.Count - 2); gameInfo.Status = data[0] == "4" ? "結束" : ""; break; case "5": // 第三節 case "6": gameInfo.GameStates = "S"; // 移除其它分數 gameInfo.AwayBoard.RemoveRange(3, gameInfo.AwayBoard.Count - 3); gameInfo.HomeBoard.RemoveRange(3, gameInfo.HomeBoard.Count - 3); gameInfo.Status = data[0] == "6" ? "結束" : ""; break; case "7": // 第四節 case "8": gameInfo.GameStates = "S"; // 移除其它分數 gameInfo.AwayBoard.RemoveRange(4, gameInfo.AwayBoard.Count - 4); gameInfo.HomeBoard.RemoveRange(4, gameInfo.HomeBoard.Count - 4); gameInfo.Status = data[0] == "8" ? "結束" : ""; break; } } // 加入 this.GameData[gameInfo.WebID] = gameInfo; // 累計 result++; } } // 傳回 return result; }
/// <summary> /// 取到资料 ABL 澳洲棒球 /// </summary> /// <param name="xml">下載的內容</param> protected Dictionary<string, BasicInfo> GetDataByABLGame(string xml, string url = null) { // 沒有資料就離開 if (string.IsNullOrEmpty(xml)) return null; Dictionary<string, BasicInfo> result = new Dictionary<string, BasicInfo>(); BasicInfo gameInfo = null; XmlDocument doc = new XmlDocument(); try { doc.LoadXml(xml); } catch (Exception e) { string msg = String.Format("解析網頁資料錯誤。{0}Url: {1}{0}Content: {2}{0}", Environment.NewLine, (url ?? String.Empty), xml); throw new Exception(msg, e); } XmlElement rootElem = doc.DocumentElement; //获取到ABL联盟的gamelist XmlNodeList personNodes = rootElem.SelectNodes("//game[@league='ABL']"); //检查有没有读到资料 if (personNodes == null && personNodes.Count == 0) { return null; } //循环game节点 foreach (XmlNode node in personNodes) { //node 为game节点 XmlElement game = (XmlElement)node; string webID = game.GetAttribute("id").Trim(); gameInfo = new BasicInfo(this.AllianceID, this.GameType, this.GameDate, webID); //status 节点元素 XmlElement status = (XmlElement)game.SelectSingleNode("./status"); //linescore 节点 XmlNode linescore = game.SelectSingleNode("./linescore"); //队伍名称 【城市 队名】中间空格 gameInfo.Home = string.Format("{0} {1}", game.GetAttribute("home_team_city"), game.GetAttribute("home_team_name")); gameInfo.Away = string.Format("{0} {1}", game.GetAttribute("away_team_city"), game.GetAttribute("away_team_name")); if (linescore != null) { #region 分數 foreach (XmlNode item in linescore.SelectNodes("./inning")) { XmlElement linning = (XmlElement)item; string tempA = linning.GetAttribute("away"); string tempH = linning.GetAttribute("home"); gameInfo.AwayBoard.Add(tempA); gameInfo.HomeBoard.Add(tempH); } #endregion #region RHE XmlElement r = (XmlElement)linescore.SelectSingleNode("./r"); XmlElement h = (XmlElement)linescore.SelectSingleNode("./h"); XmlElement e = (XmlElement)linescore.SelectSingleNode("./e"); if (r != null) { gameInfo.AwayPoint = r.GetAttribute("away"); gameInfo.HomePoint = r.GetAttribute("home"); } if (h != null) { gameInfo.AwayH = h.GetAttribute("away"); gameInfo.HomeH = h.GetAttribute("home"); } if (e != null) { gameInfo.AwayE = e.GetAttribute("away"); gameInfo.HomeE = e.GetAttribute("home"); } #endregion } #region BSOB //壘包節點 XmlNode runners_on_base = game.SelectSingleNode("./runners_on_base"); int iBase = 0; if (runners_on_base != null) { if (runners_on_base.SelectSingleNode("runner_on_1b") != null) { iBase += 1; } if (runners_on_base.SelectSingleNode("runner_on_2b") != null) { iBase += 2; } if (runners_on_base.SelectSingleNode("runner_on_3b") != null) { iBase += 4; } } gameInfo.Bases = iBase; int o = 0, s = 0, b = 0; if (status.GetAttribute("o") != null && int.TryParse(status.GetAttribute("o").Trim(), out o)) { gameInfo.BallO = o; } if (status.GetAttribute("s") != null && int.TryParse(status.GetAttribute("s").Trim(), out s)) { gameInfo.BallS = s; } if (status.GetAttribute("b") != null && int.TryParse(status.GetAttribute("b").Trim(), out b)) { gameInfo.BallB = b; } #endregion #region 状态 switch (status.GetAttribute("status").ToLower().Trim()) { case "final": case "game over": case "gameover": case "completed early": gameInfo.GameStates = "E"; gameInfo.Status = "結束"; gameInfo.BallB = 0; gameInfo.BallS = 0; gameInfo.BallO = 0; gameInfo.Bases = 0; // 補上 X if (gameInfo.AwayBoard.Count > gameInfo.HomeBoard.Count) { gameInfo.HomeBoard.Add("X"); } else if (string.IsNullOrEmpty(gameInfo.HomeBoard[gameInfo.HomeBoard.Count - 1])) { gameInfo.HomeBoard[gameInfo.HomeBoard.Count - 1] = "X"; } break; case "postponed": // 中止 gameInfo.GameStates = "P"; gameInfo.Status = "中止"; gameInfo.BallB = 0; gameInfo.BallS = 0; gameInfo.BallO = 0; gameInfo.Bases = 0; break; case "cancelled": // 取消 gameInfo.GameStates = "C"; gameInfo.Status = "取消"; gameInfo.BallB = 0; gameInfo.BallS = 0; gameInfo.BallO = 0; gameInfo.Bases = 0; break; case "suspended": // 暫停 case "delayed": // 延遲 gameInfo.GameStates = "D"; gameInfo.Status = "延遲"; break; case "delayed start": // 延遲 gameInfo.GameStates = "D"; gameInfo.Status = "延遲"; gameInfo.BallB = 0; gameInfo.BallS = 0; gameInfo.BallO = 0; gameInfo.Bases = 0; break; case "warmup": //熱身 gameInfo.GameStates = "W"; gameInfo.Status = "準備開賽"; gameInfo.BallB = 0; gameInfo.BallS = 0; gameInfo.BallO = 0; gameInfo.Bases = 0; break; case "pre-game": gameInfo.GameStates = "W"; gameInfo.Status = "賽前"; gameInfo.BallB = 0; gameInfo.BallS = 0; gameInfo.BallO = 0; gameInfo.Bases = 0; break; //case "In Progress"://比賽中 // break; default: // 有分數才是比賽開始 if (gameInfo.Quarter > 0) { gameInfo.GameStates = "S"; // 分數判斷 if (string.IsNullOrEmpty(gameInfo.HomeBoard[gameInfo.HomeBoard.Count - 1])) { gameInfo.HomeBoard[gameInfo.HomeBoard.Count - 1] = "0"; } if (string.IsNullOrEmpty(gameInfo.AwayBoard[gameInfo.AwayBoard.Count - 1])) { gameInfo.AwayBoard[gameInfo.AwayBoard.Count - 1] = "0"; } // 移除下半局 if (status.GetAttribute("inning_state") != null && status.GetAttribute("inning_state").ToString().ToLower() == "top") { if (gameInfo.AwayBoard.Count == gameInfo.HomeBoard.Count) { gameInfo.HomeBoard.RemoveAt(gameInfo.HomeBoard.Count - 1); } } // 其它判斷 if (gameInfo.BallB > 3) { gameInfo.BallB = 3; } if (gameInfo.BallS > 2) { gameInfo.BallS = 2; } if (gameInfo.BallO > 2) { gameInfo.BallO = 2; } } break; } //讯息 gameInfo.TrackerText = status.GetAttribute("reason").ToLower(); #endregion result[gameInfo.WebID] = gameInfo; } return result; }
private int FollowByWeb() { // 沒有資料就離開 if (string.IsNullOrEmpty(this.DownHome.Data)) return 0; int result = 0; BasicInfo gameInfo = null; HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument(); //string xPath = "/html[1]/body[1]/div[1]/div[2]/div[3]/div[8]/table[1]"; // 載入資料 document.LoadHtml(this.DownHome.Data); // 資料位置 HtmlAgilityPack.HtmlNode contentNode = document.GetElementbyId("contents"); if (contentNode == null) { return 0; } HtmlAgilityPack.HtmlNode nodeGames = contentNode.SelectSingleNode("//div[contains(@class,'NpbSchedule')]/table"); // 判斷資料 if (nodeGames != null && nodeGames.ChildNodes != null) { DateTime gameDate = this.GameDate; DateTime gameTime = this.GameDate; string gameDateStr = null; // 資料 foreach (HtmlAgilityPack.HtmlNode game in nodeGames.ChildNodes) { // 不是資料就往下處理 if (game.Name != "tr") continue; string webID = null; string webUrl = null; string webStatus = null; int timeIndex = 8; int idIndex = 6; int awayIndex = 0; int homeIndex = 4; #region 取得日期 if (game.ChildNodes.Count == 15) { gameDateStr = game.ChildNodes[1].InnerText.Replace("月", "/").Replace("日", "").Trim(); // 截斷日期 if (gameDateStr.IndexOf("(") != -1) gameDateStr = DateTime.Now.ToString("yyyy") + "/" + gameDateStr.Substring(0, gameDateStr.IndexOf("(")).Trim(); // 判斷日期,失敗就往下處理 if (!DateTime.TryParse(gameDateStr, out gameDate)) { gameDateStr = null; continue; } timeIndex = 11; idIndex = 9; awayIndex = 3; homeIndex = 7; } // 沒有日期就往下處理 if (string.IsNullOrEmpty(gameDateStr)) continue; // 錯誤的陣列就往下處理 if (timeIndex >= game.ChildNodes.Count) continue; #endregion #region 取得時間 // 判斷日期,錯誤就離開 string timeStr = game.ChildNodes[timeIndex].InnerText; // 取出資料 timeStr = timeStr.Substring(0, timeStr.IndexOf(" ")); if (!DateTime.TryParse(gameDate.ToString("yyyy/MM/dd") + " " + timeStr, out gameTime)) continue; // 判斷是否為今天,不是今天就往下處理 if (this.GameDate.Date != gameTime.Date) { gameDateStr = null; continue; } #endregion #region 跟盤 ID webID = game.ChildNodes[idIndex].ChildNodes[0].GetAttributeValue("href", ""); webStatus = game.ChildNodes[idIndex].ChildNodes[0].InnerText; Uri uri = null; // 錯誤處理 try { uri = new Uri(@"http://www.a.com" + webID); // 判斷是否有資料 if (uri.Segments.Length == 4) { webID = uri.Segments[3].Replace("/", ""); if (webStatus == "中止") { webUrl = string.Format(sWebUrl5, webID); } else { webUrl = string.Format(sWebUrl6, webID); } } } catch { webID = null; } // 錯誤,清除跟盤 ID // 沒有跟盤 ID 就往下處理 if (webID == null || string.IsNullOrEmpty(webID.Trim())) { gameDateStr = null; continue; } #endregion // 建立比賽資料 gameInfo = null; gameInfo = new BasicInfo(this.AllianceID, this.GameType, gameTime, webID); gameInfo.IsBall = true; // 賽程的主客隊與資料是相反的 gameInfo.Away = game.ChildNodes[homeIndex].InnerText; gameInfo.AwayPoint = ""; gameInfo.Home = game.ChildNodes[awayIndex].InnerText; gameInfo.HomePoint = ""; gameInfo.GameStates = (webStatus == "中止") ? ("P") : ("X"); #region 下載比賽資料,比賽時間 10 分鐘前就開始處理 if (!this.DownReal.ContainsKey(webID) && GetUtcKr(DateTime.Now) >= gameTime.AddMinutes(-10)) { this.DownReal[webID] = new BasicDownload(this.Sport, webUrl, webID); this.DownReal[webID].DownloadString(); } #endregion #region 處理比賽資料 if (this.DownReal.ContainsKey(webID) && !string.IsNullOrEmpty(this.DownReal[webID].Data)) { HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); HtmlAgilityPack.HtmlNode node = null; // 讀取資料 doc.LoadHtml(this.DownReal[webID].Data); // 判斷是中止 if (gameInfo.GameStates == "P") { string findxpath = FindXPath(doc.DocumentNode, "yjSNLiveBattlereview"); if (!string.IsNullOrEmpty(findxpath)) { node = doc.DocumentNode.SelectSingleNode(findxpath); if (node != null && node.ChildNodes.Count > 0) { // 原因 gameInfo.TrackerText = node.ChildNodes[node.ChildNodes.Count - 2].InnerText; } } } else { #region 分數 node = doc.DocumentNode.SelectSingleNode(FindXPath(doc.DocumentNode, "scoreboard")); if (node != null && node.ChildNodes.Count >= 1) { // Away foreach (HtmlAgilityPack.HtmlNode data in node.ChildNodes[1].ChildNodes[1].ChildNodes[2].ChildNodes) { // 不是資料就往下處理 if (data.Name != "td") continue; // 分數 gameInfo.AwayBoard.Add(data.InnerText.Replace(" ", "")); } gameInfo.AwayPoint = gameInfo.AwayBoard[gameInfo.AwayBoard.Count - 3]; gameInfo.AwayH = gameInfo.AwayBoard[gameInfo.AwayBoard.Count - 2]; gameInfo.AwayE = gameInfo.AwayBoard[gameInfo.AwayBoard.Count - 1]; gameInfo.AwayBoard.RemoveAt(gameInfo.AwayBoard.Count - 1); gameInfo.AwayBoard.RemoveAt(gameInfo.AwayBoard.Count - 1); gameInfo.AwayBoard.RemoveAt(gameInfo.AwayBoard.Count - 1); // Home foreach (HtmlAgilityPack.HtmlNode data in node.ChildNodes[1].ChildNodes[1].ChildNodes[4].ChildNodes) { // 不是資料就往下處理 if (data.Name != "td") continue; gameInfo.HomeBoard.Add(data.InnerText.Replace(" ", "")); } gameInfo.HomePoint = gameInfo.HomeBoard[gameInfo.HomeBoard.Count - 3]; gameInfo.HomeH = gameInfo.HomeBoard[gameInfo.HomeBoard.Count - 2]; gameInfo.HomeE = gameInfo.HomeBoard[gameInfo.HomeBoard.Count - 1]; gameInfo.HomeBoard.RemoveAt(gameInfo.HomeBoard.Count - 1); gameInfo.HomeBoard.RemoveAt(gameInfo.HomeBoard.Count - 1); gameInfo.HomeBoard.RemoveAt(gameInfo.HomeBoard.Count - 1); } // 移除無用的分數 if (gameInfo.Quarter > 0) { int index = gameInfo.AwayBoard.Count - 1; while (index >= 0) { if (gameInfo.AwayBoard[index] == "-") gameInfo.AwayBoard[index] = "0"; if (gameInfo.HomeBoard[index] == "-") gameInfo.HomeBoard[index] = "0"; if (string.IsNullOrEmpty(gameInfo.AwayBoard[index])) gameInfo.AwayBoard.RemoveAt(index); if (string.IsNullOrEmpty(gameInfo.HomeBoard[index])) gameInfo.HomeBoard.RemoveAt(index); index--; } gameInfo.GameStates = "S"; } #endregion #region BSO node = doc.DocumentNode.SelectSingleNode(FindXPath(doc.DocumentNode, "sbo")); if (node != null && node.ChildNodes.Count >= 1) { string txt = null; int bStart = 0; int bEnd = 0; // B txt = node.ChildNodes[3].ChildNodes[3].ChildNodes[1].InnerHtml.Replace("\n", ""); bStart = txt.IndexOf("<b>"); bEnd = txt.IndexOf("</b>"); if (bStart != -1 && bEnd != -1) { gameInfo.BallB = txt.Substring(bStart + 3, bEnd - bStart - 3).Length; if (gameInfo.BallB > 3) gameInfo.BallB = 0; } // S txt = node.ChildNodes[3].ChildNodes[3].ChildNodes[3].InnerHtml.Replace("\n", ""); bStart = txt.IndexOf("<b>"); bEnd = txt.IndexOf("</b>"); if (bStart != -1 && bEnd != -1) { gameInfo.BallS = txt.Substring(bStart + 3, bEnd - bStart - 3).Length; if (gameInfo.BallS > 2) gameInfo.BallS = 0; } // O txt = node.ChildNodes[3].ChildNodes[3].ChildNodes[5].InnerHtml.Replace("\n", ""); bStart = txt.IndexOf("<b>"); bEnd = txt.IndexOf("</b>"); if (bStart != -1 && bEnd != -1) { gameInfo.BallO = txt.Substring(bStart + 3, bEnd - bStart - 3).Length; if (gameInfo.BallO > 2) { gameInfo.BallB = 0; gameInfo.BallS = 0; gameInfo.BallO = 0; } } } #endregion #region Bases node = doc.DocumentNode.SelectSingleNode(FindXPath(doc.DocumentNode, "base")); if (node != null && node.ChildNodes.Count >= 1) { if (!string.IsNullOrEmpty(FindXPath(doc.DocumentNode, "base1"))) { gameInfo.Bases += 1; } if (!string.IsNullOrEmpty(FindXPath(doc.DocumentNode, "base2"))) { gameInfo.Bases += 2; } if (!string.IsNullOrEmpty(FindXPath(doc.DocumentNode, "base3"))) { gameInfo.Bases += 4; } } #endregion #region 狀態 node = doc.DocumentNode.SelectSingleNode(FindXPath(doc.DocumentNode, "livenavi")); if (node != null) { if (node.InnerText.IndexOf("試合前") != -1) { gameInfo.GameStates = "X"; gameInfo.BallB = 0; gameInfo.BallS = 0; gameInfo.BallO = 0; gameInfo.Bases = 0; gameInfo.AwayPoint = ""; gameInfo.AwayBoard.Clear(); gameInfo.AwayH = null; gameInfo.AwayE = null; gameInfo.HomePoint = ""; gameInfo.HomeBoard.Clear(); gameInfo.HomeH = null; gameInfo.HomeE = null; } if (node.InnerText.IndexOf("試合終了") != -1) { gameInfo.GameStates = "E"; gameInfo.BallB = 0; gameInfo.BallS = 0; gameInfo.BallO = 0; gameInfo.Bases = 0; } } // 比賽結束 node = doc.DocumentNode.SelectSingleNode(FindXPath(doc.DocumentNode, "afterdata")); if (node != null && !string.IsNullOrEmpty(node.InnerText)) { gameInfo.GameStates = "E"; gameInfo.BallB = 0; gameInfo.BallS = 0; gameInfo.BallO = 0; gameInfo.Bases = 0; } #endregion } #endregion } if (base.CheckGame(gameInfo))//檢查比分是否合法 { this.GameData[gameInfo.WebID] = gameInfo;// 加入 result++;// 累計 } } } // 傳回 return result; }
/// <summary> /// 取得資料。(Asiascore) /// </summary> /// <param name="data">資料</param> /// <param name="find1">搜尋條件</param> /// <param name="find2">搜尋條件</param> /// <param name="playMinute">比賽的時間長度</param> protected Dictionary<string, BasicInfo> GetDataByAsiaScore(string html, string find1, string find2, int playMinute) { // 沒有資料就離開 if (string.IsNullOrEmpty(html)) return null; Dictionary<string, BasicInfo> result = new Dictionary<string, BasicInfo>(); BasicInfo gameInfo = null; DateTime gameDate = this.GameDate; // 判斷資料 string[] all = html.Split(new string[] { "¬~ZA÷" }, StringSplitOptions.RemoveEmptyEntries); // 聯盟 (第一筆是多餘的) for (int allianceIndex = 1; allianceIndex < all.Length; allianceIndex++) { // 比賽集合 string[] games = ("ZA÷" + all[allianceIndex]).Split(new string[] { "~" }, StringSplitOptions.RemoveEmptyEntries); string allianceName = null; // 聯盟資料 // 比賽資料 for (int gameIndex = 0; gameIndex < games.Length; gameIndex++) { Dictionary<string, string> info = new Dictionary<string, string>(); #region 取出資料 string[] data = games[gameIndex].Split(new string[] { "¬" }, StringSplitOptions.RemoveEmptyEntries); // 資料 foreach (string d in data) { string[] txt = d.Split(new string[] { "÷" }, StringSplitOptions.RemoveEmptyEntries); // 記錄 info[txt[0]] = txt[1]; } #endregion #region 第一筆是聯盟 if (gameIndex == 0) { allianceName = info["ZA"]; continue; } else { // 沒有編號就往下處理 if (!info.ContainsKey("AA")) continue; } #endregion // 不是正確的聯盟就離開 if (allianceName.ToLower().IndexOf(find1) == -1 || allianceName.ToLower().IndexOf(find2) == -1) break; // 沒有隊伍就往下處理 if (!info.ContainsKey("AE") || !info.ContainsKey("AF")) continue; // 時間是 1970 年加上 Ti DateTime gameTime = DateTime.Parse("1970/1/1 00:00:00").AddTicks(long.Parse(info["AD"]) * 10000000); // 轉成台灣時間 UTC+8 gameTime = gameTime.AddHours(8); // 建立比賽資料 gameInfo = null; gameInfo = new BasicInfo(this.AllianceID, this.GameType, gameDate, info["AA"]); gameInfo.Away = info["AE"].Replace("GOAL", ""); gameInfo.Home = info["AF"].Replace("GOAL", ""); // 2014/04/18, 新增聯盟(含賽別) gameInfo.AllianceName = allianceName; #region 分數 if (info.ContainsKey("AG")) { string[] nums = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" }; for (int i = 0; i < nums.Length; i += 2) { // 沒有資料就離開 if (!info.ContainsKey("B" + nums[i])) break; // 分數 gameInfo.AwayBoard.Add(info["B" + nums[i]]); gameInfo.HomeBoard.Add(info["B" + nums[i + 1]]); } gameInfo.AwayPoint = info["AG"]; gameInfo.HomePoint = info["AH"]; } #endregion #region 比賽狀態 switch (info["AB"]) { case "2": gameInfo.GameStates = "S"; // 狀態 if (info.ContainsKey("AC") && (info["AC"] == "38" || info["AC"] == "46")) { gameInfo.Status = "中場休息"; } if (info.ContainsKey("BX")) { // 剩餘時間 gameInfo.Status = (playMinute - int.Parse(info["BX"])).ToString(); } break; case "3": if (info.ContainsKey("AC")) { switch (info["AC"]) { case "4": case "5": case "36": gameInfo.GameStates = "P"; gameInfo.Status = "中止"; break; default: gameInfo.GameStates = "E"; gameInfo.Status = "結束"; break; } } else { gameInfo.GameStates = "E"; gameInfo.Status = "結束"; } break; } #endregion // 加入 result[gameInfo.WebID] = gameInfo; } } // 傳回 return result; }
public override int Follow() { // 沒有資料就離開 if (string.IsNullOrEmpty(this.DownHome.Data)) return 0; int result = 0; BasicInfo gameInfo = null; HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument(); string xPath = "/html[1]/body[1]/center[1]/table[1]/tr[3]/td[1]/td[1]/table[1]/tr[2]/td[1]/table[1]/tr[1]/td[1]/font[1]/select[1]"; // 載入資料 document.LoadHtml(this.DownHome.Data); // 資料位置 (年度) HtmlAgilityPack.HtmlNode nodeGames = document.DocumentNode.SelectSingleNode(xPath); // 判斷資料 if (nodeGames != null && nodeGames.ChildNodes.Count > 0) { string gameYear = null; string gameMonth = null; DateTime gameDate = this.GameDate; DateTime gameTime = this.GameDate; Dictionary<string, string> box = new Dictionary<string, string>(); #region 取出年度 foreach (HtmlAgilityPack.HtmlNode year in nodeGames.ChildNodes) { // 找出選擇 if (year.GetAttributeValue("selected", "") == "selected") { gameYear = year.GetAttributeValue("value", ""); break; } } #endregion #region 取出月份 xPath = "/html[1]/body[1]/center[1]/table[1]/tr[3]/td[1]/td[1]/table[1]/tr[2]/td[1]/table[2]/tr[1]/td[1]/table[1]/tr[1]/td[1]"; nodeGames = document.DocumentNode.SelectSingleNode(xPath); // 沒有資料就離開 if (nodeGames == null || string.IsNullOrEmpty(nodeGames.InnerText) || nodeGames.InnerText.LastIndexOf("月") == -1) { // 失敗就離開 return 0; } else { gameMonth = nodeGames.InnerText.Substring(0, nodeGames.InnerText.LastIndexOf("月")).Trim(); // 轉成正確的月份 if (gameMonth == "一") gameMonth = "1"; if (gameMonth == "二") gameMonth = "2"; if (gameMonth == "三") gameMonth = "3"; if (gameMonth == "四") gameMonth = "4"; if (gameMonth == "五") gameMonth = "5"; if (gameMonth == "六") gameMonth = "6"; if (gameMonth == "七") gameMonth = "7"; if (gameMonth == "八") gameMonth = "8"; if (gameMonth == "九") gameMonth = "9"; if (gameMonth == "十") gameMonth = "10"; if (gameMonth == "十一") gameMonth = "11"; if (gameMonth == "十二") gameMonth = "12"; } // 轉成日期 if (!DateTime.TryParse(gameYear + "/" + gameMonth + "/1", out gameDate)) return 0; #endregion #region 取出狀態 xPath = FindXPath(document.DocumentNode, "lblG2"); // 判斷資料 if (!string.IsNullOrEmpty(xPath)) { nodeGames = document.DocumentNode.SelectSingleNode(xPath); // 資料 foreach (HtmlAgilityPack.HtmlNode gameRow in nodeGames.ChildNodes[1].ChildNodes) { // 不是資料就往下處理 if (gameRow.Name != "tr") continue; if (gameRow.ChildNodes.Count < 2) continue; // 判斷比賽中 if (gameRow.ChildNodes[2].ChildNodes[0].Name == "font") { box[gameRow.ChildNodes[0].InnerText] = gameRow.ChildNodes[2].InnerText.Trim(); } if (gameRow.ChildNodes[2].ChildNodes[0].Name == "a" && gameRow.ChildNodes[2].ChildNodes.Count < 5) { box[gameRow.ChildNodes[0].InnerText] = "E"; } } } #endregion xPath = "/html[1]/body[1]/center[1]/table[1]/tr[3]/td[1]/td[1]/table[1]/tr[2]/td[1]/table[2]/tr[1]/td[1]/table[1]/tr[2]/td[1]/table[1]"; nodeGames = document.DocumentNode.SelectSingleNode(xPath); // 沒有資料就離開 if (nodeGames == null || nodeGames.ChildNodes.Count == 0) return 0; // 資料 foreach (HtmlAgilityPack.HtmlNode gameRow in nodeGames.ChildNodes) { // 不是資料就往下處理 if (gameRow.Name != "tr") continue; // 資料 foreach (HtmlAgilityPack.HtmlNode td in gameRow.ChildNodes) { // 不是資料就往下處理 if (td.ChildNodes[0].ChildNodes.Count != 2) continue; // 不是日期就往下處理 if (!DateTime.TryParse(gameDate.ToString("yyyy-MM") + "-" + td.ChildNodes[0].ChildNodes[0].InnerText, out gameTime)) continue; string webID = null; string webUrl1 = null; string webUrl2 = null; string webUrl3 = null; string[] info = new string[] { "", "", "", "", "", "", "" }; int index = 0; // 資料 foreach (HtmlAgilityPack.HtmlNode data in td.ChildNodes[0].ChildNodes[1].ChildNodes) { // 資料是分隔就清空 if (data.Name == "img") { index = 0; continue; } // 不是資料就往下處理 if (data.Name != "#text" || string.IsNullOrEmpty(data.InnerText.Trim())) continue; // 放入資料 info[index] = data.InnerText.Trim(); // 位置累計 index++; // 判斷數量與時間 if (index >= 5) { string[] team = new string[] { "", "" }; if (info[1].ToLower().IndexOf("vs.") != -1) { // 判斷時間 if (!DateTime.TryParse(gameTime.ToString("yyyy-MM-dd") + " " + info[3], out gameTime)) continue; team = info[1].ToLower().Split(new string[] { "vs." }, StringSplitOptions.RemoveEmptyEntries); } else { // 判斷時間 if (!DateTime.TryParse(gameTime.ToString("yyyy-MM-dd") + " " + info[4], out gameTime)) continue; team[0] = info[1]; team[1] = info[2]; } // 不是今天就往下處理 if (gameTime.Date != this.GameDate.Date) continue; //if (gameTime.Date != DateTime.Parse("2013-4-6")) continue; webID = info[0]; // 建立比賽資料 gameInfo = null; gameInfo = new BasicInfo(this.AllianceID, this.GameType, gameTime, webID); gameInfo.IsBall = true; gameInfo.Away = team[0].Trim(); gameInfo.Home = team[1].Trim(); #region 下載比賽資料,比賽時間 10 分鐘前就開始處理 // 比賽分數 webUrl1 = string.Format(sWebUrl1, gameTime.ToString("yyyy"), webID); if (!this.DownReal.ContainsKey(webUrl1) && GetUtcKr(DateTime.Now) >= gameTime.AddMinutes(-10)) { this.DownReal[webUrl1] = new BasicDownload(this.Sport, webUrl1); this.DownReal[webUrl1].DownloadString(); } // 單局內容 webUrl2 = string.Format(sWebUrl2, gameTime.ToString("yyyy"), webID); if (!this.DownReal.ContainsKey(webUrl2) && GetUtcKr(DateTime.Now) >= gameTime.AddMinutes(-10)) { this.DownReal[webUrl2] = new BasicDownload(this.Sport, webUrl2); this.DownReal[webUrl2].DownloadString(); } // 全局內容 webUrl3 = string.Format(sWebUrl3, gameTime.ToString("yyyy"), webID); if (!this.DownReal.ContainsKey(webUrl3) && GetUtcKr(DateTime.Now) >= gameTime.AddMinutes(-10)) { this.DownReal[webUrl3] = new BasicDownload(this.Sport, webUrl3); this.DownReal[webUrl3].DownloadString(); } #endregion #region 處理比賽資料 if (this.DownReal.ContainsKey(webUrl1) && this.DownReal.ContainsKey(webUrl2) && this.DownReal.ContainsKey(webUrl3) && !string.IsNullOrEmpty(this.DownReal[webUrl1].Data) && !string.IsNullOrEmpty(this.DownReal[webUrl2].Data) && !string.IsNullOrEmpty(this.DownReal[webUrl3].Data)) { HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); int round1 = 0; int round2 = 0; #region 單局內容 doc.LoadHtml(this.DownReal[webUrl2].Data); // 判斷資料 if (doc.DocumentNode.ChildNodes[0].ChildNodes.Count == 7) { // 轉成局數 int.TryParse(doc.DocumentNode.ChildNodes[0].ChildNodes[3].ChildNodes[0].InnerText, out round1); // 判斷比賽結束 if (gameInfo.GameStates == "X") { //gameInfo.GameStates = (this.DownReal[webUrl2].Data.IndexOf("比賽結束") != -1) ? ("E") : ("S"); } } #endregion #region 全局內容 doc.LoadHtml(this.DownReal[webUrl3].Data); // 資料 foreach (HtmlAgilityPack.HtmlNode table in doc.DocumentNode.ChildNodes) { // 不是資料就往下處理 if (table.Name != "table") continue; if (table.ChildNodes.Count <= 5) continue; // 資料 foreach (HtmlAgilityPack.HtmlNode tr in table.ChildNodes) { // 不是資料就往下處理 if (tr.Name != "tr") continue; // 累計 round2++; } } round2 = (round2 - 1) / 3; // 判斷局數 if (round1 == 0 && round2 > 0) round1 = round2; #endregion #region 比賽分數 doc.LoadHtml(this.DownReal[webUrl1].Data); // 判斷資料 if (doc.DocumentNode.ChildNodes[0].ChildNodes.Count == 7) { // Away foreach (HtmlAgilityPack.HtmlNode d in doc.DocumentNode.ChildNodes[0].ChildNodes[3].ChildNodes) { // 不是資料就往下處理 if (d.Name != "td") continue; // 加入分數 gameInfo.AwayBoard.Add((!string.IsNullOrEmpty(d.InnerText.Trim())) ? (d.InnerText.Trim()) : ("")); } gameInfo.AwayBoard.RemoveAt(0); // RHE gameInfo.AwayPoint = gameInfo.AwayBoard[gameInfo.AwayBoard.Count - 3]; gameInfo.AwayH = gameInfo.AwayBoard[gameInfo.AwayBoard.Count - 2]; gameInfo.AwayE = gameInfo.AwayBoard[gameInfo.AwayBoard.Count - 1]; // 移除 RHE gameInfo.AwayBoard.RemoveAt(gameInfo.AwayBoard.Count - 1); gameInfo.AwayBoard.RemoveAt(gameInfo.AwayBoard.Count - 1); gameInfo.AwayBoard.RemoveAt(gameInfo.AwayBoard.Count - 1); // Home foreach (HtmlAgilityPack.HtmlNode d in doc.DocumentNode.ChildNodes[0].ChildNodes[5].ChildNodes) { // 不是資料就往下處理 if (d.Name != "td") continue; // 加入分數 gameInfo.HomeBoard.Add((!string.IsNullOrEmpty(d.InnerText.Trim())) ? (d.InnerText.Trim()) : ("")); } gameInfo.HomeBoard.RemoveAt(0); // RHE gameInfo.HomePoint = gameInfo.HomeBoard[gameInfo.HomeBoard.Count - 3]; gameInfo.HomeH = gameInfo.HomeBoard[gameInfo.HomeBoard.Count - 2]; gameInfo.HomeE = gameInfo.HomeBoard[gameInfo.HomeBoard.Count - 1]; // 移除 RHE gameInfo.HomeBoard.RemoveAt(gameInfo.HomeBoard.Count - 1); gameInfo.HomeBoard.RemoveAt(gameInfo.HomeBoard.Count - 1); gameInfo.HomeBoard.RemoveAt(gameInfo.HomeBoard.Count - 1); } // 移除多餘的分數 測試 round1 = gameInfo.AwayBoard.Count; if (round1 < gameInfo.HomeBoard.Count) round1 = gameInfo.HomeBoard.Count; while (round1 > 0) { round1--; if (round1 < gameInfo.AwayBoard.Count && string.IsNullOrEmpty(gameInfo.AwayBoard[round1])) { gameInfo.AwayBoard.RemoveAt(round1); } if (round1 < gameInfo.HomeBoard.Count && string.IsNullOrEmpty(gameInfo.HomeBoard[round1])) { gameInfo.HomeBoard.RemoveAt(round1); } if (gameInfo.AwayBoard.Count == 0 || gameInfo.HomeBoard.Count == 0) { break; } if (!string.IsNullOrEmpty(gameInfo.AwayBoard[gameInfo.AwayBoard.Count - 1]) || !string.IsNullOrEmpty(gameInfo.HomeBoard[gameInfo.HomeBoard.Count - 1])) { if (string.IsNullOrEmpty(gameInfo.AwayBoard[gameInfo.AwayBoard.Count - 1])) { gameInfo.AwayBoard.RemoveAt(gameInfo.AwayBoard.Count - 1); } if (string.IsNullOrEmpty(gameInfo.HomeBoard[gameInfo.HomeBoard.Count - 1])) { gameInfo.HomeBoard.RemoveAt(gameInfo.HomeBoard.Count - 1); } break; } } #endregion #region 比賽狀態 if (gameInfo.Quarter == 0) { gameInfo.GameStates = "X"; } else { // 判斷比賽結束 if (gameInfo.GameStates == "S" || gameInfo.GameStates == "X") { gameInfo.GameStates = (this.DownReal[webUrl3].Data.IndexOf("再見") != -1) ? ("E") : ("S"); } } if (gameInfo.GameStates == "S" && box.ContainsKey(gameInfo.Away + gameInfo.Home)) { // 比賽結束 if (box[gameInfo.Away + gameInfo.Home] == "E") { gameInfo.GameStates = "E"; } else { if (gameInfo.Quarter == 0) { gameInfo.GameStates = "X"; gameInfo.AwayPoint = ""; gameInfo.AwayH = "0"; gameInfo.AwayE = "0"; gameInfo.HomePoint = ""; gameInfo.HomeH = "0"; gameInfo.HomeE = "0"; gameInfo.BallB = 0; gameInfo.BallS = 0; gameInfo.BallO = 0; gameInfo.Bases = 0; } } } #endregion } #endregion // 加入 this.GameData[gameInfo.WebID] = gameInfo; // 累計 result++; } } } } } // 傳回 return result; }
private int FollowByBasicToCBS() { // 沒有資料就離開 if (string.IsNullOrEmpty(this.DownHome.Data)) return 0; int result = 0; BasicInfo gameInfo = null; HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument(); string xPath = "/html[1]/body[1]/div[3]/div[3]/div[1]/div[2]/div[1]"; // 載入資料 document.LoadHtml(this.DownHome.Data); // 資料位置 HtmlAgilityPack.HtmlNode nodeGames = document.DocumentNode.SelectSingleNode(xPath); // 判斷資料 if (nodeGames != null && nodeGames.ChildNodes != null) { DateTime gameDate = this.GameDate; DateTime gameTime = this.GameDate; // 資料 foreach (HtmlAgilityPack.HtmlNode gameCol in nodeGames.ChildNodes) { // 不是資料就往下處理 if (gameCol.Name != "div") continue; if (gameCol.GetAttributeValue("class", "").IndexOf("scoreBox") == -1) continue; // 資料 foreach (HtmlAgilityPack.HtmlNode game in gameCol.ChildNodes) { #region span 隊伍與分數 if (game.Name == "span" && game.Id.IndexOf("board") != -1) { HtmlAgilityPack.HtmlNode table = game.ChildNodes[1]; // 跟盤 ID string webID = game.Id.Replace("board", "").Trim(); // 建立比賽資料 gameInfo = null; gameInfo = new BasicInfo(this.AllianceID, this.GameType, gameTime, webID); gameInfo.IsBall = true; #region 客隊 gameInfo.Away = table.ChildNodes[1].ChildNodes[0].InnerText; // 截取 if (gameInfo.Away.IndexOf("(") != -1) { gameInfo.Away = gameInfo.Away.Substring(0, gameInfo.Away.IndexOf("(")).Trim(); } // 分數 if (table.ChildNodes[1].ChildNodes.Count > 3) { for (int i = 1; i < table.ChildNodes[1].ChildNodes.Count - 3; i++) { if (!string.IsNullOrEmpty(table.ChildNodes[1].ChildNodes[i].InnerText.Replace(" ", ""))) { gameInfo.AwayBoard.Add(table.ChildNodes[1].ChildNodes[i].InnerText); } } // 總分 gameInfo.AwayPoint = table.ChildNodes[1].ChildNodes[table.ChildNodes[1].ChildNodes.Count - 3].InnerText; gameInfo.AwayH = table.ChildNodes[1].ChildNodes[table.ChildNodes[1].ChildNodes.Count - 2].InnerText; gameInfo.AwayE = table.ChildNodes[1].ChildNodes[table.ChildNodes[1].ChildNodes.Count - 1].InnerText; } #endregion #region 主隊 gameInfo.Home = table.ChildNodes[2].ChildNodes[0].InnerText; // 截取 if (gameInfo.Home.IndexOf("(") != -1) { gameInfo.Home = gameInfo.Home.Substring(0, gameInfo.Home.IndexOf("(")).Trim(); } // 分數 if (table.ChildNodes[2].ChildNodes.Count > 3) { for (int i = 1; i < table.ChildNodes[2].ChildNodes.Count - 3; i++) { if (!string.IsNullOrEmpty(table.ChildNodes[2].ChildNodes[i].InnerText.Replace(" ", ""))) { gameInfo.HomeBoard.Add(table.ChildNodes[2].ChildNodes[i].InnerText); } } // 總分 gameInfo.HomePoint = table.ChildNodes[2].ChildNodes[table.ChildNodes[1].ChildNodes.Count - 3].InnerText; gameInfo.HomeH = table.ChildNodes[2].ChildNodes[table.ChildNodes[1].ChildNodes.Count - 2].InnerText; gameInfo.HomeE = table.ChildNodes[2].ChildNodes[table.ChildNodes[1].ChildNodes.Count - 1].InnerText; } #endregion #region 比賽狀態 if (table.ChildNodes[0].ChildNodes[0].InnerText.IndexOf("Final") != -1) { gameInfo.GameStates = "E"; gameInfo.Status = "結束"; gameInfo.BallB = 0; gameInfo.BallS = 0; gameInfo.BallO = 0; gameInfo.Bases = 0; } else if (gameInfo.Quarter > 0) { gameInfo.GameStates = "S"; } #endregion // 加入 this.GameData[gameInfo.WebID] = gameInfo; // 累計 result++; } #endregion #region div BSO Bases if (gameInfo != null && gameInfo.GameStates == "S" && game.Name == "div" && game.Id == "alerts" + gameInfo.WebID && game.ChildNodes[0].ChildNodes[0].ChildNodes[1].ChildNodes.Count > 0) { HtmlAgilityPack.HtmlNode table = game.ChildNodes[0].ChildNodes[0].ChildNodes[1].ChildNodes[0].ChildNodes[0].ChildNodes[0]; // 判斷資料 if (table != null && table.ChildNodes.Count == 3) { // B if (table.ChildNodes[0].ChildNodes[1].GetAttributeValue("class", "") != "none") gameInfo.BallB = 1; if (table.ChildNodes[0].ChildNodes[3].GetAttributeValue("class", "") != "none") gameInfo.BallB = 2; if (table.ChildNodes[0].ChildNodes[5].GetAttributeValue("class", "") != "none") gameInfo.BallB = 3; // S if (table.ChildNodes[1].ChildNodes[0].GetAttributeValue("class", "") != "none") gameInfo.BallS = 1; if (table.ChildNodes[1].ChildNodes[2].GetAttributeValue("class", "") != "none") gameInfo.BallS = 2; // OUT if (table.ChildNodes[2].ChildNodes[0].GetAttributeValue("class", "") != "none") gameInfo.BallO = 1; if (table.ChildNodes[2].ChildNodes[2].GetAttributeValue("class", "") != "none") gameInfo.BallO = 2; } } #endregion } } } // 傳回 return result; }
private void GetOldGames() { DataTable dt = this.GetIceHockeyData(frmMain.ConnectionString, this.GameDate.AddDays(startIndex), this.GameDate.AddDays(endIndex)); foreach (DataRow dr in dt.Rows) { int allianceid = dr.Field<int>("AllianceID"); string webID = dr.Field<string>("WebID"); string gameDate = dr.Field<DateTime>("GameDate").ToString(DATE_STRING_FORMAT); DateTime gameTime = DateTime.Parse(gameDate + " " + dr.Field<TimeSpan>("GameTime").ToString(@"hh\:mm")); BasicInfo basic = new BasicInfo(allianceid, this.GameType, gameTime, webID); basic.AllianceName = dr.Field<string>("alliancename"); basic.GameStates = dr.Field<string>("GameStates"); basic.Status = dr.Field<string>("StatusText"); basic.Home = dr.Field<string>("home"); basic.Away = dr.Field<string>("away"); basic.HomePoint = dr.Field<int>("RA").ToString(); basic.AwayPoint = dr.Field<int>("RB").ToString(); basic.HomeBoard = dr.Field<string>("RunsA") != null ? dr.Field<string>("RunsA").Split(',').ToList() : null; basic.AwayBoard = dr.Field<string>("RunsB") != null ? dr.Field<string>("RunsB").Split(',').ToList() : null; basic.TrackerText = dr.Field<string>("TrackerText"); basic.Record = dr.Field<string>("Record"); basic.OrderBy = dr.Field<int>("OrderBy"); Dictionary<string, BasicInfo> dic = this.gameData.ContainsKey(gameDate) ? this.gameData[gameDate] : new Dictionary<string, BasicInfo>(); if (!dic.ContainsKey(basic.WebID)) { dic.Add(basic.WebID, basic); } this.gameData[gameDate] = dic; } }
private int FollowByBasicToMLB() { // 沒有資料就離開 if (string.IsNullOrEmpty(this.DownHome.Data)) return 0; int result = 0; BasicInfo gameInfo = null; JObject json = new JObject(); // 載入資料 json = JObject.Parse(this.DownHome.Data); // 判斷資料 if (json["data"]["games"] != null && json["data"]["games"]["game"] != null) { DateTime gameTime = this.GameDate; string webID = null; int num = 0; // 資料 foreach (JObject game in (JArray)json["data"]["games"]["game"]) { // 日期時間錯誤就離開 if (!DateTime.TryParse(game["time_date"].ToString() + " " + game["ampm"].ToString(), out gameTime)) continue; webID = game["gameday"].ToString(); // 建立比賽資料 gameInfo = null; gameInfo = new BasicInfo(this.AllianceID, this.GameType, gameTime, webID); gameInfo.IsBall = true; gameInfo.Away = game["away_team_name"].ToString(); gameInfo.Home = game["home_team_name"].ToString(); if (game["linescore"] != null && game["status"] != null) { // 分數 if (game["linescore"]["inning"] is JObject) { if (game["linescore"]["inning"]["away"] != null) gameInfo.AwayBoard.Add((!string.IsNullOrEmpty(game["linescore"]["inning"]["away"].ToString())) ? (game["linescore"]["inning"]["away"].ToString()) : ("0")); if (game["linescore"]["inning"]["home"] != null) gameInfo.HomeBoard.Add((!string.IsNullOrEmpty(game["linescore"]["inning"]["home"].ToString())) ? (game["linescore"]["inning"]["home"].ToString()) : ("0")); } else { foreach (JObject board in (JArray)game["linescore"]["inning"]) { if (board["away"] != null) gameInfo.AwayBoard.Add((!string.IsNullOrEmpty(board["away"].ToString())) ? (board["away"].ToString()) : ("0")); if (board["home"] != null) gameInfo.HomeBoard.Add((!string.IsNullOrEmpty(board["home"].ToString())) ? (board["home"].ToString()) : ("0")); } } // R、H、E gameInfo.AwayPoint = game["linescore"]["r"]["away"].ToString(); gameInfo.AwayH = game["linescore"]["h"]["away"].ToString(); gameInfo.AwayE = game["linescore"]["e"]["away"].ToString(); gameInfo.HomePoint = game["linescore"]["r"]["home"].ToString(); gameInfo.HomeH = game["linescore"]["h"]["home"].ToString(); gameInfo.HomeE = game["linescore"]["e"]["home"].ToString(); // 比賽狀態 if (game["status"]["status"].ToString().ToLower().IndexOf("final") != -1 || game["status"]["status"].ToString().ToLower().IndexOf("game over") != -1) { gameInfo.GameStates = "E"; // 比賽結束補 X if (gameInfo.AwayBoard.Count != gameInfo.HomeBoard.Count) { gameInfo.HomeBoard.Add("X"); } } else if (game["status"]["status"].ToString().ToLower().IndexOf("pre-game") == -1) { gameInfo.GameStates = "S"; // BSO if (int.TryParse(game["status"]["b"].ToString(), out num)) gameInfo.BallB = num; if (int.TryParse(game["status"]["s"].ToString(), out num)) gameInfo.BallS = num; if (int.TryParse(game["status"]["o"].ToString(), out num)) gameInfo.BallO = num; if (game["runners_on_base"] != null && game["runners_on_base"]["status"] != null && int.TryParse(game["runners_on_base"]["status"].ToString(), out num)) { gameInfo.Bases = num; } } } // 加入 this.GameData[gameInfo.WebID] = gameInfo; // 累計 result++; } } // 傳回 return result; }
public override int Follow() { // 沒有資料就離開 if (string.IsNullOrEmpty(this.DownHome.Data)) return 0; int result = 0; BasicInfo gameInfo = null; HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument(); // 載入資料 document.LoadHtml(this.DownHome.Data); //document.Load(@"C:\Users\Administrator\Desktop\ahl.txt");//本機資料測試 // 資料位置 //string xPath = "/html[1]/body[1]/div[1]/div[3]/div[1]/div[1]/div[2]/div[3]"; //HtmlAgilityPack.HtmlNode nodeGames = document.DocumentNode.SelectSingleNode(xPath); HtmlAgilityPack.HtmlNode nodeGames = document.GetElementbyId("AHLscoreBox"); // 判斷資料 if (nodeGames != null && nodeGames.ChildNodes != null) { DateTime gameDate = this.GameDate; DateTime gameTime = this.GameDate; //string gameDateStr = null; int gameQuarter = 0; // 資料 foreach (HtmlAgilityPack.HtmlNode game in nodeGames.ChildNodes) { string webID = null; #region 跟盤 ID HtmlAgilityPack.HtmlNodeCollection GameDateInfo = game.SelectNodes("div[@class='final-hd-link']/a"); if (GameDateInfo != null && GameDateInfo.Count > 0) { var href = GameDateInfo[0].GetAttributeValue("href", ""); if (string.IsNullOrEmpty(href))//取到空值 continue; try { if (!href.Contains("http")) href = "http://" + href; Uri uri = new Uri(href); // 判斷是否有資料 if (uri.Query != null && !string.IsNullOrEmpty(uri.Query)) { HttpRequest req = new HttpRequest("", uri.AbsoluteUri, uri.Query.Substring(1)); // 判斷資料 if (req["game_id"] != null && !string.IsNullOrEmpty(req["game_id"].Trim()))//已開賽 { webID = req["game_id"]; } else if (req["file_path"] != null && !string.IsNullOrEmpty(req["file_path"].Trim()))//未開賽 { webID = req["file_path"]; if (webID.IndexOf("/") != -1) { webID = webID.Substring(webID.IndexOf("/") + 1).Trim(); } if (webID.IndexOf("-") != -1) { webID = webID.Substring(0, webID.IndexOf("-")).Trim(); } } } } catch { } } else continue; // 沒有跟盤 ID 就往下處理 if (webID == null || string.IsNullOrEmpty(webID.Trim())) continue; #endregion // 建立比賽資料,時間以台灣為主 DateTime GameTime; if (diffTime > 0) GameTime = this.GameDate.AddHours(diffTime); else { TimeSpan ts = DateTime.Now - this.GameDate; GameTime = this.GameDate.AddHours(ts.TotalHours); } gameInfo = null; gameInfo = new BasicInfo(this.AllianceID, this.GameType, GameTime, webID); gameQuarter = 0; #region 處理比賽資料 #region 狀態 gameInfo.Status = game.ChildNodes[1].InnerText.ToLower().Trim(); gameInfo.Status = gameInfo.Status.ToLower().Trim(); // 判斷目前局數 if (gameInfo.Status.IndexOf("1st") != -1) gameQuarter = 1; if (gameInfo.Status.IndexOf("2nd") != -1) gameQuarter = 2; if (gameInfo.Status.IndexOf("3rd") != -1) gameQuarter = 3; if (gameInfo.Status.IndexOf("ot") != -1) gameQuarter = 4; // 判斷 if (gameInfo.Status.IndexOf("final") != -1) { gameInfo.GameStates = "E"; gameInfo.Status = "結束"; gameQuarter = 10; } else if (gameQuarter > 0) // 有局數才有比賽 { // 取代文字 gameInfo.GameStates = "S"; // 取代文字 gameInfo.Status = gameInfo.Status.Replace("half", ""); gameInfo.Status = gameInfo.Status.Replace("halftime", ""); gameInfo.Status = gameInfo.Status.Replace("qtr", ""); gameInfo.Status = gameInfo.Status.Replace("1st", ""); gameInfo.Status = gameInfo.Status.Replace("2nd", ""); gameInfo.Status = gameInfo.Status.Replace("3rd", ""); gameInfo.Status = gameInfo.Status.Replace("4th", ""); gameInfo.Status = gameInfo.Status.Replace("ot", ""); gameInfo.Status = gameInfo.Status.Replace("2ot", ""); gameInfo.Status = gameInfo.Status.Trim(); if (gameInfo.Status == "00:00") { gameInfo.Status = "結束"; } } #endregion HtmlAgilityPack.HtmlNode info = null; // 判斷資料格式 if (game.ChildNodes[5].ChildNodes.Count == 5) { #region 比賽未開始,只有隊伍名稱 // Away info = game.ChildNodes[5].ChildNodes[1]; gameInfo.Away = info.ChildNodes[1].InnerText; // Home info = game.ChildNodes[5].ChildNodes[3]; gameInfo.Home = info.ChildNodes[1].InnerText; #endregion } else { #region Away info = game.ChildNodes[5].ChildNodes[3]; gameInfo.Away = info.ChildNodes[1].InnerText; gameInfo.AwayPoint = info.ChildNodes[info.ChildNodes.Count - 10].InnerText; // 分數 for (int i = 3; i < info.ChildNodes.Count - 10; i += 2) { if (info.ChildNodes[i].InnerText != null && !string.IsNullOrEmpty(info.ChildNodes[i].InnerText.Trim()) && gameInfo.AwayBoard.Count < gameQuarter) { gameInfo.AwayBoard.Add(info.ChildNodes[i].InnerText.Trim()); } } #endregion #region Home info = game.ChildNodes[5].ChildNodes[5]; gameInfo.Home = info.ChildNodes[1].InnerText; gameInfo.HomePoint = info.ChildNodes[info.ChildNodes.Count - 8].InnerText; // 分數 for (int i = 3; i < info.ChildNodes.Count - 8; i += 2) { if (info.ChildNodes[i].InnerText != null && !string.IsNullOrEmpty(info.ChildNodes[i].InnerText.Trim()) && gameInfo.HomeBoard.Count < gameQuarter) { gameInfo.HomeBoard.Add(info.ChildNodes[i].InnerText.Trim()); } } #endregion } #region 處理隊伍名稱 Dictionary<string, string> webName = this.GetWebName(); // Away gameInfo.Away = gameInfo.Away.Replace("\n", ""); gameInfo.Away = gameInfo.Away.Replace("\t", ""); gameInfo.Away = gameInfo.Away.Trim(); // 刪除無用資料 if (gameInfo.Away.IndexOf("(") != -1) { gameInfo.Away = gameInfo.Away.Substring(0, gameInfo.Away.IndexOf("(")).Trim(); } // 變更名稱 if (webName.ContainsKey(gameInfo.Away)) { gameInfo.Away = webName[gameInfo.Away]; } // Home gameInfo.Home = info.ChildNodes[1].InnerText; gameInfo.Home = gameInfo.Home.Replace("\n", ""); gameInfo.Home = gameInfo.Home.Replace("\t", ""); gameInfo.Home = gameInfo.Home.Trim(); // 刪除無用資料 if (gameInfo.Home.IndexOf("(") != -1) { gameInfo.Home = gameInfo.Home.Substring(0, gameInfo.Home.IndexOf("(")).Trim(); } // 變更名稱 if (webName.ContainsKey(gameInfo.Home)) { gameInfo.Home = webName[gameInfo.Home]; } #endregion #endregion // 加入 this.GameData[gameInfo.WebID] = gameInfo; // 累計 result++; } } // 傳回 return result; }
private int FollowByWeb() { cnm++; // 沒有資料就離開 if (this.DownWeb.Document == null || this.DownWeb.Document.Body == null) { return 0; } // 不是 Live 就離開 //HtmlElementCollection CLOSEdiv =this.DownWeb.Document.Body.GetElementsByTagName("div"); //foreach (HtmlElement item in CLOSEdiv) //{ // if (item.InnerHtml == "CLOSE") { // item.InvokeMember("Click"); // } //} if (!this.FollowByWebIsLive) return 0; Logs.UpdateJson(this.DownWeb.Document.Body.InnerText); int result = 0; BasicInfo gameInfo = null; HtmlElementCollection divDoc = this.DownWeb.Document.Body.GetElementsByTagName("span"); // 資料 foreach (HtmlElement div in divDoc) { if (div.InnerHtml == "MLB Pre-Season Power Rankings") { HtmlElementCollection close = div.Parent.NextSibling.Children; if (close.Count > 1 && close[1].InnerHtml == "CLOSE") { close[1].InvokeMember("Click"); } } if (div.Id != null && div.Id.IndexOf("board") == 0) { // 跟盤 ID string webID = div.Id.Replace("board", "").Trim(); List<List<string>> teamBoard = this.FollowByWebTeamBoard(webID); List<string> bsoBases = this.FollowByWebBSOBases(webID); //Logs.UpdateJson(this.DownWeb.Document.Body.InnerHtml + "/////=====///" + getTest("c_" + webID + "test") + "//" + getTest("g_" + webID + "_Status") + "//" + getTest("g_" + webID + "_AwayBoard") + "//" + getTest("g_" + webID + "_HomeBoard") + "//" + getTest("g_" + webID + "_BSOBases") + "//" + (this.DownWeb.Url == null).ToString()); // 判斷分數 if (teamBoard != null && teamBoard.Count > 0 && teamBoard[1].Count > 0) { // 建立比賽資料 gameInfo = null; gameInfo = new BasicInfo(this.AllianceID, this.GameType, this.GameDate, webID); gameInfo.IsBall = true; #region 客隊 gameInfo.Away = "Away"; // 分數 if (teamBoard[1].Count > 0) { for (int i = 0; i < teamBoard[1].Count - 3; i++) { gameInfo.AwayBoard.Add(teamBoard[1][i]); } // 總分 gameInfo.AwayPoint = teamBoard[1][teamBoard[1].Count - 3]; gameInfo.AwayH = teamBoard[1][teamBoard[1].Count - 2]; gameInfo.AwayE = teamBoard[1][teamBoard[1].Count - 1]; } #endregion #region 主隊 gameInfo.Home = "Home"; // 分數 if (teamBoard[2].Count > 0) { for (int i = 0; i < teamBoard[2].Count - 3; i++) { gameInfo.HomeBoard.Add(teamBoard[2][i]); } // 總分 gameInfo.HomePoint = teamBoard[2][teamBoard[2].Count - 3]; gameInfo.HomeH = teamBoard[2][teamBoard[2].Count - 2]; gameInfo.HomeE = teamBoard[2][teamBoard[2].Count - 1]; } #endregion #region BSO Bases if (bsoBases != null && bsoBases.Count == 6) { // BSO gameInfo.BallB = int.Parse(bsoBases[0]); gameInfo.BallS = int.Parse(bsoBases[1]); gameInfo.BallO = int.Parse(bsoBases[2]); int bases = 0; // Bases if (int.Parse(bsoBases[3]) > 0) bases = 1; if (int.Parse(bsoBases[4]) > 0) bases ^= 2; if (int.Parse(bsoBases[5]) > 0) bases ^= 4; gameInfo.Bases = bases; } #endregion #region 比賽狀態 if (teamBoard[0][0].ToLower().IndexOf("final") != -1) { gameInfo.GameStates = "E"; gameInfo.Status = "結束"; gameInfo.BallB = 0; gameInfo.BallS = 0; gameInfo.BallO = 0; gameInfo.Bases = 0; } else if (teamBoard[0][0].ToLower().IndexOf("postponed") != -1) { gameInfo.GameStates = "P"; gameInfo.Status = "中止"; gameInfo.TrackerText = "因雨延賽"; gameInfo.BallB = 0; gameInfo.BallS = 0; gameInfo.BallO = 0; gameInfo.Bases = 0; } else if (teamBoard[0][0].ToLower().IndexOf("delay") != -1) { gameInfo.GameStates = "D"; gameInfo.Status = "DELAY"; gameInfo.BallB = 0; gameInfo.BallS = 0; gameInfo.BallO = 0; gameInfo.Bases = 0; } else if (gameInfo.Quarter > 0) { gameInfo.GameStates = "S"; } #endregion // 加入 this.GameData[gameInfo.WebID] = gameInfo; cnm = 0; // 累計 result++; } } } // 傳回 return result; }