Ejemplo n.º 1
0
        public ActionResult GetRatesMoex(DateTime?dt, string type = "shares")
        {
            dt = dt ?? homeRepository.GetLastWorkDate();
            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(delegate { return(true); });
            CookieContainer cookie = new CookieContainer();
            HttpWebRequest  wr;

            wr = (HttpWebRequest)WebRequest.Create("https://passport.moex.com/authenticate");
            //wr.Proxy = null;
            wr.CookieContainer = cookie;
            wr.Headers.Add("Authorization", string.Format("Basic {0}", System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("[email protected]:uralsib2012"))));
            wr.Timeout          = 600000;
            wr.ReadWriteTimeout = 600000;
            HttpWebResponse hwr = (HttpWebResponse)wr.GetResponse();

            hwr.Close();

            var         site  = "iss.moex.com";
            var         pathv = "c:\\data\\rate";
            MyWebClient wc    = new MyWebClient(cookie);

            //wc.Proxy = null;
            wc.Headers.Add("Pragma: no-cache");

            var filezip = $"{pathv}\\securities_micex_stock_{type}_{dt:yyyy}_{dt:MM}_{dt:dd}.csv.zip";

            wc.MyDownloadFile($"https://{site}/iss/downloads/engines/stock/markets/{type}/years/{dt:yyyy}/months/{dt:MM}/days/{dt:dd}/securities_micex_stock_{type}_{dt:yyyy}_{dt:MM}_{dt:dd}.csv.zip", filezip);
            using (ZipArchive archive = ZipFile.OpenRead(filezip))
            {
                var e = archive.Entries.Where(p => p.Name.EndsWith(".csv", StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
                if (e != null)
                {
                    var dir = pathv + "\\temp";
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }

                    var fn = Path.Combine(dir, e.Name);
                    e.ExtractToFile(fn, true);

                    using (var sr = new StreamReader(fn, Encoding.GetEncoding(1251)))
                    {
                        string t   = sr.ReadToEnd();
                        Regex  rgx = new Regex("(;-?\\d+),(\\d+)");
                        t = rgx.Replace(t, "$1.$2").Replace(";0000-00-00;", ";;");
                        using (var sw = new StreamWriter(fn + "_", false, Encoding.GetEncoding(1251)))
                        {
                            sw.Write(t);
                        }
                    }
                    System.IO.File.Delete(fn);
                    Byte[] bytes;
                    using (FileStream fs = new FileStream(fn + "_", FileMode.Open, FileAccess.Read))
                    {
                        BinaryReader reader = new BinaryReader(fs);
                        bytes = reader.ReadBytes(Convert.ToInt32(fs.Length));
                    }
                    System.IO.File.Delete(fn + "_");
                    return(File(bytes, "application/vnd.ms-excel", Path.GetFileName(fn)));
                }
            }
            return(new HttpNotFoundResult("File not found"));
        }