public void TestGetAutoCloseIndex(string text, int startindex, int expectedreturn) { int index = HtmlTableParser.GetAutoCloseIndex(text, startindex); if (index != expectedreturn) { throw new Exception(string.Format("The found index {0} is differente from the excepted one {1} for {2}", index, expectedreturn, text)); } }
public void HtmlTableParserTest() { var a = @"D:\_Test\sunamo\sunamo\Html\HtmlTableParserTests\a.html"; var hd = HtmlAgilityHelper.CreateHtmlDocument(); hd.LoadHtml(TF.ReadFile(a)); var table = HtmlAgilityHelper.Node(hd.DocumentNode, true, "table"); HtmlTableParser p = new HtmlTableParser(table, false); var v = p.ColumnValues("1", false, false); int i = 0; }
public void TestGetColrCount() { IHtmlTable ret = HtmlTableParser.Parse(@"<TABLE><TR><TH>H1</TH><TH>H2</TH></TR><TR><TD>C11</TD><TD>C12</TD></TR></TABLE>"); Assert.IsNotNull(ret, "ret must be not null"); Assert.AreEqual(ret.GetColCount(-1), 0, "Negative index for Row should return 0"); Assert.AreEqual(ret.GetColCount(2), 0, "Too large index for Row should return 0"); Assert.AreEqual(ret.GetColCount(0), 2, "Not the expected value for GetColCount"); Assert.AreEqual(ret.GetColCount(1), 2, "Not the expected value for GetColCount"); }
public DataTable OpenTable() { var source = new DominoCleanupHtmlSource(HtmlSource); DataTable table; using (var reader = source.Open(_viewUrl)) { table = HtmlTableParser.Parse(reader)[0]; } return(table); }
public void TestGetPostClosingIndex(string text, int startindex, string wantedtag, int expectedreturn) { int index = HtmlTableParser.GetPostClosingIndex(text, startindex, wantedtag); if (index != expectedreturn) { throw new Exception(string.Format("The found index {0} is differente from the excepted one {1} for {2}", index, expectedreturn, text)); } index = HtmlTableParser.GetPostClosingIndex(text.ToLower(), startindex, wantedtag.ToUpper()); if (index != expectedreturn) { throw new Exception("Case sensitivity problem, result must the same"); } }
public void TestRangeGet() { IHtmlTable ret = HtmlTableParser.Parse(@"<TABLE><TR><TH>H1</TH><TH>H2</TH></TR><TR><TD>C11</TD><TD>C12</TD></TR></TABLE>"); Assert.IsNotNull(ret, "ret must be not null"); Assert.IsNull(ret[-1, 0], "Negative index for Row should return null"); Assert.IsNull(ret[0, -1], "Negative index for Col should return null"); Assert.IsNull(ret[2, 0], "Too large index for Row should return null"); Assert.IsNull(ret[0, 2], "Too large index for Col should return null"); Assert.IsNotNull(ret[0, 0], "In range index for Row and Col should not return null"); Assert.IsNotNull(ret[1, 0], "In range index for Row and Col should not return null"); Assert.IsNotNull(ret[0, 1], "In range index for Row and Col should not return null"); Assert.IsNotNull(ret[1, 1], "In range index for Row and Col should not return null"); }
public void TestExtractCell(string text, string expectedText, int expectedRowSpan, int expectedColSpan, bool expectedIsHeader) { IHtmlCell cell = HtmlTableParser.ExtractCell(text); if (cell.InnerText != expectedText) { throw new Exception(string.Format("The found InnerText {0} is differente from the excepted one {1} for {2}", cell.InnerText, expectedText, text)); } if (cell.IsHeader != expectedIsHeader) { throw new Exception(string.Format("The found IsHeader {0} is differente from the excepted one {1} for {2}", cell.IsHeader, expectedIsHeader, text)); } if (cell.RowSpan != expectedRowSpan) { throw new Exception(string.Format("The found RowSpan {0} is differente from the excepted one {1} for {2}", cell.RowSpan, expectedRowSpan, text)); } if (cell.ColSpan != expectedColSpan) { throw new Exception(string.Format("The found ColSpan {0} is differente from the excepted one {1} for {2}", cell.ColSpan, expectedColSpan, text)); } }
public void TestInvalidCell() { Assert.Throws <HtmlTableParserNoTagEndException>(() => HtmlTableParser.ExtractCell("<td"), "Should throw HtmlTableParserNoTagEndException"); }
public void TestNoCloseCellTag() { Assert.Throws <HtmlTableParserNoCellClosingTagException>(() => HtmlTableParser.Parse("<TABLE><TR><TD></TR></TABLE>"), "A HtmlTableParserNoCellClosingTagException should have be thrown"); }
public void TestNoCloseTableTag() { Assert.Throws <HtmlTableParserNoTableClosingTagException>(() => HtmlTableParser.Parse("<TABLE>skdsdkskdksd"), "A HtmlTableParserNoTableClosingTagException should have be thrown"); }
public BrokerErrorCode GetEquitySpread(string stockCode, out EquitySymbolSpread[] info) { BrokerErrorCode errorCode = BrokerErrorCode.Success; info = new EquitySymbolSpread[2]; string quoteData = null; int retryCount = 0; do { quoteData = HttpHelper.GetWebPageResponse( URL_ICICI_EQT_SPREAD + stockCode.ToUpper(), null, null, mCookieContainer); retryCount++; } while (quoteData == null && retryCount < 5); // web problems, slow connection, server down etc. if (string.IsNullOrEmpty(quoteData) || quoteData.IndexOf("entered is not valid") > -1) { return(BrokerErrorCode.NullResponse); } ParsedTable table = (ParsedTable)HtmlTableParser.ParseHtmlIntoTables(quoteData, true); // NSE price info info[0] = new EquitySymbolSpread(); info[0].Symbol = stockCode; info[0].Exchange = Exchange.NSE; string tempStr = ParsedTable.GetValue(table, new int[] { 0, 3, 0, 1 }); DateTime lastTradeTime = DateTime.Parse(tempStr); tempStr = ParsedTable.GetValue(table, new int[] { 0, 3, 1, 1 }); lastTradeTime += TimeSpan.Parse(tempStr); info[0].QuoteTime = lastTradeTime; info[0].TotalBidQty = MarketUtils.GetVolume(ParsedTable.GetValue(table, new int[] { 0, 5, 7, 1 })); info[0].TotalOfferQty = MarketUtils.GetVolume(ParsedTable.GetValue(table, new int[] { 0, 5, 8, 1 })); for (int i = 0; i < 5; i++) { info[0].BestBidQty[i] = MarketUtils.GetVolume(ParsedTable.GetValue(table, new int[] { 0, 5, i + 2, 0 })); info[0].BestBidPrice[i] = MarketUtils.GetPrice(ParsedTable.GetValue(table, new int[] { 0, 5, i + 2, 1 })); info[0].BestOfferQty[i] = MarketUtils.GetVolume(ParsedTable.GetValue(table, new int[] { 0, 5, i + 2, 2 })); info[0].BestOfferPrice[i] = MarketUtils.GetPrice(ParsedTable.GetValue(table, new int[] { 0, 5, i + 2, 3 })); } // BSE price info info[1] = new EquitySymbolSpread(); info[1].Symbol = stockCode; info[1].Exchange = Exchange.BSE; tempStr = ParsedTable.GetValue(table, new int[] { 0, 3, 0, 3 }); lastTradeTime = DateTime.Parse(tempStr); tempStr = ParsedTable.GetValue(table, new int[] { 0, 3, 1, 3 }); lastTradeTime += TimeSpan.Parse(tempStr); info[1].QuoteTime = lastTradeTime; info[1].TotalBidQty = MarketUtils.GetVolume(ParsedTable.GetValue(table, new int[] { 0, 5, 7, 3 })); info[1].TotalOfferQty = MarketUtils.GetVolume(ParsedTable.GetValue(table, new int[] { 0, 5, 8, 3 })); for (int i = 0; i < 5; i++) { info[1].BestBidQty[i] = MarketUtils.GetVolume(ParsedTable.GetValue(table, new int[] { 0, 5, i + 2, 4 })); info[1].BestBidPrice[i] = MarketUtils.GetPrice(ParsedTable.GetValue(table, new int[] { 0, 5, i + 2, 5 })); info[1].BestOfferQty[i] = MarketUtils.GetVolume(ParsedTable.GetValue(table, new int[] { 0, 5, i + 2, 6 })); info[1].BestOfferPrice[i] = MarketUtils.GetPrice(ParsedTable.GetValue(table, new int[] { 0, 5, i + 2, 7 })); } return(errorCode); }
public void TestCellsInfos() { //Test when table is not the first caracters //Row 1 : test header //Row 2 : test cell //Row 3 : test colspan //Row 4-5: test rowspan and next row completion //Row 6-7: test rowspan and next row missing data //Row 8-9: test rowspan and colspan averlap //Row 10-13: test multi rowspan with overlap IHtmlTable ret = HtmlTableParser.Parse(@"azertyui <Table> <TR><TH>H1</TH><TH>H2</TH></TR> <TR><TD>C11</TD><TD>C12</TD></TR> <TR><TD colspan=2>C21</TD><TD>C23</TD></TR> <TR><TD>C31</TD><TD rowspan=2>C32</TD></TR> <TR><TD>C41</TD><TD>C43</TD></TR> <TR><TD>C51</TD><TD rowspan=2>C52</TD></TR> <TR></TR> <TR><TD>C71</TD><TD rowspan=2>C72</TD></TR> <TR><TD colspan=2>C81</TD><TD>C83</TD></TR> <TR><TD rowspan=3 colspan=3>C91</TD></TR> <TR><TD rowspan=3 colspan=3>CA4</TD></TR> </Table>"); Assert.IsNotNull(ret, "ret must be not null"); Assert.AreEqual(ret.RowCount, 13, "RowCount must be 13"); string[,] expectedValue = { { "H1", "H2", null, null, null, null, null }, { "C11", "C12", null, null, null, null, null }, { "C21", "C21", "C23", null, null, null, null }, { "C31", "C32", null, null, null, null, null }, { "C41", "C32", "C43", null, null, null, null }, { "C51", "C52", null, null, null, null, null }, { null, "C52", null, null, null, null, null }, { "C71", "C72", null, null, null, null, null }, { "C81", "C81", "C83", null, null, null, null }, { "C91", "C91", "C91", null, null, null, null }, { "C91", "C91", "C91", "CA4", "CA4", "CA4", null }, { "C91", "C91", "C91", "CA4", "CA4", "CA4", null }, { null, null, null, "CA4", "CA4", "CA4", null } }; for (int i = 0; i < expectedValue.GetLength(0); i++) { for (int j = 0; j < expectedValue.GetLength(1); j++) { IHtmlCell cell = ret[i, j]; string value = cell?.InnerText; Assert.AreEqual(expectedValue[i, j], value, "Value different for ({0},{1}) : {2} vs {3}", i, j, value, expectedValue[i, j]); Assert.AreEqual(cell?.ToString(), value); } } }
public void TestNotTableTag() { IHtmlTable ret = HtmlTableParser.Parse("azertyuiop"); Assert.IsNull(ret, "ret must be empty"); }
// IBroker.GetEquityOrderBook public BrokerErrorCode GetEquityOrderBookToday(bool newOrdersOnly, bool bOnlyOutstandingOrders, string stockCode, out Dictionary <string, EquityOrderBookRecord> orders) { orders = new Dictionary <string, EquityOrderBookRecord>(); // Login If needed BrokerErrorCode errorCode = CheckAndLogInIfNeeded(false); if (errorCode != BrokerErrorCode.Success) { return(errorCode); } string postData = "pgname=eqOrdBook&ismethodcall=0&mthname="; string orderBookData = IciciGetWebPageResponse(URL_ICICI_EQT_ORDERBOOK, postData, URL_ICICI_REFERRER, mCookieContainer, out errorCode); if (errorCode.Equals(BrokerErrorCode.Success) && !orderBookData.Contains("No matching Record")) { // Trim it orderBookData = orderBookData.Trim(' ', '\t', '\r', '\n'); orderBookData = HtmlUtilities.EnsureHtmlParsable(orderBookData); string subOrderBookData = StringParser.GetStringBetween(orderBookData, 0, "<thead>", "</table>", new string[] { }); ParsedTable table = (ParsedTable)HtmlTableParser.ParseHtmlIntoTables("<table>" + subOrderBookData + "</table>", true); //orders = new Dictionary<string, EquityOrderBookRecord>(); for (int i = 1; i < table.RowCount; i++) { EquityOrderBookRecord info = new EquityOrderBookRecord(); info.StockCode = table[i, 3].ToString().Trim(); info.StockCode = StringParser.GetStringBetween(info.StockCode, 0, "GetQuote('", "'", null); // If stockCode parameter is empty/null i.e. all stocks are intended // and if stock specific orders are intended and current element's stockCode doesnt match then iterate to next element if (!string.IsNullOrEmpty(stockCode) && !info.StockCode.Equals(stockCode, StringComparison.OrdinalIgnoreCase)) { continue; } var temp = StringParser.GetStringBetween(table[i, 0].ToString(), 0, "nowrap;\">", "<", null); DateTime.TryParse(temp, out info.Date); string orderRefString = table[i, 14].ToString(); orderRefString = StringParser.GetStringBetween(orderRefString, 0, "FML_ORD_ORDR_RFRNC=", "&", null); // Trim it orderRefString = orderRefString.Trim();//(' ', '\t', '\r', '\n'); info.OrderRefenceNumber = orderRefString; string tempStr = table[i, 4].ToString().ToUpperInvariant(); info.Direction = (OrderDirection)Enum.Parse(typeof(OrderDirection), tempStr); info.Quantity = int.Parse(table[i, 5].ToString()); string price = StringParser.GetStringBetween(table[i, 3].ToString(), 0, ">", "<", new string[] { "onclick", "font" }); /* * string price = StringParser.GetStringBetween(table[i, 3].ToString(), * 0, * "<font color=\"blue\">", * "</font>", * new string[] { }); * * if (String.IsNullOrEmpty(price)) * { * price = StringParser.GetStringBetween(table[i, 6].ToString(), * 0, * "<font color=\"black\">", * "</font>", * new string[] { }); * } * * if (String.IsNullOrEmpty(price)) * { * price = StringParser.GetStringBetween(table[i, 6].ToString(), * 0, * "\">", * "</a>", * new string[] { "onclick"}); * } */ info.Price = double.Parse(price); tempStr = table[i, 6].ToString().ToUpperInvariant(); // Executed orders have different string format, so ignore the clutter if (tempStr.Contains("EXECUTED")) { tempStr = "EXECUTED"; } else { tempStr = tempStr.Remove(tempStr.IndexOf("&")); } info.Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), tempStr); info.OpenQty = int.Parse(table[i, 8].ToString()); info.ExecutedQty = int.Parse(table[i, 9].ToString()); // Only add valid outstanding orders if bOnlyOutstandingOrders is true // PARTEXEC is considered OUTSTANDING (i.e. not considered EXEC until fully executed) if (!bOnlyOutstandingOrders || info.Status == OrderStatus.PARTEXEC || info.Status == OrderStatus.QUEUED || info.Status == OrderStatus.REQUESTED || info.Status == OrderStatus.ORDERED) { lock (lockObjectEquity) { if (mEquityOrderBook.ContainsKey(orderRefString)) { if (newOrdersOnly) { } else { orders.Add(orderRefString, info); } // Update the order mEquityOrderBook[orderRefString] = info; } else { mEquityOrderBook.Add(orderRefString, info); orders.Add(orderRefString, info); } } } } } return(errorCode); }
// IBroker.GetEquityTradeBook // Get trade book for a date range and for specific stock if stockCode is valid // else gets complete trade book fro the date range public BrokerErrorCode GetEquityTradeBookToday(bool newTradesOnly, string stockCode, out Dictionary <string, EquityTradeBookRecord> trades) { trades = new Dictionary <string, EquityTradeBookRecord>(); // Login If needed BrokerErrorCode errorCode = CheckAndLogInIfNeeded(false); if (errorCode != BrokerErrorCode.Success) { return(errorCode); } string postData = "pgname=eqTrdBook&ismethodcall=0&mthname="; string tradeBookData = IciciGetWebPageResponse(URL_ICICI_EQT_TRADEBOOK, postData, URL_ICICI_REFERRER, mCookieContainer, out errorCode); if (errorCode.Equals(BrokerErrorCode.Success) && !tradeBookData.Contains("No matching Trades")) { // Trim it tradeBookData = tradeBookData.Trim(' ', '\t', '\r', '\n'); tradeBookData = HtmlUtilities.EnsureHtmlParsable(tradeBookData); string subTradeBookData = StringParser.GetStringBetween(tradeBookData, 0, "<thead>", "</table>", new string[] { }); ParsedTable table = (ParsedTable)HtmlTableParser.ParseHtmlIntoTables("<table>" + subTradeBookData + "</table>", true); //trades = new Dictionary<string, EquityTradeBookRecord>(); for (int i = 1; i < table.RowCount - 1; i++) { EquityTradeBookRecord info = new EquityTradeBookRecord(); info.StockCode = table[i, 1].ToString().Trim(); // If stockCode parameter is not null i.e. all stocks are intended // and if stock specific orders are intended and current element's stockCode doesnt match then iterate to next element if (!string.IsNullOrEmpty(stockCode) && !info.StockCode.Equals(stockCode, StringComparison.OrdinalIgnoreCase)) { continue; } var temp = StringParser.GetStringBetween(table[i, 0].ToString(), 0, "nowrap;\">", "<", null); DateTime.TryParse(temp, out info.Date); //if (bSuccess) //{ // info[0].UpdateTime = result; //} string tempStr = table[i, 2].ToString().ToUpperInvariant().StartsWith("B") ? "BUY" : "SELL"; info.Direction = (OrderDirection)Enum.Parse(typeof(OrderDirection), tempStr); info.Quantity = int.Parse(table[i, 3].ToString()); info.Price = double.Parse(table[i, 4].ToString()); info.TradeValue = double.Parse(table[i, 5].ToString()); //tempStr = StringParser.GetStringBetween(table[i, 6].ToString(), 0, "\">", "</a>", null); info.Brokerage = double.Parse(table[i, 6].ToString()); info.SettlementNumber = StringParser.GetStringBetween(table[i, 7].ToString(), 0, "setno=", "&", null); string orderRefString = StringParser.GetStringBetween(table[i, 10].ToString(), 0, "FML_ORD_ORDR_RFRNC=", "&", null); info.OrderRefenceNumber = orderRefString.Trim(); lock (lockObjectEquity) { if (mEquityTradeBook.ContainsKey(info.OrderRefenceNumber)) { if (newTradesOnly) { } else { trades.Add(info.OrderRefenceNumber, info); } // Update the trade // update required because PartExec may have become full exec mEquityTradeBook[info.OrderRefenceNumber] = info; } else { mEquityTradeBook.Add(info.OrderRefenceNumber, info); trades.Add(info.OrderRefenceNumber, info); } } } } return(errorCode); }
public void TestMultipleTableTag() { Assert.Throws <HtmlTableParserMultiTableException>(() => HtmlTableParser.Parse("<TABLE><TABLE>"), "A HtmlTableParserMultiTableException should have be thrown"); }
////////////////////////////////////////// ////// GET EQUITY QUOTE ////// //////////////////////////////////////// // NOTE: We dont want to use IciciGetWebPageResponse here since it updates the login refresh time // whereas getting equity quote doesnt actually refresh contact time with server // it doesnt even need us to be logged in public BrokerErrorCode GetEquityQuote(string stockCode, out EquitySymbolQuote[] info) { BrokerErrorCode errorCode = BrokerErrorCode.Success; info = new EquitySymbolQuote[2]; string quoteData = null; int retryCount = 0; do { quoteData = HttpHelper.GetWebPageResponse( URL_ICICI_EQT_QUOTE + stockCode.ToUpper(), null, null, mCookieContainer); retryCount++; } while (quoteData == null && retryCount < 5); if (string.IsNullOrEmpty(quoteData) || quoteData.IndexOf("entered is not valid") > -1) { // web problems, slow connection, server down etc. return(BrokerErrorCode.NullResponse); } quoteData = quoteData.Substring(quoteData.IndexOf("Best 5 Bids/Offers", 0)); string subQuoteData = StringParser.GetStringBetween(quoteData, 0, "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" class=\"smallfont1\">", "</table>", new string[] { }); ParsedTable table = (ParsedTable)HtmlTableParser.ParseHtmlIntoTables("<table>" + subQuoteData + "</table>", true); DateTime result = DateTime.Now; bool bSuccess = false; // NSE price info info[0] = new EquitySymbolQuote(); info[0].StockCode = stockCode; info[0].Exchange = Exchange.NSE; bSuccess = DateTime.TryParse(table[1, 4] + " " + table[2, 4], out result); if (bSuccess) { info[0].QuoteTime = result; } info[0].LastTradePrice = table[1, 1].ToString().Trim(); info[0].OpenPrice = table[3, 1].ToString().Trim(); info[0].HighPrice = table[4, 1].ToString().Trim(); info[0].LowPrice = table[5, 1].ToString().Trim(); info[0].PreviousClosePrice = table[6, 1].ToString().Trim(); info[0].PercentageChange = table[8, 1].ToString().Trim(); info[0].VolumeTraded = table[11, 1].ToString().Trim(); info[0].BestBidPrice = table[3, 4].ToString().Trim(); info[0].BestOfferPrice = table[4, 4].ToString().Trim(); info[0].BestBidQty = table[5, 4].ToString().Trim(); info[0].BestOfferQty = table[6, 4].ToString().Trim(); info[0].Price52WkHigh = table[7, 4].ToString().Trim(); info[0].Price52WkLow = table[8, 4].ToString().Trim(); // BSE price info info[1] = new EquitySymbolQuote(); info[1].StockCode = stockCode; info[1].Exchange = Exchange.BSE; bSuccess = DateTime.TryParse(table[1, 5] + " " + table[2, 5], out result); if (bSuccess) { info[1].QuoteTime = result; } info[1].LastTradePrice = table[1, 2].ToString(); info[1].OpenPrice = table[3, 2].ToString(); info[1].HighPrice = table[4, 2].ToString(); info[1].LowPrice = table[5, 2].ToString(); info[1].PreviousClosePrice = table[6, 2].ToString(); info[1].PercentageChange = table[8, 2].ToString(); info[1].VolumeTraded = table[11, 2].ToString(); info[1].BestBidPrice = table[3, 5].ToString(); info[1].BestOfferPrice = table[4, 5].ToString(); info[1].BestBidQty = table[5, 5].ToString(); info[1].BestOfferQty = table[6, 5].ToString(); info[1].Price52WkHigh = table[7, 5].ToString(); info[1].Price52WkLow = table[8, 5].ToString(); return(errorCode); }