Пример #1
0
        public override async Task Prepare(DataContainer dataContainer)
        {
            // Delete old data
            if (Reload)
            {
                Directory.Delete(Folder, true);
            }

            // Create folder
            Directory.CreateDirectory(Folder);

            // Inernal data
            _InternalData internalData = new _InternalData();

            // Draw header
            base.DrawHeader();

            // Search
            await DownloadSearchs(dataContainer.Products);

            // Parse
            Parse(internalData);

            // SeDeSerialize
            Cache(dataContainer, internalData);

            // End info
            Console.WriteLine();
        }
Пример #2
0
        private void Cache(DataContainer dataContainer, _InternalData internalData)
        {
            string prefix = "3/3 Caching...";

            Utils.DrawMessage(prefix, Utils.CreateProgressBar(Utils.ProgressBarLength, 0), ConsoleColor.Gray);

            try
            {
                // Serialize or deserialize
                _Cache cache = null;
                if (Reload || !File.Exists(SerializedFile))
                {
                    cache = new _Cache(internalData);

                    Serializer.Serialize(SerializedFile, cache);
                }
                else
                {
                    cache = (_Cache)Serializer.Deserialize(SerializedFile);
                }

                Utils.DrawMessage(prefix, Utils.CreateProgressBar(Utils.ProgressBarLength, 50), ConsoleColor.Gray);

                int drawEvery = Utils.PercentIntervalByLength(cache.Name.Count * 2);

                // Save to datacontainer
                for (int i = 0; i < cache.Name.Count; ++i)
                {
                    // Create forex data
                    _GoogleData gd = new _GoogleData()
                    {
                        Value = cache.Value[i]
                    };

                    // Add to data container
                    dataContainer.GetEvent(new DateTime(cache.Date[i])).GoogleDatas.Add(cache.Name[i], gd);

                    if (i % drawEvery == 0)
                    {
                        Utils.DrawMessage(prefix, Utils.CreateProgressBar(Utils.ProgressBarLength, 50 + (double)i / cache.Name.Count * 50), ConsoleColor.Gray);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine();
                Utils.DrawMessage("", ex.Message, ConsoleColor.Red);
                Console.WriteLine();

                System.Environment.Exit(1);
            }

            Utils.DrawMessage(prefix, Utils.CreateProgressBar(Utils.ProgressBarLength, 100), ConsoleColor.Green);
            Console.WriteLine();
        }
Пример #3
0
        public _Cache(_InternalData internalData)
        {
            Name  = new List <string>();
            Date  = new List <long>();
            Value = new List <double>();

            for (int i = 0; i < internalData.Events.Count; ++i)
            {
                Name.Add(internalData.Events[i].Name);
                Date.Add(internalData.Events[i].Date.Ticks);
                Value.Add(internalData.Events[i].Value);
            }
        }
Пример #4
0
        private void Parse(_InternalData internalData)
        {
            string prefix = "2/3 Parsing...";

            Utils.DrawMessage(prefix, Utils.CreateProgressBar(Utils.ProgressBarLength, 0), ConsoleColor.Gray);

            // Files to parse
            string[] files = Directory.GetFiles(Folder).Where(x => x.Contains(".json")).ToArray();

            // Count when we update progress bar
            int drawEvery = Utils.PercentIntervalByLength(files.Length);

            // Download
            try
            {
                if (Reload || !File.Exists(SerializedFile))
                {
                    for (int i = 0; i < files.Length; ++i)
                    {
                        // Parse only no errors data
                        string content = File.ReadAllText(files[i]);
                        content = content.Substring(content.IndexOf('{'));
                        content = content.Substring(0, content.LastIndexOf('}') + 1);
                        JObject json = JObject.Parse(content);
                        if (((string)json["status"]) == "ok")
                        {
                            JArray rows = (JArray)json["table"]["rows"];
                            foreach (JObject row in rows)
                            {
                                // Parse date
                                string   dateStr = ((JConstructor)row["c"][0]["v"]).ToString();
                                string[] parts   = dateStr.Split(new char[] { '\n' }).Select(x => x.Trim().Replace(",", "")).ToArray();
                                int      year    = int.Parse(parts[1]);
                                int      month   = int.Parse(parts[2]);
                                if (month == 0)
                                {
                                    month = 12;
                                    --year;
                                }
                                int      day  = int.Parse(parts[3]);
                                DateTime date = new DateTime(year, month, day).AddMonths(1);
                                month = date.Month;

                                // Parse value
                                int value = int.Parse((string)row["c"][1]["f"]);

                                // Save event to every day of month
                                while (date.Month == month)
                                {
                                    internalData.Events.Add(new _Event()
                                    {
                                        Name  = Path.GetFileNameWithoutExtension(files[i]).Split(new char[] { '-' })[1],
                                        Date  = date,
                                        Value = value
                                    });

                                    date = date.AddDays(1);
                                }
                            }
                        }

                        // Update progress bar
                        if (i % drawEvery == 0)
                        {
                            Utils.DrawMessage(prefix, Utils.CreateProgressBar(Utils.ProgressBarLength, (double)i / files.Length * 100.0), ConsoleColor.Gray);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine();
                Utils.DrawMessage("", ex.Message, ConsoleColor.Red);
                Console.WriteLine();

                System.Environment.Exit(1);
            }

            Utils.DrawMessage(prefix, Utils.CreateProgressBar(Utils.ProgressBarLength, 100), ConsoleColor.Green);
            Console.WriteLine();
        }