/* * // decript file * private void DecriptFile() * { * MessageBox.Show("открываем " + openedFile + " и дешифруем с помощью " + encriptionKey); * } */ private byte[] TableToArray() { // 0-4 bytes - int count of Rows // i - index current Row // (4+i byte[] buffer; int rows = dataGridMain.Rows.Count; MemoryStream memoryStream = new MemoryStream(); buffer = BitConverter.GetBytes(rows); memoryStream.Write(buffer, 0, buffer.Length); byte b; RowOfTable row = new RowOfTable(); for (int i = 0; i < rows; i++) { // fill RowOfTable row.Site = (string)dataGridMain.Rows[i].Cells[0].Value; row.Name = (string)dataGridMain.Rows[i].Cells[1].Value; row.Pass = (string)dataGridMain.Rows[i].Cells[4].Value; row.Comment = (string)dataGridMain.Rows[i].Cells[3].Value; // write length of the fields in the table //memoryStream.WriteByte((byte)row.Site.Length); //memoryStream.WriteByte((byte)row.Name.Length); //memoryStream.WriteByte((byte)row.Pass.Length); //memoryStream.WriteByte((byte)row.Comment.Length); b = (byte)(row.Site.Length * 2); memoryStream.WriteByte(b); b = (byte)(row.Name.Length * 2); memoryStream.WriteByte(b); b = (byte)(row.Pass.Length * 2); memoryStream.WriteByte(b); b = (byte)(row.Comment.Length * 2); memoryStream.WriteByte(b); // write fields of current row if (row.Site.Length > 0) { buffer = Encoding.Unicode.GetBytes(row.Site); memoryStream.Write(buffer, 0, buffer.Length); } if (row.Name.Length > 0) { buffer = Encoding.Unicode.GetBytes(row.Name); memoryStream.Write(buffer, 0, buffer.Length); } if (row.Pass.Length > 0) { buffer = Encoding.Unicode.GetBytes(row.Pass); memoryStream.Write(buffer, 0, buffer.Length); } if (row.Comment.Length > 0) { buffer = Encoding.Unicode.GetBytes(row.Comment); memoryStream.Write(buffer, 0, buffer.Length); } } buffer = memoryStream.ToArray(); return(buffer); }
private bool TableFromArray(byte[] array) { MemoryStream memoryStream = new MemoryStream(array); byte[] buffer = new byte[4]; memoryStream.Read(buffer, 0, 4); // get rows number int row = BitConverter.ToInt32(buffer, 0); if (row <= 0 || row >= MAXROWSCOUNT) { showError(); return(false); } int rowIndex; RowOfTable rowOfTable = new RowOfTable(); for (int i = 0; i < row; i++) { rowOfTable.siteLength = (byte)memoryStream.ReadByte(); rowOfTable.nameLength = (byte)memoryStream.ReadByte(); rowOfTable.passLength = (byte)memoryStream.ReadByte(); rowOfTable.commentLength = (byte)memoryStream.ReadByte(); if (rowOfTable.siteLength > 0) { buffer = new byte[rowOfTable.siteLength]; memoryStream.Read(buffer, 0, rowOfTable.siteLength); rowOfTable.Site = Encoding.Unicode.GetString(buffer); } else { rowOfTable.Site = string.Empty; } if (rowOfTable.nameLength > 0) { buffer = new byte[rowOfTable.nameLength]; memoryStream.Read(buffer, 0, rowOfTable.nameLength); rowOfTable.Name = Encoding.Unicode.GetString(buffer); } else { rowOfTable.Name = string.Empty; } if (rowOfTable.passLength > 0) { buffer = new byte[rowOfTable.passLength]; memoryStream.Read(buffer, 0, rowOfTable.passLength); rowOfTable.Pass = Encoding.Unicode.GetString(buffer); } else { rowOfTable.Pass = string.Empty; } if (rowOfTable.commentLength > 0) { buffer = new byte[rowOfTable.commentLength]; memoryStream.Read(buffer, 0, rowOfTable.commentLength); rowOfTable.Comment = Encoding.Unicode.GetString(buffer); } else { rowOfTable.Comment = string.Empty; } rowIndex = dataGridMain.Rows.Add(); dataGridMain.Rows[rowIndex].Cells[0].Value = rowOfTable.Site; dataGridMain.Rows[rowIndex].Cells[1].Value = rowOfTable.Name; dataGridMain.Rows[rowIndex].Cells[4].Value = rowOfTable.Pass; dataGridMain.Rows[rowIndex].Cells[3].Value = rowOfTable.Comment; } void showError() { MessageBox.Show("Invalid key or file is corrupted.", "Decryption error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } return(true); }
public async Task StartCrawling() { List <RowOfTable> Rows = new List <RowOfTable>(); string errors = ""; //int PageNumber = 1; int LastPageNumber = 50; for (int PageNumber = 1; PageNumber <= LastPageNumber; PageNumber++) { var pageHTML = LoadNextPage.Next(PageNumber); var htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(pageHTML); var section = htmlDocument .DocumentNode; //WriteLine(section.InnerHtml); WriteLine("break"); try { var nodes = section.SelectNodes("//div[@class='sos-table-wrapper sos-table-mod2 sos-main-font']/div[@class='sos-table-row sos-rtl-right']"); foreach (var n in nodes) { RowOfTable row = new RowOfTable(); WriteLine("row"); var children = n.ChildNodes.Where(n => n.Name != "#text").ToList(); row.OnvaneMarKazeDarmani = (children[0].InnerText.Trim()); row.NoeGhararDad = (children[1].InnerText.Trim()); row.Ostan = (children[2].InnerText.Trim()); row.SodureMoarefiName = (children[4].Element("div").Element("i").Attributes["title"].Value); row.SodureOnlineMoarefiName = (children[5].Element("div").Element("i").Attributes["title"].Value); //WriteLine(children[0].InnerText.Trim()); //WriteLine(children[1].InnerText.Trim()); //WriteLine(children[2].InnerText.Trim()); //WriteLine(children[3].InnerText.Trim()); //WriteLine(children[4].Element("div").Element("i").Attributes["title"].Value); //WriteLine(children[5].Element("div").Element("i").Attributes["title"].Value); //WriteLine(children[6].Element("div").Element("a").Attributes["href"].Value); var detail = LoadNextPage.Detail(children[6].Element("div").Element("a").Attributes["href"].Value.Split("/")[3]); HtmlDocument detailDocument = new HtmlDocument(); detailDocument.LoadHtml(detail); var detailChildren = detailDocument.DocumentNode.SelectNodes("//body/div/dl"); try { foreach (var d in detailChildren) { WriteLine("row children"); var children2 = d.ChildNodes.Where(e => e.Name != "#text").ToList(); //WriteLine(children2[3].InnerText.Trim()); //WriteLine(children2[4].InnerText.Trim()); row.Shahr = (children2[5].InnerText.Trim()); row.Address = (children2[7].InnerText.Trim()); row.Telephone = (children2[9].InnerText.Trim()); row.WebSite = (children2[17].InnerText.Trim()); //WriteLine(children2[5].InnerText.Trim()); //WriteLine(children2[6].InnerText.Trim()); //WriteLine(children2[7].InnerText.Trim()); //WriteLine(children2[8].InnerText.Trim()); //WriteLine(children2[15].InnerText.Trim()); //WriteLine(children2[16].InnerText.Trim()); //WriteLine(children2[17].InnerText.Trim()); //WriteLine(children2[18].InnerText.Trim()); //WriteLine(children2[19].InnerText.Trim()); //WriteLine(children2[20].InnerText.Trim()); } } catch (Exception) { errors += $"error on page = ${PageNumber}, on item => name = {row.OnvaneMarKazeDarmani}\n"; } Rows.Add(row); } } catch (Exception) { errors += $"error on page = {PageNumber}\n"; } } string json = JsonConvert.SerializeObject(Rows); WriteLine(json); WriteLine(errors); }