Пример #1
0
        private AmazonWatchItem CheckPriceTask(AmazonWatchItem item)
        {
            try
            {
                var itemId               = item.Id;
                var currentItemTitle     = item.Title;
                var itemUrl              = item.Url;
                var currentItemPrice     = item.Price;
                var itemLastModifiedDate = item.LastModifiedDate;
                var itemCreateDate       = item.CreateDate;
                var itemDesiredPrice     = item.DesiredPrice;

                AmazonScraper scraper = new AmazonScraper(itemUrl);
                scraper.RequestHtml();

                var updatedTitle = scraper.Title;
                var updatedPrice = CommonFunctions.StringPriceToFloat(scraper.Price);

                AmazonWatchItem updatedItem = new AmazonWatchItem(itemId,
                                                                  !string.IsNullOrWhiteSpace(updatedTitle) ? updatedTitle : currentItemTitle,
                                                                  itemUrl,
                                                                  updatedPrice,
                                                                  itemDesiredPrice,
                                                                  itemCreateDate,
                                                                  !string.IsNullOrWhiteSpace(updatedTitle) && updatedPrice > 0 ? DateTime.Now : itemLastModifiedDate,
                                                                  default(DateTime));

                return(updatedItem);
            }
            catch (Exception e)
            {
                CommonFunctions.Log.Error("CheckPriceTask: Error checking prices", e);
            }
            return(null);
        }
        private void AmazonItemEdit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                AmazonWatchItem itemToEdit = (AmazonWatchItem)watchListItems_datagrid.SelectedItem;

                if (itemToEdit != null)
                {
                    NumericInputDialog priceInput = new NumericInputDialog("Enter your desired price:");
                    if (priceInput.ShowDialog() == true)
                    {
                        float.TryParse(priceInput.Answer.Trim(), out float desiredPrice);

                        if (desiredPrice > 0)
                        {
                            CommonFunctions.Log.Debug($"Updaing price on {itemToEdit.Title} from {itemToEdit.DesiredPrice.ToString()} to {desiredPrice.ToString()}");
                            DBHelper.UpdateDesiredPrice(CommonFunctions.ItemsConnectionString, itemToEdit.Id, desiredPrice);
                            FillWatchList();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CommonFunctions.Log.Error("Error editing item price", ex);
            }
        }
        public void FillWatchList()
        {
            watchListItems_datagrid.ItemsSource = null;
            watchListItems_datagrid.Items.Refresh();
            List <AmazonWatchItem> watchList = new List <AmazonWatchItem>();

            DataTable itemsDT = DBHelper.GetItemInformation(CommonFunctions.ItemsConnectionString);

            foreach (DataRow row in itemsDT.Rows)
            {
                int    itemID       = Int32.Parse(row["ItemID"].ToString());
                string title        = row["Title"].ToString();
                string url          = row["Url"].ToString();
                float  amazonPrice  = float.Parse(row["AmazonPrice"].ToString());
                float  desiredPrice = float.Parse(row["DesiredPrice"].ToString());
                DateTime.TryParse(row["CreateDate"].ToString(), out DateTime createDate);
                DateTime.TryParse(row["LastModifiedDate"].ToString(), out DateTime lastModifiedDate);
                DateTime.TryParse(row["LastNotifiedDate"].ToString(), out DateTime lastNotifiedDate);

                CommonFunctions.Log.Debug($"Adding item to watch list:= '{itemID}', '{title}', '{url}', '{amazonPrice}', '{desiredPrice}', '{createDate}', '{lastModifiedDate}', '{lastNotifiedDate}'");
                AmazonWatchItem item = new AmazonWatchItem(itemID, title, url, amazonPrice, desiredPrice, createDate, lastModifiedDate, lastNotifiedDate);
                watchList.Add(item);
            }
            if (watchList.Count > 0)
            {
                watchListItems_datagrid.ItemsSource = watchList;
                watchListItems_datagrid.Items.Refresh();
            }
        }
        private void AmazonItemDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                AmazonWatchItem itemToDelete = (AmazonWatchItem)watchListItems_datagrid.SelectedItem;
                CommonFunctions.Log.Debug($"Deleting item {itemToDelete.Title}");

                DBHelper.DeleteItem(CommonFunctions.ItemsConnectionString, itemToDelete.Id);
                FillWatchList();
            }
            catch (Exception ex)
            {
                CommonFunctions.Log.Error("Error deleting item", ex);
            }
        }
