Esempio n. 1
0
        /// <summary>
        /// For actually performing the check for cheap deals from a given user, expected to be run in its separate thread.
        /// </summary>
        /// <param name="selectedExpansionID">Leave as empty string if all expansion should be checked.</param>
        private void checkUserRun(string user, string isFoil, string isSigned, string isAltered, string isPlayset, string minCondition,
                                  bool domesticOnly, double maxPrice, double shippingAddition, double percentBelowOthers, bool checkTrend,
                                  System.Collections.Generic.List <string> selectedLanguage, string selectedExpansionID = "")
        {
            MainView.Instance.LogMainWindow("Check for cheap deals from seller '" + user + "'...");
            if (domesticOnly)
            {
                MainView.Instance.LogMainWindow("WARNING - domestics only is checked, if the specified seller is from a foreign country, no deals will be found.");
            }
            // Go through the stock of a specified user, checks for cheap deals and add them to the cart.
            int start = 0;

            while (true)
            {
                String      sUrl = "https://api.cardmarket.com/ws/v2.0/users/" + user + "/articles?start=" + start + "&maxResults=1000";
                XmlDocument doc2 = null;
                try
                {
                    // get the users stock, filtered by the selected parameters
                    doc2 = MKMInteract.RequestHelper.makeRequest(sUrl, "GET");
                }
                catch (Exception eError)
                {
                    MKMHelpers.LogError("looking for cheap deals from user " + user, eError.Message, false, sUrl);
                    break;
                }

                var node2 = doc2.GetElementsByTagName("article");
                foreach (XmlNode article in node2)
                {
                    if (selectedExpansionID != "") // if we want only cards from a specified set, check if this product is from that set using local database
                    {
                        DataRow card = MKMDbManager.Instance.GetSingleCard(article[MKMDbManager.InventoryFields.ProductID].InnerText);

                        if (card == null || card.Field <string>(MKMDbManager.InventoryFields.ExpansionID) != selectedExpansionID) // compare
                        {
                            continue;
                        }
                    }

                    if ( // do as much filtering here as possible to reduce the number of API calls
                        MKMHelpers.IsBetterOrSameCondition(article["condition"].InnerText, minCondition) &&
                        (!foilBox.Checked || article["isFoil"].InnerText == "true") &&
                        (!playsetBox.Checked || article["isPlayset"].InnerText == "true") &&
                        (selectedLanguage[0] == "" || article["language"]["idLanguage"].InnerText == selectedLanguage[0]) &&
                        (!signedBox.Checked || article["isSigned"].InnerText == "true") &&
                        (!signedBox.Checked || article["isAltered"].InnerText == "true") &&
                        (maxPrice >= Convert.ToDouble(article["price"].InnerText, CultureInfo.InvariantCulture))
                        )
                    {
                        MainView.Instance.LogMainWindow("Checking product ID: " + article["idProduct"].InnerText);
                        checkArticle(article["idProduct"].InnerText,
                                     selectedLanguage, minCondition, isFoil, isSigned,
                                     isAltered, isPlayset, article["idArticle"].InnerText, maxPrice, shippingAddition, percentBelowOthers, checkTrend);
                    }
                }
                if (node2.Count != 1000) // there is no additional items to fetch
                {
                    break;
                }
                start += 1000;
            }
            MainView.Instance.LogMainWindow("Check finished.");
        }
        private void checkEditionButton_Click(object sender, EventArgs e)
        {
            checkEditionButton.Enabled = false;
            checkEditionButton.Text    = "Checking cheap deals...";
            var isFoil       = "";
            var isSigned     = "";
            var isAltered    = "";
            var isPlayset    = "";
            var minCondition = conditionCombo.Text;

            if (foilBox.Checked)
            {
                isFoil = "true";
            }

            if (signedBox.Checked)
            {
                isSigned = "true";
            }

            if (alteredBox.Checked)
            {
                isAltered = "true";
            }

            if (playsetBox.Checked)
            {
                isPlayset = "true";
            }


            if (checkBoxUser.Enabled)
            {
                if (domnesticCheck.Checked)
                {
                    frm1.logBox.Invoke(new MainView.logboxAppendCallback(frm1.logBoxAppend),
                                       "WARNING - domestics only is checked, if the specified seller is from a foreign country, no deals will be found.\n");
                }
                // Go through the stock of a specified user, checks for cheap deals and add them to the cart.
                int    start           = 0;
                double maxAllowedPrice = Convert.ToDouble(maxPrice.Text);
                while (true)
                {
                    String sUrl = "https://api.cardmarket.com/ws/v2.0/users/" + textBoxUser.Text + "/articles?start=" + start + "&maxResults=1000";

                    try
                    {
                        // get the users stock, filtered by the selected parameters
                        var doc2 = MKMInteract.RequestHelper.makeRequest(sUrl, "GET");

                        var    node2    = doc2.GetElementsByTagName("article");
                        String language = (langCombo.SelectedItem as MKMHelpers.ComboboxItem).Value.ToString();
                        foreach (XmlNode article in node2)
                        {
                            if ( // do as much filtering here as possible to reduce the number of API calls
                                MKMHelpers.IsBetterOrSameCondition(article["condition"].InnerText, conditionCombo.Text) &&
                                (!foilBox.Checked || article["isFoil"].InnerText == "true") &&
                                (!playsetBox.Checked || article["isPlayset"].InnerText == "true") &&
                                (language == "" || article["language"]["idLanguage"].InnerText == language) &&
                                (!signedBox.Checked || article["isSigned"].InnerText == "true") &&
                                (!signedBox.Checked || article["isAltered"].InnerText == "true") &&
                                (maxAllowedPrice >= Convert.ToDouble(article["price"].InnerText, CultureInfo.InvariantCulture))
                                )
                            {
                                frm1.logBox.Invoke(new MainView.logboxAppendCallback(frm1.logBoxAppend),
                                                   "Checking: " + article["idProduct"].InnerText + "\n");
                                checkArticle(article["idProduct"].InnerText,
                                             language, minCondition, isFoil, isSigned,
                                             isAltered, isPlayset, article["idArticle"].InnerText);

                                frm1.logBox.Invoke(new MainView.logboxAppendCallback(frm1.logBoxAppend), "Done.\n");
                            }
                        }
                        if (node2.Count != 1000) // there is no additional items to fetch
                        {
                            break;
                        }
                    }
                    catch (Exception eError)
                    {
                        frm1.logBox.Invoke(new MainView.logboxAppendCallback(frm1.logBoxAppend),
                                           "ERR Msg : " + eError.Message + "\n");
                        frm1.logBox.Invoke(new MainView.logboxAppendCallback(frm1.logBoxAppend), "ERR URL : " + sUrl + "\n");

                        using (var sw = File.AppendText(@".\\error_log.txt"))
                        {
                            sw.WriteLine("ERR Msg : " + eError.Message);
                            sw.WriteLine("ERR URL : " + sUrl);
                        }
                    }
                    start += 1000;
                }
            }
            else
            {
                var sEdId = editionBox.SelectedItem.ToString();

                var sT = dt.Clone();

                var result = dt.Select(string.Format("[Expansion ID] = '{0}'", sEdId));

                foreach (var row in result)
                {
                    sT.ImportRow(row);
                }

                foreach (DataRow oRecord in sT.Rows)
                {
                    frm1.logBox.Invoke(new MainView.logboxAppendCallback(frm1.logBoxAppend),
                                       "Checking: " + oRecord["idProduct"] + "\n");
                    checkArticle(oRecord["idProduct"].ToString(),
                                 (langCombo.SelectedItem as MKMHelpers.ComboboxItem).Value.ToString(), minCondition, isFoil, isSigned,
                                 isAltered, isPlayset, "");

                    frm1.logBox.Invoke(new MainView.logboxAppendCallback(frm1.logBoxAppend), "Done.\n");
                }
            }

            checkEditionButton.Text    = "Check now";
            checkEditionButton.Enabled = true;
        }