diff --git a/MxWDB/JSON/AHDump.cs b/MxWDB/JSON/AHDump.cs index a3b3b99..4b8982a 100644 --- a/MxWDB/JSON/AHDump.cs +++ b/MxWDB/JSON/AHDump.cs @@ -79,8 +79,9 @@ namespace MxWDB.JSON aucds.Tables.Add(auctb); */ - var jf = File.ReadAllText(string.Format("json/ah.dump.json")); - var root = JObject.Parse(jf).SelectToken("auctions").ToString(); + string rawContent = File.ReadAllText("json/ah.dump.json"); + string cleanJson = JsonConvert.DeserializeObject(rawContent); + var root = JObject.Parse(cleanJson).SelectToken("auctions").ToString(); var RootAuctions = JsonConvert.DeserializeObject>(root); DataTable auctb = new DataTable("auclist"); @@ -165,8 +166,9 @@ namespace MxWDB.JSON aucds.Tables.Add(auctb); */ - var jf = File.ReadAllText(string.Format("json/ah.com.dump.json")); - var root = JObject.Parse(jf).SelectToken("auctions").ToString(); + string rawContent = File.ReadAllText("json/ah.com.dump.json"); + string cleanJson = JsonConvert.DeserializeObject(rawContent); + var root = JObject.Parse(cleanJson).SelectToken("auctions").ToString(); var RootAuctions = JsonConvert.DeserializeObject>(root); DataTable auctbcom = new DataTable("auclistcom"); diff --git a/MxWDB/JSON/AHUrl.cs b/MxWDB/JSON/AHUrl.cs index ed4e656..cdad3bb 100644 --- a/MxWDB/JSON/AHUrl.cs +++ b/MxWDB/JSON/AHUrl.cs @@ -1,9 +1,16 @@ -using System; +using Newtonsoft.Json; +using RestSharp; +using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; +using static MxWDB.JSON.AHDump; +using static MxWDB.JSON.Token; namespace MxWDB.JSON { @@ -20,24 +27,85 @@ namespace MxWDB.JSON public List files { get; set; } } - public static void GetDump(string key, string secret, string token) + public class Auction { - using (WebClient wc = new WebClient()) - { - ServicePointManager.ServerCertificateValidationCallback += (p1, p2, p3, p4) => true; - ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; - Utilities.Web.DownloadFileNoAsync($"https://us.api.blizzard.com/data/wow/connected-realm/{Settings.GetRID()}/auctions?namespace=dynamic-us&locale=en_US&access_token={token}", "json/ah.dump.json"); - } + public int id { get; set; } + public Item item { get; set; } + public Int64 buyout { get; set; } + public int quantity { get; set; } + public string time_left { get; set; } } - public static void GetDumpCom(string key, string secret, string token) + public class Commodities { - using (WebClient wc = new WebClient()) - { - ServicePointManager.ServerCertificateValidationCallback += (p1, p2, p3, p4) => true; - ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; - Utilities.Web.DownloadFileNoAsync($"https://us.api.blizzard.com/data/wow/auctions/commodities?namespace=dynamic-us&access_token={token}", "json/ah.com.dump.json"); - } + public string href { get; set; } + } + + public class ConnectedRealm + { + public string href { get; set; } + } + + public class Item + { + public int id { get; set; } + public int context { get; set; } + public List bonus_lists { get; set; } + public List modifiers { get; set; } + } + + public class Links + { + public Self self { get; set; } + } + + public class Modifier + { + public int type { get; set; } + public int value { get; set; } + } + + public class AuctionsRoot + { + public Links _links { get; set; } + public ConnectedRealm connected_realm { get; set; } + public List auctions { get; set; } + public Commodities commodities { get; set; } + } + + public class Self + { + public string href { get; set; } + } + + private static readonly HttpClient client = new HttpClient(); + + public static async Task GetDump(string key, string secret, string token) + { + var ahRequest = new HttpRequestMessage(HttpMethod.Get, $"https://us.api.blizzard.com/data/wow/connected-realm/{Settings.GetRID()}/auctions?namespace=dynamic-us&locale=en_US"); + + // This is the critical part: Adding the token to the header + ahRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); + + var ahResponse = await client.SendAsync(ahRequest); + var content = await ahResponse.Content.ReadAsStringAsync(); + + string output = JsonConvert.SerializeObject(content, Formatting.Indented); + File.WriteAllText("json/ah.dump.json", output); + } + + public static async Task GetDumpCom(string key, string secret, string token) + { + var ahRequest = new HttpRequestMessage(HttpMethod.Get, $"https://us.api.blizzard.com/data/wow/auctions/commodities?namespace=dynamic-us"); + + // This is the critical part: Adding the token to the header + ahRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); + + var ahResponse = await client.SendAsync(ahRequest); + var content = await ahResponse.Content.ReadAsStringAsync(); + + string output = JsonConvert.SerializeObject(content, Formatting.Indented); + File.WriteAllText("json/ah.com.dump.json", output); } } } diff --git a/MxWDB/JSON/AHUrl_Com.cs b/MxWDB/JSON/AHUrl_Com.cs new file mode 100644 index 0000000..d0f5e07 --- /dev/null +++ b/MxWDB/JSON/AHUrl_Com.cs @@ -0,0 +1,97 @@ +using Newtonsoft.Json; +using RestSharp; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; +using static MxWDB.JSON.AHDump; +using static MxWDB.JSON.Token; + +namespace MxWDB.JSON +{ + class AHUrl_Com + { + public class JSON + { + public string url { get; set; } + public long lastModified { get; set; } + } + + public class RootObject + { + public List files { get; set; } + } + + public class Auction + { + public int id { get; set; } + public Item item { get; set; } + public int quantity { get; set; } + public Int64 unit_price { get; set; } + public string time_left { get; set; } + } + + public class Commodities + { + public string href { get; set; } + } + + public class ConnectedRealm + { + public string href { get; set; } + } + + public class Item + { + public int id { get; set; } + public int context { get; set; } + public List bonus_lists { get; set; } + public List modifiers { get; set; } + } + + public class Links + { + public Self self { get; set; } + } + + public class Modifier + { + public int type { get; set; } + public int value { get; set; } + } + + public class AuctionsComRoot + { + public Links _links { get; set; } + public ConnectedRealm connected_realm { get; set; } + public List auctions { get; set; } + public Commodities commodities { get; set; } + } + + public class Self + { + public string href { get; set; } + } + + private static readonly HttpClient client = new HttpClient(); + + public static async Task GetDumpCom(string key, string secret, string token) + { + var ahRequest = new HttpRequestMessage(HttpMethod.Get, $"https://us.api.blizzard.com/data/wow/auctions/commodities?namespace=dynamic-us"); + + // This is the critical part: Adding the token to the header + ahRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); + + var ahResponse = await client.SendAsync(ahRequest); + var content = await ahResponse.Content.ReadAsStringAsync(); + + string output = JsonConvert.SerializeObject(content, Formatting.Indented); + File.WriteAllText("json/ah.com.dump.json", output); + } + } +} diff --git a/MxWDB/JSON/GenSpecial.cs b/MxWDB/JSON/GenSpecial.cs index 088fc5f..09c49b7 100644 --- a/MxWDB/JSON/GenSpecial.cs +++ b/MxWDB/JSON/GenSpecial.cs @@ -1,4 +1,4 @@ -using MySqlConnector; +using MySql.Data.MySqlClient; using Newtonsoft.Json; using System; using System.Collections.Generic; diff --git a/MxWDB/JSON/IndexGen.cs b/MxWDB/JSON/IndexGen.cs index 7b0f1e5..baf265d 100644 --- a/MxWDB/JSON/IndexGen.cs +++ b/MxWDB/JSON/IndexGen.cs @@ -1,4 +1,4 @@ -using MySqlConnector; +using MySql.Data.MySqlClient; using Newtonsoft.Json; using System; using System.Collections.Generic; @@ -54,6 +54,7 @@ namespace MxWDB.JSON { catid = 1; itemid = Convert.ToInt32(d); + //Utilities.DB.GenHistoryTable(itemid, GetItemValue(itemid)); Rows.Add(string.Format("('{0}','{1}','{2}','{3}','{4}')", itemid, cid, catid, GetItemValue(itemid), GetItemQuantity(itemid))); } } @@ -64,6 +65,7 @@ namespace MxWDB.JSON { catid = 2; itemid = Convert.ToInt32(d); + //Utilities.DB.GenHistoryTable(itemid, GetItemValue(itemid)); Rows.Add(string.Format("('{0}','{1}','{2}','{3}','{4}')", itemid, cid, catid, GetItemValue(itemid), GetItemQuantity(itemid))); } } @@ -74,6 +76,7 @@ namespace MxWDB.JSON { catid = 3; itemid = Convert.ToInt32(d); + //Utilities.DB.GenHistoryTable(itemid, GetItemValue(itemid)); Rows.Add(string.Format("('{0}','{1}','{2}','{3}','{4}')", itemid, cid, catid, GetItemValue(itemid), GetItemQuantity(itemid))); } } @@ -84,6 +87,7 @@ namespace MxWDB.JSON { catid = 4; itemid = Convert.ToInt32(d); + //Utilities.DB.GenHistoryTable(itemid, GetItemValue(itemid)); Rows.Add(string.Format("('{0}','{1}','{2}','{3}','{4}')", itemid, cid, catid, GetItemValue(itemid), GetItemQuantity(itemid))); } } @@ -94,6 +98,7 @@ namespace MxWDB.JSON { catid = 5; itemid = Convert.ToInt32(d); + //Utilities.DB.GenHistoryTable(itemid, GetItemValue(itemid)); Rows.Add(string.Format("('{0}','{1}','{2}','{3}','{4}')", itemid, cid, catid, GetItemValue(itemid), GetItemQuantity(itemid))); } } @@ -104,6 +109,7 @@ namespace MxWDB.JSON { catid = 6; itemid = Convert.ToInt32(d); + //Utilities.DB.GenHistoryTable(itemid, GetItemValue(itemid)); Rows.Add(string.Format("('{0}','{1}','{2}','{3}','{4}')", itemid, cid, catid, GetItemValue(itemid), GetItemQuantity(itemid))); } } @@ -114,6 +120,7 @@ namespace MxWDB.JSON { catid = 7; itemid = Convert.ToInt32(d); + //Utilities.DB.GenHistoryTable(itemid, GetItemValue(itemid)); Rows.Add(string.Format("('{0}','{1}','{2}','{3}','{4}')", itemid, cid, catid, GetItemValue(itemid), GetItemQuantity(itemid))); } } @@ -124,6 +131,7 @@ namespace MxWDB.JSON { catid = 8; itemid = Convert.ToInt32(d); + //Utilities.DB.GenHistoryTable(itemid, GetItemValue(itemid)); Rows.Add(string.Format("('{0}','{1}','{2}','{3}','{4}')", itemid, cid, catid, GetItemValue(itemid), GetItemQuantity(itemid))); } } diff --git a/MxWDB/JSON/Settings.cs b/MxWDB/JSON/Settings.cs index ddc57e5..b450fc7 100644 --- a/MxWDB/JSON/Settings.cs +++ b/MxWDB/JSON/Settings.cs @@ -21,6 +21,9 @@ namespace MxWDB.JSON public string dbport { get; set; } public string dbuser { get; set; } public string dbpass { get; set; } + public int dump_day { get; set; } + public int dump_hour { get; set; } + public bool completed { get; set; } public bool debug { get; set; } } @@ -61,8 +64,65 @@ namespace MxWDB.JSON /// public static int GetRID() => JsonConvert.DeserializeObject(File.ReadAllText("json/settings.json")).realmid; /// + ///Load the Dump Day from the Settings Json. + /// + public static int GetDDay() => JsonConvert.DeserializeObject(File.ReadAllText("json/settings.json")).dump_day; + /// + ///Load the Dump Hour from the Settings Json. + /// + public static int GetDHour() => JsonConvert.DeserializeObject(File.ReadAllText("json/settings.json")).dump_hour; + /// + ///Used for the backend Hypervisor. + /// + public static bool GetCompleted() => JsonConvert.DeserializeObject(File.ReadAllText("json/settings.json")).completed; + /// ///Used for developper. So far, it prevent the software from downloading a new dump, saving a lot of time for testing. /// public static bool GetDebug() => JsonConvert.DeserializeObject(File.ReadAllText("json/settings.json")).debug; + + public static void SetSuccess(bool comp) + { + SETTINGS settings = JsonConvert.DeserializeObject(File.ReadAllText("json/settings.json")); + File.WriteAllText("json/settings.json", JsonConvert.SerializeObject((object)new SETTINGS() + { + apikey = settings.apikey, + apisecret = settings.apisecret, + dbname = settings.dbname, + region = settings.region, + realmid = settings.realmid, + dbhost = settings.dbhost, + dbport = settings.dbport, + dbuser = settings.dbuser, + dbpass = settings.dbpass, + dump_day = settings.dump_day, + dump_hour = settings.dump_hour, + completed = comp, + debug = settings.debug + }, (Formatting)1)); + } + + public static void SetDumpPeriod() + { + DateTime now = DateTime.Now; + int day = now.Day; + int hour = now.Hour; + SETTINGS settings = JsonConvert.DeserializeObject(File.ReadAllText("json/settings.json")); + File.WriteAllText("json/settings.json", JsonConvert.SerializeObject((object)new SETTINGS() + { + apikey = settings.apikey, + apisecret = settings.apisecret, + dbname = settings.dbname, + region = settings.region, + realmid = settings.realmid, + dbhost = settings.dbhost, + dbport = settings.dbport, + dbuser = settings.dbuser, + dbpass = settings.dbpass, + dump_day = day, + dump_hour = hour, + completed = settings.completed, + debug = settings.debug + }, (Formatting)1)); + } } } diff --git a/MxWDB/JSON/Token.cs b/MxWDB/JSON/Token.cs index 28881d9..458e954 100644 --- a/MxWDB/JSON/Token.cs +++ b/MxWDB/JSON/Token.cs @@ -6,6 +6,7 @@ using System.Net; using System.Text; using System.Threading.Tasks; using System.IO; +using RestSharp; namespace MxWDB.JSON { @@ -16,18 +17,18 @@ namespace MxWDB.JSON public Self self { get; set; } } - public class Root + public class TokenRoot { public Links _links { get; set; } public long last_updated_timestamp { get; set; } - public int price { get; set; } + public Int64 price { get; set; } } public class Self { public string href { get; set; } } - public static void GetToken(string token) + /*public static void GetToken(string token) { using (WebClient wc = new WebClient()) { @@ -35,8 +36,33 @@ namespace MxWDB.JSON ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; Utilities.Web.DownloadFileNoAsync($"https://us.api.blizzard.com/data/wow/token/index?namespace=dynamic-us&locale=en_US&access_token={token}", "json/token.json"); } + }*/ + + /// + /// Get an access token from BNET using a registered application Client ID and Secret ID. + /// + /// Your registered application Client ID. + /// Your registered application Secret ID. + /// + public static void GetToken(string token) + { + ServicePointManager.SecurityProtocol |= SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; + + RestClient client = new RestClient($"https://{JSON.Settings.GetRegion()}.api.blizzard.com/data/wow/token/index?namespace=dynamic-us&locale=en_US"); + var request = new RestRequest + { + Method = RestSharp.Method.Get + }; + request.AddHeader("Authorization", $"Bearer {token}"); + request.AddHeader("Content-type", "application/x-www-form-urlencoded"); + RestResponse response = client.Execute(request); + + var tokenResponse = JsonConvert.DeserializeObject(response.Content); + + string output = JsonConvert.SerializeObject(tokenResponse, Formatting.Indented); + File.WriteAllText("json/token.json", output); } - public static int GetTokenValue() => JsonConvert.DeserializeObject(File.ReadAllText("json/settings.json")).price; + public static Int64 GetTokenValue() => JsonConvert.DeserializeObject(File.ReadAllText("json/settings.json")).price; } } diff --git a/MxWDB/MxWDB.csproj b/MxWDB/MxWDB.csproj index aba414a..9c48402 100644 --- a/MxWDB/MxWDB.csproj +++ b/MxWDB/MxWDB.csproj @@ -54,14 +54,14 @@ ..\packages\Microsoft.Extensions.Logging.Abstractions.7.0.1\lib\net462\Microsoft.Extensions.Logging.Abstractions.dll - - ..\packages\MySqlConnector.2.3.7\lib\net48\MySqlConnector.dll + + ..\packages\MySql.Data.9.0.0\lib\net48\MySql.Data.dll ..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll - - ..\packages\RestSharp.111.4.1\lib\net48\RestSharp.dll + + ..\packages\RestSharp.112.0.0\lib\net48\RestSharp.dll @@ -115,6 +115,7 @@ + diff --git a/MxWDB/Program.cs b/MxWDB/Program.cs index 2af80f8..5986b1d 100644 --- a/MxWDB/Program.cs +++ b/MxWDB/Program.cs @@ -21,7 +21,7 @@ namespace MxWDB { internal class Program { - public static string version = "1.0.0_WoW11.0"; + public static string version = "1.2.0_WoW12.0.1"; public static int line = 0; @@ -29,7 +29,7 @@ namespace MxWDB public static string state = "init"; public static string oauth_token = ""; - static void Main(string[] args) + static async Task Main(string[] args) { //// Main Server Loop while (serverloop) @@ -38,6 +38,7 @@ namespace MxWDB { case "init": MSG.Splash(); + Settings.SetSuccess(false); if (IO.FirstRun()) { IO.CreateDirectory("json"); @@ -57,7 +58,7 @@ namespace MxWDB MSG.CMW($"[{state}] DBName: {JSON.Settings.GetDBName()}", true, 2); MSG.CMW($"[{state}] DBUser: {JSON.Settings.GetDBUser()}", true, 2); MSG.CMW($"[{state}] Region: {JSON.Settings.GetRegion()}", true, 2); - MSG.CMW($"[{state}] RealmID: {JSON.Settings.GetRID()}", true, 2); + MSG.CMW($"[{state}] RealmID: {JSON.Settings.GetRID()}", true, 2); state = "oauth"; } else { @@ -70,34 +71,47 @@ namespace MxWDB } break; case "oauth": + DateTime now = DateTime.Now; MSG.CMW($"[{state}] Getting an access token from BNET...", true, 1); - oauth_token = OAuth.GetAccessToken(JSON.Settings.GetAPIKey(), JSON.Settings.GetAPISecret()); - MSG.CMW($"[{state}] Access Token: {oauth_token}.", true, 2); - if (args.Contains("--token")) { state = "token"; } else { state = "dump"; } + oauth_token = await OAuth.GetAccessToken(Settings.GetAPIKey(), Settings.GetAPISecret()); + MSG.CMW($"[{state}] Access Token: {oauth_token}.", true, 2); + if (now.Day == Settings.GetDDay() && now.Hour == Settings.GetDHour()) + { + MSG.CMW($"[{state}] No need to get a new dump. Skipping.", true, 1); + state = "checkdb"; + } else + { + state = "dump"; + } + if (args.Contains("--token")) { state = "token"; } break; /// ARGS - WoW Token case "token": MSG.CMW($"[{state}] Getting the latest Token info from BNET...", true, 1); Token.GetToken(oauth_token); - int v = JsonConvert.DeserializeObject(File.ReadAllText("json/token.json")).price; + Int64 v = JsonConvert.DeserializeObject(File.ReadAllText("json/token.json")).price; DB.UpdateTokenValue(v); - int copper = v % 100; + Int64 copper = v % 100; v = (v - copper) / 100; - int silver = v % 100; - int gold = (v - silver) / 100; - MSG.CMW($"[{state}] WoW Token from region {Settings.GetRegion()} is worth {gold}g.", true, 1); + Int64 silver = v % 100; + Int64 gold = (v - silver) / 100; + MSG.CMW($"[{state}] WoW Token from region {Settings.GetRegion()} is worth {gold}g.", true, 1); + Settings.SetSuccess(true); serverloop =false; break; case "dump": if (!JSON.Settings.GetDebug()) { + if (File.Exists("json/ah.dump.json")) { File.Delete("json/ah.dump.json"); } + if (File.Exists("json/ah.com.dump.json")) { File.Delete("json/ah.com.dump.json"); } MSG.CMW($"[{state}] Downloading the latest AH dump from BNet...", true, 1); - JSON.AHUrl.GetDump(JSON.Settings.GetAPIKey(), JSON.Settings.GetAPISecret(), oauth_token); + await AHUrl.GetDump(JSON.Settings.GetAPIKey(), JSON.Settings.GetAPISecret(), oauth_token); MSG.CMW($"[{state}] Downloading the latest AH Commodities dump from BNet...", true, 1); - JSON.AHUrl.GetDumpCom(JSON.Settings.GetAPIKey(), JSON.Settings.GetAPISecret(), oauth_token); + await AHUrl_Com.GetDumpCom(JSON.Settings.GetAPIKey(), JSON.Settings.GetAPISecret(), oauth_token); if (IO.FileIsDownloaded("json/ah.dump.json") && IO.FileIsDownloaded("json/ah.com.dump.json")) { MSG.CMW($"[{state}] Both Dump appears to be correctly downloaded.", true, 2); + Settings.SetDumpPeriod(); state = "checkdb"; } else @@ -119,7 +133,7 @@ namespace MxWDB MSG.CMW($"[{state}] We can connect to the DB.", true, 2); MSG.CMW($"[{state}] Cleaning the DB...", true, 1); DB.CleanDB(); - state = "genintable"; + state = "gendb"; } else { MSG.CMW($"[{state}] We can't connect to the DB.", true, 3); @@ -129,17 +143,14 @@ namespace MxWDB Console.ReadKey(); } break; - case "genintable": + case "gendb": MSG.CMW($"[{state}] Generating the internal table from Auctions Data...", true, 1); JSON.AHDump.GenTable(); - MSG.CMW($"[{state}] Generating the internal table from Commodities Data...", true, 1); - JSON.AHDump.GenTableCom(); - state = "gendbtable"; - break; - case "gendbtable": MSG.CMW($"[{state}] Generating the DB tables from Auctions Data...", true, 1); DB.GenItemsTable(); DB.GenListingTable(); + MSG.CMW($"[{state}] Generating the internal table from Commodities Data...", true, 1); + JSON.AHDump.GenTableCom(); MSG.CMW($"[{state}] Generating the DB tables from Commodities Data...", true, 1); DB.GenItemsTableCom(); DB.GenListingTableCom(); @@ -149,8 +160,9 @@ namespace MxWDB MSG.CMW($"[{state}] Generating the Index Table...", true, 1); JSON.IndexGen.GenIndex("json/index.json"); MSG.CMW($"[{state}] Generating the Special Table...", true, 1); - JSON.SpecialGen.GenSpecial("json/special.json"); - serverloop=false; + JSON.SpecialGen.GenSpecial("json/special.json"); + Settings.SetSuccess(true); + serverloop =false; break; } } diff --git a/MxWDB/Utilities/DB.cs b/MxWDB/Utilities/DB.cs index e5649eb..af0b5bd 100644 --- a/MxWDB/Utilities/DB.cs +++ b/MxWDB/Utilities/DB.cs @@ -1,5 +1,5 @@ using MxWDB.JSON; -using MySqlConnector; +using MySql.Data.MySqlClient; using Newtonsoft.Json; using System; using System.Collections.Generic; @@ -244,7 +244,6 @@ namespace MxWDB.Utilities { mcc.Open(); var temp = mcc.State.ToString(); - MSG.CMW(temp, true,4); if (mcc.State == ConnectionState.Open && temp == "Open") { mcc.Close(); @@ -310,8 +309,9 @@ namespace MxWDB.Utilities myCmd.CommandType = CommandType.Text; myCmd.ExecuteNonQuery(); } + mcc.Close(); } - } + } public static void GenItemsTableCom() { @@ -327,7 +327,7 @@ namespace MxWDB.Utilities Int64 minimum = 0; int ie = JSON.AHDump.aucdscom.Tables[0].Select(string.Format("iid = '{0}'", il)).Length; if (ie >= 1) - { + { if (JSON.AHDump.aucdscom.Tables["auclistcom"].Compute("AVG(uv)", string.Format("iid = '{0}'", il)) != DBNull.Value) { median = Convert.ToInt64(JSON.AHDump.aucdscom.Tables["auclistcom"].Compute("AVG(uv)", string.Format("iid = '{0}'", il))); @@ -352,6 +352,32 @@ namespace MxWDB.Utilities myCmd.CommandType = CommandType.Text; myCmd.ExecuteNonQuery(); } + mcc.Close(); + } + } + + public static void GenHistoryTable(int itemid, Int64 value) + { + string hcolumn = ""; + DateTime now = DateTime.Now; + if (now.Hour < 10) + { + hcolumn = $"h0{now.Hour}"; + } else + { + hcolumn = $"h{now.Hour}"; + } + + using (MySqlConnection mcc = ItemDB()) + { + string cmd = $"INSERT INTO ah_history (itemid,day,month,year,{hcolumn}) VALUES ('{itemid}','{now.Day}','{now.Month}','{now.Year}','{value}')"; + mcc.Open(); + using (MySqlCommand myCmd = new MySqlCommand(cmd, mcc)) + { + myCmd.CommandType = CommandType.Text; + myCmd.ExecuteNonQuery(); + } + mcc.Close(); } } @@ -383,6 +409,8 @@ namespace MxWDB.Utilities myCmd.CommandType = CommandType.Text; myCmd.ExecuteNonQuery(); } + mcc.Close(); + JSON.AHDump.aucds.Clear(); } } @@ -414,6 +442,8 @@ namespace MxWDB.Utilities myCmd.CommandType = CommandType.Text; myCmd.ExecuteNonQuery(); } + mcc.Close(); + JSON.AHDump.aucdscom.Clear(); } } @@ -492,7 +522,7 @@ namespace MxWDB.Utilities mc6.Close(); } - public static void UpdateTokenValue(int v) + public static void UpdateTokenValue(Int64 v) { string stm2 = string.Format($"DELETE FROM mxw_token WHERE region='{Settings.GetRegion()}'"); MySqlConnection mc2 = ItemDB(); @@ -510,6 +540,7 @@ namespace MxWDB.Utilities myCmd.CommandType = CommandType.Text; myCmd.ExecuteNonQuery(); } + mcc.Close(); } } } diff --git a/MxWDB/Utilities/OAuth.cs b/MxWDB/Utilities/OAuth.cs index c16e2fa..bb3ca67 100644 --- a/MxWDB/Utilities/OAuth.cs +++ b/MxWDB/Utilities/OAuth.cs @@ -1,4 +1,5 @@ using Google.Protobuf.WellKnownTypes; +using MySqlX.XDevAPI; using Newtonsoft.Json; using Org.BouncyCastle.Asn1.Cmp; using Org.BouncyCastle.Asn1.Crmf; @@ -7,19 +8,30 @@ using RestSharp; using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Linq; using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading.Tasks; -using System.IO; namespace MxWDB.Utilities { class OAuth { - public class OAuthRoot + public class BlizzardToken { - public string access_token { get; set; } + [JsonPropertyName("access_token")] + public string AccessToken { get; set; } = string.Empty; + + [JsonPropertyName("token_type")] + public string TokenType { get; set; } = string.Empty; + + [JsonPropertyName("expires_in")] + public int ExpiresIn { get; set; } } /// @@ -28,25 +40,25 @@ namespace MxWDB.Utilities /// Your registered application Client ID. /// Your registered application Secret ID. /// - public static string GetAccessToken(string key, string secret) + public static async Task GetAccessToken(string key, string secret) { - ServicePointManager.SecurityProtocol |= SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; + - RestClient client = new RestClient($"https://{JSON.Settings.GetRegion()}.battle.net/oauth/token"); - var request = new RestRequest - { - Method = RestSharp.Method.Post - }; - var by = Encoding.UTF8.GetBytes($"{key}:{secret}"); - var b64 = Convert.ToBase64String(by); - request.AddParameter("grant_type", "client_credentials"); - request.AddHeader("Authorization", $"Basic {b64}"); - request.AddHeader("Content-type", "application/x-www-form-urlencoded"); - RestResponse response = client.Execute(request); + var client = new HttpClient(); + var authHeader = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes($"{key}:{secret}")); - var tokenResponse = JsonConvert.DeserializeObject(response.Content); + var request = new HttpRequestMessage(HttpMethod.Post, "https://oauth.battle.net/token"); + request.Headers.Authorization = new AuthenticationHeaderValue("Basic", authHeader); + request.Content = new FormUrlEncodedContent(new[] {new KeyValuePair("grant_type", "client_credentials")}); - return tokenResponse.access_token; + var response = await client.SendAsync(request); + + // Read the body as a string + string jsonContent = await response.Content.ReadAsStringAsync(); + + var doc = JsonDocument.Parse(jsonContent); + var tokenData = System.Text.Json.JsonSerializer.Deserialize(doc); + return doc.RootElement.GetProperty("access_token").GetString(); } } } diff --git a/MxWDB/packages.config b/MxWDB/packages.config index 89181f5..e9953d8 100644 --- a/MxWDB/packages.config +++ b/MxWDB/packages.config @@ -7,9 +7,9 @@ - + - +