Пример #5
0
        private static void ComparePrices()
        {
            List <AmazonWatchItem> notificationList = new List <AmazonWatchItem>();

            DataTable itemsTable = DBHelper.GetItemInformation(CommonFunctions.ItemsConnectionString);

            foreach (DataRow row in itemsTable.Rows)
            {
                try
                {
                    DisplayLogItem priceLog = new DisplayLogItem();

                    int    itemID       = Int32.Parse(row["ItemID"].ToString());
                    string title        = row["Title"].ToString();
                    string url          = row["Url"].ToString();
                    float  amazonPrice  = float.Parse(row["AmazonPrice"].ToString());
                    float  desiredPrice = float.Parse(row["DesiredPrice"].ToString());
                    DateTime.TryParse(row["CreateDate"].ToString(), out DateTime createDate);
                    DateTime.TryParse(row["LastModifiedDate"].ToString(), out DateTime lastModifiedDate);
                    DateTime.TryParse(row["LastNotifiedDate"].ToString(), out DateTime lastNotifiedDate);

                    CommonFunctions.Log.Debug($"Comparing price for {title}:= ${amazonPrice} vs ${desiredPrice}");

                    if (amazonPrice <= desiredPrice && amazonPrice > 0)
                    {
                        if (!CommonFunctions.UserSettings.LimitNotifications)
                        {
                            AmazonWatchItem notifyItem = new AmazonWatchItem(itemID, title, url, amazonPrice, desiredPrice, createDate, lastModifiedDate, lastNotifiedDate);
                            notificationList.Add(notifyItem);
                        }
                        else if (CommonFunctions.UserSettings.LimitNotifications && lastNotifiedDate.Date != DateTime.Now.Date)
                        {
                            AmazonWatchItem notifyItem = new AmazonWatchItem(itemID, title, url, amazonPrice, desiredPrice, createDate, lastModifiedDate, lastNotifiedDate);
                            notificationList.Add(notifyItem);
                        }

                        DisplayLogItem notifyLog = new DisplayLogItem
                        {
                            LogText = DisplayLogItem.CreateLogText($"{CommonFunctions.RemoveSQLCharacters(title)}",
                                                                   $"is at or below your desired price of ${desiredPrice}! You should consider buying!",
                                                                   $"Current Price: ${amazonPrice}",
                                                                   $"Desired Price: ${desiredPrice}"),
                            LogImage = DisplayLogItem.GetLogIcon(DisplayLogItem.LogIcon.Buy)
                        };


                        notifyLog.Log();
                    }
                    else
                    {
                        priceLog.LogText = DisplayLogItem.CreateLogText($"{CommonFunctions.RemoveSQLCharacters(title)}",
                                                                        amazonPrice > 0 ? $"Current Price: ${amazonPrice}" : $"Current Price: Unable to find",
                                                                        $"Desired Price: ${desiredPrice}");

                        priceLog.LogImage = amazonPrice > 0 ? DisplayLogItem.GetLogIcon(DisplayLogItem.LogIcon.Item) : DisplayLogItem.GetLogIcon(DisplayLogItem.LogIcon.NoPriceItem);
                        priceLog.Log();
                    }
                }
                catch (Exception compareError)
                {
                    CommonFunctions.Log.Error("Error comparing desired price and purchase price", compareError);

                    DisplayLogItem compareErrorLog = new DisplayLogItem("The was an error comparing prices for one of your items", DisplayLogItem.LogIcon.Error);
                    compareErrorLog.Log();
                }
            }

            if (notificationList.Count > 0)
            {
                if (CommonFunctions.UserSettings.EmailNotifications || CommonFunctions.UserSettings.TextNotifications)
                {
                    DisplayLogItem notifyLog = new DisplayLogItem();
                    notifyLog.LogText  = DisplayLogItem.CreateLogText("Notifications", $"Sending alert for {notificationList.Count} " + (notificationList.Count > 1 ? "items" : "item"));
                    notifyLog.LogImage = DisplayLogItem.GetLogIcon(DisplayLogItem.LogIcon.Notification);
                    notifyLog.Log();
                }

                Notifier notifier = new Notifier(notificationList);
                notifier.Notify();
            }
        }