Пример #1
0
        public static async Task GetDataFromDate(DateTime time, IProgress <int> progress, bool saveToFile = true)
        {
            List <DailyPrice> dataList = new List <DailyPrice>();
            int count = 0;

            var strFileName = $"{Directory.GetCurrentDirectory()}\\data\\{Utils.GetUnixTime(time).ToString()}.txt";

            Console.WriteLine($"Downloading data for {time.Date.ToString()}");
            using (var outStream = new StreamWriter(File.Open(strFileName, FileMode.OpenOrCreate)))
            {
                outStream.AutoFlush = true;

                if (Global.StockCodes.Count > 0)
                {
                    for (int idx = 0; idx < Global.StockCodes.Count(); idx++)
                    {
                        var stockCode = Global.StockCodes[idx];

                        if (!String.IsNullOrWhiteSpace(stockCode))
                        {
                            string data = await WebMethods.WebGet($"http://finance.vietstock.vn/AjaxData/TradingResult/GetStockData.ashx?scode={stockCode}&_={Utils.GetUnixTime(time).ToString()}");

                            if (!String.IsNullOrWhiteSpace(data) && data != "[]")
                            {
                                outStream.WriteLine(data);

                                try
                                {
                                    var des = JsonConvert.DeserializeObject <List <DailyPrice> >(data);

                                    if (!(des == null) && des.Count > 0)
                                    {
                                        dataList.Add(des[0]);
                                    }
                                }
                                catch
                                {
                                    continue;
                                }
                            }

                            count += 1;
                            progress.Report((int)Math.Floor(((double)count / (double)Global.StockCodes.Count) * 100));

                            // Console.Write($" \r{count}/{stockCodes.Count} - {stockCode}   ");
                        }
                    }
                }
            }
            if (!saveToFile)
            {
                File.Delete(strFileName);
            }
            ProcessData(dataList);
            Global.DataList.AddRange(dataList);
        }
Пример #2
0
        public static async Task <DailyPrice> GetSingleStock(string stockCode)
        {
            string data = await WebMethods.WebGet($"http://finance.vietstock.vn/AjaxData/TradingResult/GetStockData.ashx?scode={stockCode}&_={Utils.GetUnixTime(DateTime.Now).ToString()}");

            if (data != null)
            {
                try
                {
                    var des = JsonConvert.DeserializeObject <List <DailyPrice> >(data);
                    if (des != null && des.Count > 0)
                    {
                        return(des[0]);
                    }
                }
                catch
                {
                    return(null);
                }
            }
            return(null);
        }