private static void ConfigurationToJSON(ConsoleScrapperData scrapperData, String fileName = "")
 {
     try
     {
         string rawJson = JsonConvert.SerializeObject(scrapperData, Formatting.Indented);
         if (!String.IsNullOrEmpty(fileName) && File.Exists(fileName) && fileName.ToLower().EndsWith(".json"))
         {
             File.WriteAllText(fileName, rawJson);
         }
         else
         {
             string tmpfileName = AppDomain.CurrentDomain.BaseDirectory + "\\config.json";
             File.WriteAllText(tmpfileName, rawJson);
         }
     }
     catch (Exception ex)
     {
         logger.Error(ex.ToString());
         //should do something here.
         Console.WriteLine("\nUnable to write json configuration file, {0}!\n", fileName);
     }
 }
        private static ConsoleScrapperData JSONToConfiguration(String fileName)
        {
            ConsoleScrapperData scrapperData;

            try
            {
                if (File.Exists(fileName))
                {
                    StreamReader json    = File.OpenText(fileName);
                    String       rawJson = json.ReadToEnd();
                    scrapperData = JsonConvert.DeserializeObject <ConsoleScrapperData>(rawJson);
                }
                else
                {
                    scrapperData = new ConsoleScrapperData();
                }
            }catch (Exception ex)
            {
                logger.Error(ex.ToString());
                scrapperData = new ConsoleScrapperData();
            }
            scrapperData.ProductsToCheck = scrapperData.ProductsToCheck.Distinct(new DistinctProductComparer()).ToList();
            return(scrapperData);
        }
        static void Main(string[] args)
        {
            logger = new LoggerCin(AppDomain.CurrentDomain.BaseDirectory + "logs.log");
            try
            {
                scrapperData = new ConsoleScrapperData();
                ParserResult <Options> result = CommandLine.Parser.Default.ParseArguments <Options>(args);
                if (!result.Errors.Any())
                {
                    var options = result.Value;
                    if (!String.IsNullOrEmpty(options.JsonPath))
                    {
                        jsonFile = options.JsonPath;
                    }
                    else
                    {
                        jsonFile = AppDomain.CurrentDomain.BaseDirectory + "\\config.json";
                    }

                    if (!String.IsNullOrEmpty(options.IFTTTKey))
                    {
                        scrapperData.IFTTTWebhookKey = options.IFTTTKey;
                    }
                    if (!String.IsNullOrEmpty(options.IFTTTEvent))
                    {
                        scrapperData.IFTTTEventName = options.IFTTTEvent;
                    }
                    if (options.Interval >= 1)
                    {
                        scrapperData.MonitoringInterval = options.Interval * 60000;
                    }
                }

                if (File.Exists(jsonFile))
                {
                    scrapperData = JSONToConfiguration(jsonFile);
                }

                if (scrapperData.ProductsToCheck.Any())
                {
                    scrapperData.ProductsToCheck = scrapperData.ProductsToCheck.OrderByDescending(p => p.StoreName).ToList();
                }

                System.Console.WriteLine("Running scheduled product check every {0} minutes...", (int)scrapperData.MonitoringInterval / 60000);
                if (!String.IsNullOrEmpty(scrapperData.IFTTTEventName) && !String.IsNullOrEmpty(scrapperData.IFTTTWebhookKey))
                {
                    System.Console.WriteLine("IFTTT Webhook key and event name added. Notifications enabled!");
                }
                else
                {
                    System.Console.WriteLine("No IFTTT webhook key and event name. Notifications disabled!");
                }

                if (scrapperData.ProductsToCheck.Any())
                {
                    System.Console.WriteLine("Monitoring " + scrapperData.ProductsToCheck.Count() + " products from configuration.");
                }

                aTimer = new Timer
                {
                    Interval = scrapperData.MonitoringInterval
                };
                aTimer.Elapsed  += OnTimedEvent;
                aTimer.AutoReset = true;
                aTimer.Enabled   = true;
                ConsoleLoop().Wait();
            }
            catch (Exception ex)
            {
                if (logger != null)
                {
                    logger.Fatal(ex.ToString());
                }
                else
                {
                    LoggerCin logger = new LoggerCin(AppDomain.CurrentDomain.BaseDirectory + "Fatal.log");
                }
            }
        }