Exemple #1
0
        /// <summary>
        /// Fetches the latest database from MKM, stores it as a CSV file and re-loads the internal structures of this database manager with new data.
        /// Also creates the SQL database and stores it as mkmtool.sqlite file.
        /// </summary>
        /// <returns><c>False</c> in case the update failed either due to bad response from MKM or IO problems.</returns>
        public bool UpdateDatabaseFiles()
        {
            try
            {
                MainView.Instance.LogMainWindow("Updating MKM inventory database...");
                // build inventory
                var doc = MKMInteract.RequestHelper.makeRequest("https://api.cardmarket.com/ws/v2.0/productlist", "GET");

                var node               = doc.GetElementsByTagName("response");
                var data               = Convert.FromBase64String(node.Item(0)["productsfile"].InnerText);
                var aDecompressed      = GzDecompress(data);
                var downloadedProducts = ConvertCSVtoDataTable(aDecompressed);

                // only join the downloaded with the current version, so that we don't overwrite other
                // data we cache into the Inventory (Rarity) that is not present in the productlist
                // the productList is sorted by productid, so just read the last few that we don't have yet
                // this assumes the previous rows never change, which is the case only if there is no error in them...
                for (int i = Inventory.Rows.Count; i < downloadedProducts.Rows.Count; i++)
                {
                    var insertRow = Inventory.NewRow();
                    foreach (DataColumn col2 in downloadedProducts.Columns)
                    {
                        insertRow[col2.ColumnName] = downloadedProducts.Rows[i][col2.ColumnName];
                    }
                    Inventory.Rows.Add(insertRow);
                }
                WriteTableAsCSV(@".\\mkminventory.csv", Inventory);
                Inventory.AcceptChanges();
                buildSinglesDatabase();
                MainView.Instance.LogMainWindow("MKM inventory database updated.");
            }
            catch (Exception eError)
            {
                LogError("downloading MKM inventory", eError.Message, true);
                return(false);
            }
            try
            {
                MainView.Instance.LogMainWindow("Updating MKM expansions database...");

                // build expansions
                var doc  = MKMInteract.RequestHelper.getExpansions("1"); // Only MTG at present
                var node = doc.GetElementsByTagName("expansion");

                using (StreamWriter exp = new StreamWriter(@".\\mkmexpansions.csv"))
                {
                    exp.WriteLine(string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\"",
                                                ExpansionsFields.ExpansionID, ExpansionsFields.Abbreviation, ExpansionsFields.Name, ExpansionsFields.ReleaseDate));
                    foreach (XmlNode nExpansion in node)
                    {
                        exp.WriteLine("\"" + nExpansion[ExpansionsFields.ExpansionID].InnerText + "\",\"" // put commas around each, in case wizards ever decide to do set with a comma in the name
                                      + nExpansion[ExpansionsFields.Abbreviation].InnerText + "\",\"" + nExpansion[ExpansionsFields.Name].InnerText + "\",\""
                                      + nExpansion[ExpansionsFields.ReleaseDate].InnerText + "\"");
                    }
                }
                MainView.Instance.LogMainWindow("MKM expansions database updated.");
            }
            catch (Exception eError)
            {
                LogError("downloading MKM expansions", eError.Message, true);
                return(false);
            }
            try
            {
                Expansions = ConvertCSVtoDataTable(@".\\mkmexpansions.csv"); // grab only MTG Singles
            }
            catch (Exception eError)
            {
                LogError("parsing mkm inventory, product list cannot be obtained", eError.Message, true);
                return(false);
            }

#if false // if you want the sql database for some reason, change this to true. MKMTool does not use it and creating it takes unnecessary time
            // Store the database as an SQL database. This is currently not actually used anywhere by MKMTool itself,
            // but it has existed in previous versions, might be used by other software and might have other uses in the future, so for now we keep it.
            // However, if something fails, we continue as if nothing happened.
            SQLiteConnection m_dbConnection;

            var sql2 = CreateTableSql(Inventory, "inventory");

            Console.WriteLine(sql2);

            if (!File.Exists("mkmtool.sqlite"))
            {
                SQLiteConnection.CreateFile("mkmtool.sqlite");

                m_dbConnection = new SQLiteConnection("Data Source=mkmtool.sqlite;Version=3;");
                m_dbConnection.Open();

                var sql = CreateTableSql(Inventory, "inventory");

                var command = new SQLiteCommand(sql, m_dbConnection);

                command.ExecuteNonQuery();

                sql = string.Format("CREATE TABLE expansions ({0}, {1}, {2}, {3})",
                                    ExpansionsFields.ExpansionID, ExpansionsFields.Abbreviation, ExpansionsFields.Name, ExpansionsFields.ReleaseDate);

                command = new SQLiteCommand(sql, m_dbConnection);

                command.ExecuteNonQuery();
            }
            else
            {
                //clean inventory table
                m_dbConnection = new SQLiteConnection("Data Source=mkmtool.sqlite;Version=3;");
                m_dbConnection.Open();

                var sql = "DELETE FROM inventory";

                var command = new SQLiteCommand(sql, m_dbConnection);
                command.ExecuteNonQuery();

                sql = "DELETE FROM expansions";

                command = new SQLiteCommand(sql, m_dbConnection);
                command.ExecuteNonQuery();

                sql = "VACUUM";

                command = new SQLiteCommand(sql, m_dbConnection);
                command.ExecuteNonQuery();
            }

            BulkInsertDataTable("inventory", Inventory, m_dbConnection);

            BulkInsertDataTable("expansions", Expansions, m_dbConnection);

            m_dbConnection.Close();
#endif
            return(true);
        }