public DataTable downloadNSEEquityData(int UserId, DateTime Startdate, DateTime Enddate)
        {
            string pathval = System.AppDomain.CurrentDomain.BaseDirectory;

            if (pathval.EndsWith("\\bin\\Debug\\"))
            {
                pathval = pathval.Replace("\\bin\\Debug", "");
            }
            string pathNse           = Path.Combine(pathval, @"MarketDataDownloadFiles\NseEquities.txt");
            string pathFilter        = Path.Combine(pathval, @"MarketDataDownloadFiles\NseEquitiesFiltered.txt");
            string pathNseZip        = Path.Combine(pathval, @"MarketDataDownloadFiles\NSEEquities.zip");
            string pathNSEEquityFile = Path.Combine(pathval, @"MarketDataDownloadFiles\NSEEquities.CSV");
            string pathextract       = Path.Combine(pathval, @"MarketDataDownloadFiles");

            string            Downloadtype      = "NSEEquities";
            MarketDataPullDao marketdatapullDao = new MarketDataPullDao();
            DateTime          date     = Startdate;
            DataTable         dtResult = new DataTable("NSECorpActsDownloadResult");

            dtResult.Columns.Add("Date", typeof(string));
            dtResult.Columns.Add("Result", typeof(string));
            dtResult.Columns.Add("NumofRecords", typeof(int));
            DataRow dr;

            //date = CEodFetch.ValidateDay(date);
            do
            {
                bool result = true;

                string ProcessStatus_NseData = "";
                int    num_of_rec_NseData    = -1;
                ProductPriceDownloadLogBo productPriceDownloadLogBo = new ProductPriceDownloadLogBo();
                AdminDownloadProcessLogVo processLogVo = new AdminDownloadProcessLogVo();
                processLogVo.CreatedBy  = UserId;
                processLogVo.AssetClass = "Equity";
                processLogVo.SourceName = "NSE";
                processLogVo.StartTime  = DateTime.Now;
                processLogVo.EndTime    = DateTime.Now;
                processLogVo.ModifiedOn = DateTime.Now;
                processLogVo.ModifiedBy = UserId;
                processLogVo.ForDate    = date;
                //Create a process for the download
                processLogVo.ProcessID = productPriceDownloadLogBo.CreateProcessLog(processLogVo);
                try
                {
                    string fetchData = CEodFetch.GetNseEquities(date);

                    //Download data from NSE Site
                    WebClient myWebClient = new WebClient();
                    myWebClient.DownloadFile(fetchData, pathNseZip);
                    processLogVo.IsConnectionToSiteEstablished = 1;
                    processLogVo.IsFileDownloaded = 1;
                    result = productPriceDownloadLogBo.UpdateProcessLog(processLogVo);
                    if (result)
                    {
                        //Zip Extracting Code
                        FastZip fz = new FastZip();
                        fz.ExtractZip(pathNseZip, pathextract, "");

                        //filename convert
                        string pathNSEDownloadFile = Path.Combine(pathval, @"MarketDataDownloadFiles\") + CEodFetch.GetDownloadFile(date, Downloadtype);

                        FileInfo renamefile = new FileInfo(pathNSEDownloadFile);
                        if (renamefile.Exists)
                        {
                            File.Delete(pathNSEEquityFile);
                            File.Move(pathNSEDownloadFile, pathNSEEquityFile);
                        }

// ********************************************************************************



                        //Store the retrieved data in a text file

                        //result = CEodFetch.WriteToFile(pathNse, pathNSEEquityFile);

                        if (result)
                        {
                            //Create a stream reader for the text file
                            StreamReader  sr         = new StreamReader(pathNSEEquityFile);
                            StringBuilder strNewData = new StringBuilder();
                            String        line;

                            //Omit lines which are there in the text files
                            //for information purpose
                            while ((line = sr.ReadLine()) != null)
                            {
                                if (line.Contains(","))
                                {
                                    strNewData.AppendLine(line);
                                }
                            }
                            sr.Close();

                            //Create Filtered Data text file
                            result = CEodFetch.WriteToFile(pathFilter, strNewData.ToString());

                            if (result)
                            {
                                //Create Data Set from the filtered data
                                DataSet ds = TextToDataSet.GetNseEquitiesDataSet(pathFilter);


                                if (null != ds)
                                {
                                    processLogVo.NoOfRecordsDownloaded = ds.Tables[0].Rows.Count;

                                    ds.WriteXml(Path.Combine(pathval, @"MarketDataDownloadFiles\" + processLogVo.ProcessID + ".xml"), XmlWriteMode.WriteSchema);
                                    processLogVo.IsConversiontoXMLComplete = 1;
                                    processLogVo.XMLFileName = processLogVo.ProcessID + ".xml";
                                    result = productPriceDownloadLogBo.UpdateProcessLog(processLogVo);
                                    //Store the data set into NSE EOD Market Data Table
                                    string Message = "";
                                    //result = DBHelper.UpdateTable(ds.Tables["NSE"], out Message);
                                    result = marketdatapullDao.InsertDataToTemp(ds, "NSE");


                                    if (result)
                                    {
                                        num_of_rec_NseData = marketdatapullDao.InsertDataintoMarketPriceTable(date, "NSE");

                                        if (num_of_rec_NseData == 0)
                                        {
                                            ProcessStatus_NseData            = "Latest data already exists in the database for the date : " + date + "";
                                            processLogVo.NoOfRecordsInserted = num_of_rec_NseData;
                                            processLogVo.Description         = ProcessStatus_NseData;
                                        }
                                        else
                                        {
                                            ProcessStatus_NseData            = "Data Updated successfully into the table";
                                            processLogVo.IsInsertiontoDBdone = 1;
                                            processLogVo.NoOfRecordsInserted = num_of_rec_NseData;
                                            processLogVo.Description         = ProcessStatus_NseData;
                                        }
                                    }
                                    else
                                    {
                                        ProcessStatus_NseData            = "Failed to insert the data into NSEEquities: " + Message;
                                        num_of_rec_NseData               = 0;
                                        processLogVo.NoOfRecordsInserted = num_of_rec_NseData;
                                        processLogVo.Description         = ProcessStatus_NseData;
                                    }
                                }
                                else
                                {
                                    ProcessStatus_NseData            = "Could not create data set from the filtered data!";
                                    num_of_rec_NseData               = 0;
                                    processLogVo.NoOfRecordsInserted = num_of_rec_NseData;
                                    processLogVo.Description         = ProcessStatus_NseData;
                                }
                            }
                            else
                            {
                                ProcessStatus_NseData            = "Could not write the filtered data to the disk!";
                                num_of_rec_NseData               = 0;
                                processLogVo.NoOfRecordsInserted = num_of_rec_NseData;
                                processLogVo.Description         = ProcessStatus_NseData;
                            }
                        }
                        else
                        {
                            ProcessStatus_NseData = "Could not write the downloaded data to the disk!";
                            num_of_rec_NseData    = 0;
                            processLogVo.NoOfRecordsDownloaded = num_of_rec_NseData;
                            processLogVo.Description           = ProcessStatus_NseData;
                        }
                    }
                    else
                    {
                        ProcessStatus_NseData = "Data not available on the site!";
                        num_of_rec_NseData    = 0;
                        processLogVo.NoOfRecordsDownloaded = num_of_rec_NseData;
                        processLogVo.Description           = ProcessStatus_NseData;
                    }
                }
                catch (Exception exp)
                {
                    ProcessStatus_NseData = "Exception in NSE Equities EOD Fetch: " + exp.Message;
                    num_of_rec_NseData    = 0;
                    processLogVo.NoOfRecordsDownloaded = num_of_rec_NseData;
                    processLogVo.Description           = ProcessStatus_NseData;
                }
                processLogVo.EndTime = DateTime.Now;
                result             = productPriceDownloadLogBo.UpdateProcessLog(processLogVo);
                dr                 = dtResult.NewRow();
                dr["Date"]         = date.ToShortDateString();
                dr["Result"]       = ProcessStatus_NseData;
                dr["NumofRecords"] = num_of_rec_NseData;
                dtResult.Rows.Add(dr);
                date = date.AddDays(1);
            } while (date <= Enddate);
            DataTable dtresulttable = dtResult;

            return(dtResult);
        }
Пример #2
0
        public DataTable downloadNSEEquityData(int UserId, DateTime Startdate, DateTime Enddate)
        {
            string pathval = System.AppDomain.CurrentDomain.BaseDirectory;

            if (pathval.EndsWith("\\bin\\Debug\\"))
            {
                pathval = pathval.Replace("\\bin\\Debug", "");
            }
            string pathNse           = Path.Combine(pathval, @"MarketDataDownloadFiles\NseEquities.txt");
            string pathFilter        = Path.Combine(pathval, @"MarketDataDownloadFiles\NseEquitiesFiltered.txt");
            string pathNseZip        = Path.Combine(pathval, @"MarketDataDownloadFiles\NSEEquities.zip");
            string pathNSEEquityFile = Path.Combine(pathval, @"MarketDataDownloadFiles\NSEEquities.CSV");
            string pathextract       = Path.Combine(pathval, @"MarketDataDownloadFiles");

            string            Downloadtype      = "NSEEquities";
            MarketDataPullDao marketdatapullDao = new MarketDataPullDao();
            DateTime          date     = Startdate;
            DataTable         dtResult = new DataTable("NSECorpActsDownloadResult");

            dtResult.Columns.Add("Date", typeof(string));
            dtResult.Columns.Add("Result", typeof(string));
            dtResult.Columns.Add("NumofRecords", typeof(int));
            DataRow dr;

            //date = CEodFetch.ValidateDay(date);
            do
            {
                bool result = true;

                string ProcessStatus_NseData = "";
                int    num_of_rec_NseData    = -1;
                ProductPriceDownloadLogBo productPriceDownloadLogBo = new ProductPriceDownloadLogBo();
                AdminDownloadProcessLogVo processLogVo = new AdminDownloadProcessLogVo();
                processLogVo.CreatedBy  = UserId;
                processLogVo.AssetClass = "Equity";
                processLogVo.SourceName = "NSE";
                processLogVo.StartTime  = DateTime.Now;
                processLogVo.EndTime    = DateTime.Now;
                processLogVo.ModifiedOn = DateTime.Now;
                processLogVo.ModifiedBy = UserId;
                processLogVo.ForDate    = date;
                //Create a process for the download
                processLogVo.ProcessID = productPriceDownloadLogBo.CreateProcessLog(processLogVo);
                try
                {
                    string fetchData = CEodFetch.GetNseEquities(date);
                    //string sPostData = "";
                    //string sResponse = "";
                    //HttpWebRequest oRequest = (HttpWebRequest)System.Net.HttpWebRequest.Create(fetchData);
                    //oRequest.Method = "POST";
                    //oRequest.ContentType = "application/x-www-form-urlencoded";
                    //oRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                    //oRequest.Accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
                    ////oRequest.ContentLength = sPostData.Length;

                    //UTF8Encoding encoding = new UTF8Encoding();
                    //byte[] bytes = encoding.GetBytes(sPostData);

                    //Stream oWStream = oRequest.GetRequestStream();
                    //oWStream.Write(bytes, 0, bytes.Length);
                    //oWStream.Close();

                    //sResponse = "";
                    //oRequest.Timeout = Timeout.Infinite;
                    //WebResponse oResponse = oRequest.GetResponse();

                    //using (StreamReader oSR = new StreamReader(oResponse.GetResponseStream()))
                    //{
                    //    sResponse = oSR.ReadToEnd();
                    //    oSR.Close();
                    //}

                    //Download data from NSE Site
                    WebClient myWebClient = new WebClient();
                    // myWebClient.Headers.Add("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
                    myWebClient.Headers.Add("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");

                    myWebClient.Headers.Add("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.44 Safari/534.7");


                    //myWebClient.Headers.Add("Accept","application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
                    //myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

                    myWebClient.DownloadFile(fetchData, pathNseZip);

                    processLogVo.IsConnectionToSiteEstablished = 1;
                    processLogVo.IsFileDownloaded = 1;
                    result = productPriceDownloadLogBo.UpdateProcessLog(processLogVo);
                    if (result)
                    {
                        //Zip Extracting Code
                        FastZip fz = new FastZip();
                        fz.ExtractZip(pathNseZip, pathextract, "");

                        //filename convert
                        string pathNSEDownloadFile = Path.Combine(pathval, @"MarketDataDownloadFiles\") + CEodFetch.GetDownloadFile(date, Downloadtype);

                        FileInfo renamefile = new FileInfo(pathNSEDownloadFile);
                        if (renamefile.Exists)
                        {
                            File.Delete(pathNSEEquityFile);
                            File.Move(pathNSEDownloadFile, pathNSEEquityFile);
                        }

// ********************************************************************************



                        //Store the retrieved data in a text file

                        //result = CEodFetch.WriteToFile(pathNse, pathNSEEquityFile);

                        if (result)
                        {
                            //Create a stream reader for the text file
                            StreamReader  sr         = new StreamReader(pathNSEEquityFile);
                            StringBuilder strNewData = new StringBuilder();
                            String        line;

                            //Omit lines which are there in the text files
                            //for information purpose
                            while ((line = sr.ReadLine()) != null)
                            {
                                if (line.Contains(","))
                                {
                                    strNewData.AppendLine(line);
                                }
                            }
                            sr.Close();

                            //Create Filtered Data text file
                            result = CEodFetch.WriteToFile(pathFilter, strNewData.ToString());

                            if (result)
                            {
                                //Create Data Set from the filtered data
                                DataSet ds = TextToDataSet.GetNseEquitiesDataSet(pathFilter);


                                if (null != ds)
                                {
                                    processLogVo.NoOfRecordsDownloaded = ds.Tables[0].Rows.Count;

                                    ds.WriteXml(Path.Combine(pathval, @"MarketDataDownloadFiles\" + processLogVo.ProcessID + ".xml"), XmlWriteMode.WriteSchema);
                                    processLogVo.IsConversiontoXMLComplete = 1;
                                    processLogVo.XMLFileName = processLogVo.ProcessID + ".xml";
                                    result = productPriceDownloadLogBo.UpdateProcessLog(processLogVo);
                                    //Store the data set into NSE EOD Market Data Table
                                    string Message = "";
                                    //result = DBHelper.UpdateTable(ds.Tables["NSE"], out Message);
                                    result = marketdatapullDao.InsertDataToTemp(ds, "NSE");


                                    if (result)
                                    {
                                        num_of_rec_NseData = marketdatapullDao.InsertDataintoMarketPriceTable(date, "NSE");

                                        if (num_of_rec_NseData == 0)
                                        {
                                            ProcessStatus_NseData            = "Latest data already exists in the database for the date : " + date + "";
                                            processLogVo.NoOfRecordsInserted = num_of_rec_NseData;
                                            processLogVo.Description         = ProcessStatus_NseData;
                                        }
                                        else
                                        {
                                            ProcessStatus_NseData            = "Data Updated successfully into the table";
                                            processLogVo.IsInsertiontoDBdone = 1;
                                            processLogVo.NoOfRecordsInserted = num_of_rec_NseData;
                                            processLogVo.Description         = ProcessStatus_NseData;
                                        }
                                    }
                                    else
                                    {
                                        ProcessStatus_NseData            = "Failed to insert the data into NSEEquities: " + Message;
                                        num_of_rec_NseData               = 0;
                                        processLogVo.NoOfRecordsInserted = num_of_rec_NseData;
                                        processLogVo.Description         = ProcessStatus_NseData;
                                    }
                                }
                                else
                                {
                                    ProcessStatus_NseData            = "Could not create data set from the filtered data!";
                                    num_of_rec_NseData               = 0;
                                    processLogVo.NoOfRecordsInserted = num_of_rec_NseData;
                                    processLogVo.Description         = ProcessStatus_NseData;
                                }
                            }
                            else
                            {
                                ProcessStatus_NseData            = "Could not write the filtered data to the disk!";
                                num_of_rec_NseData               = 0;
                                processLogVo.NoOfRecordsInserted = num_of_rec_NseData;
                                processLogVo.Description         = ProcessStatus_NseData;
                            }
                        }
                        else
                        {
                            ProcessStatus_NseData = "Could not write the downloaded data to the disk!";
                            num_of_rec_NseData    = 0;
                            processLogVo.NoOfRecordsDownloaded = num_of_rec_NseData;
                            processLogVo.Description           = ProcessStatus_NseData;
                        }
                    }
                    else
                    {
                        ProcessStatus_NseData = "Data not available on the site!";
                        num_of_rec_NseData    = 0;
                        processLogVo.NoOfRecordsDownloaded = num_of_rec_NseData;
                        processLogVo.Description           = ProcessStatus_NseData;
                    }
                }
                catch (Exception exp)
                {
                    ProcessStatus_NseData = "Exception in NSE Equities EOD Fetch: " + exp.Message;
                    num_of_rec_NseData    = 0;
                    processLogVo.NoOfRecordsDownloaded = num_of_rec_NseData;
                    processLogVo.Description           = ProcessStatus_NseData;
                }
                processLogVo.EndTime = DateTime.Now;
                result             = productPriceDownloadLogBo.UpdateProcessLog(processLogVo);
                dr                 = dtResult.NewRow();
                dr["Date"]         = date.ToShortDateString();
                dr["Result"]       = ProcessStatus_NseData;
                dr["NumofRecords"] = num_of_rec_NseData;
                dtResult.Rows.Add(dr);
                date = date.AddDays(1);
            } while (date <= Enddate);
            DataTable dtresulttable = dtResult;

            return(dtResult);
        }