/// <summary> /// Returns a List of ints that represent Match Id's from a file in the matches Blob storage container /// </summary> /// <param name="filename">Name of file that contains a JSONified list of Ints</param> /// <returns>List of ints</returns> public List <int> LoadMatchsetFromFile(string filename) { try { var container = ApiTools.GetBlobContainer("matches"); var blob = container.GetBlockBlobReference(filename); if (!blob.Exists()) { return(new List <int>()); } var json = blob.DownloadText(); var toReturn = JsonConvert.DeserializeObject <int[]>(json); Logger.LogMessageToFile(GetType().Name, "Matchset loaded from file"); return(toReturn.ToList()); } catch (Exception e) { Logger.LogMessageToFile(GetType().Name, e.Message); return(new List <int>()); } }
/// <summary> /// Fetches the URL used to grab CDN assets from DataDragon /// </summary> /// <param name="region">Region of data</param> /// <returns>DataDragon object containing data on the current Version of DataDragon</returns> public static DataDragon GetCdnUrl(string region) { var cacheName = "cdn_" + region; var cached = HttpContext.Current.Cache[cacheName] as DataDragon; if (cached != null) { return(cached); } var client = ApiTools.GlobalApiClient(); var request = new RestRequest(UrlFormat, Method.GET); request.AddUrlSegment("region", region); request.AddUrlSegment("method", "realm"); request.AddApiKey(); var response = client.Execute <DataDragon>(request); HttpContext.Current.Cache[cacheName] = response.Data; return(response.Data); }
/// <summary> /// Returns a list of all Champions in the current Version /// </summary> /// <param name="region">Region of data</param> public static List <Champion> GetAllChampions(string region) { var cacheName = "allChamps_" + region; var cached = HttpContext.Current.Cache[cacheName] as List <Champion>; if (cached != null) { return(cached); } var client = ApiTools.GlobalApiClient(); var request = new RestRequest(UrlFormat, Method.GET); request.AddUrlSegment("region", region); request.AddUrlSegment("method", "champion"); request.AddParameter("dataById", true); request.AddApiKey(); var response = client.Execute(request); var response2 = client.Execute <dynamic>(request); var champs = ParseAllChampsResponse(region, response.Content); HttpContext.Current.Cache[cacheName] = champs; return(champs); }
/// <summary> /// Used to upload the Template file for ItemSets /// </summary> public static void UploadItemSetTemplate() { var container = ApiTools.GetBlobContainer("itemsets"); var blob = container.GetBlockBlobReference("ItemSetTemplate.txt"); using (var r = File.OpenRead(ConfigurationManager.AppSettings["ItemSetTemplatePath"])) { blob.UploadFromStream(r); } }
/// <summary> /// Grabs the details of a Match from the Riot API /// </summary> /// <param name="region">Region to fetch from</param> /// <param name="matchId">Id of the Match to fetch</param> /// <returns>MatchDetail object containing the details of the match</returns> public static MatchDetail GetMatch(string region, int matchId) { Logger.LogMessageToFile(MethodBase.GetCurrentMethod().DeclaringType.ToString(), "Loading Match " + matchId); // Load the match from our Cache if we can, as this is much faster and saves on API calls var cachedMatch = CacheService <MatchDetail> .GetFromCache("MatchCache", region, matchId); if (cachedMatch != null && cachedMatch.MatchId != 0) { cachedMatch.FromCache = true; return(cachedMatch); } var client = ApiTools.ApiClient(region); var request = new RestRequest(UrlFormat, Method.GET); request.AddUrlSegment("region", region); request.AddUrlSegment("matchid", matchId.ToString()); request.AddParameter("includeTimeline", true); request.AddApiKey(); var response = client.Execute <MatchDetail>(request); //Check to see if we are approaching rate limiting if (response.StatusCode == HttpStatusCode.ServiceUnavailable || response.StatusCode.ToString() == "429") { Logger.LogMessageToFile(MethodBase.GetCurrentMethod().DeclaringType.ToString(), "Too many calls, briefly pausing. Headers: " + String.Join(",", response.Headers)); Thread.Sleep(Convert.ToInt32(ConfigurationManager.AppSettings["msBetweenApiCalls"]) * 2); } var match = response.Data; match.FromCache = false; Logger.LogMessageToFile(MethodBase.GetCurrentMethod().DeclaringType.ToString(), "ResponseCode: " + response.StatusCode); if (match.MatchId == 0) { Logger.LogMessageToFile(MethodBase.GetCurrentMethod().DeclaringType.ToString(), "Warning: Did not correctly load Match " + matchId + " Response: " + response.StatusDescription); } else { CacheService <MatchDetail> .WriteToCache("MatchCache", region, matchId, match); } // Save match to file cache return(match); }
/// <summary> /// Returns all Blob items from the Cache /// </summary> /// <param name="cache">Name of Cache to load from</param> /// <returns></returns> public static List <T> GetAllFromCache(string cache) { var container = ApiTools.GetBlobContainer(cache); var allBlobs = container.ListBlobs(null, true); var toReturn = new List <T>(); foreach (var blob in allBlobs) { var temp = (CloudBlockBlob)blob; toReturn.Add(JsonConvert.DeserializeObject <T>(temp.DownloadText())); } return(toReturn); }
/// <summary> /// Returns all Blobs in a Cache that match based on a certain Prefix /// </summary> /// <param name="cache">Cache to load from</param> /// <param name="region">Region of Data</param> /// <param name="champId">ChampId of Data</param> /// <param name="role">Role of Data</param> /// <returns></returns> public static List <T> GetListFromCache(string cache, string region, int champId, string role) { var container = ApiTools.GetBlobContainer(cache); var allBlobs = container.ListBlobs(region + "_" + champId + "_" + role, true); var toReturn = new List <T>(); foreach (var blob in allBlobs) { var temp = (CloudBlockBlob)blob; toReturn.Add(JsonConvert.DeserializeObject <T>(temp.DownloadText())); } return(toReturn); }
/// <summary> /// Writes a Jsonified Object to a Cache /// </summary> /// <param name="cache">Name of Cache to write to</param> /// <param name="region">Region of data</param> /// <param name="id">ID of data</param> /// <param name="json">JSON to write</param> /// <returns></returns> public static bool WriteToCache(string cache, string region, string id, string json) { try { var container = ApiTools.GetBlobContainer(cache); var blob = container.GetBlockBlobReference(region + "_" + id + ".json"); blob.UploadText(json); return(true); } catch { return(false); } }
/// <summary> /// Gets an Itemset from the Azure Storage. /// Does not use CacheService due to custom naming schemes and performance optimizations. /// </summary> /// <param name="region">Region of data</param> /// <param name="champId">Id of champion</param> /// <param name="role">Role of champion</param> /// <returns></returns> public CloudBlockBlob GetItemsetBlob(string region, int champId, string role) { var container = ApiTools.GetBlobContainer("itemsets"); var filename = "LWI_" + StaticDataService.GetChampion(region, champId).Name + "_" + role + ".json"; var blob = container.GetBlockBlobReference(filename); if (!blob.Exists()) { var itemSet = GetItemset(region, champId, role); var json = itemSet.GenerateJson(); blob.UploadText(json); } return(blob); }
/// <summary> /// Scrapes the Id's of current featured Matches in a region. Used to keep statistics up to date. /// This method is called externally. /// </summary> /// <param name="region"></param> public static void ScrapeCurrentFeaturedGames(string region) { Logger.LogMessageToFile(MethodBase.GetCurrentMethod().DeclaringType.ToString(), "Scraping current featured games"); var client = ApiTools.ApiClient(region); var request = new RestRequest( "/observer-mode/rest/featured", Method.GET); request.AddApiKey(); var response = client.Execute <FeaturedGames>(request); //Check to see if we are approaching rate limiting if (response.StatusCode == HttpStatusCode.ServiceUnavailable || response.StatusCode.ToString() == "429") { Logger.LogMessageToFile(MethodBase.GetCurrentMethod().DeclaringType.ToString(), "Too many calls, briefly pausing. Headers: " + String.Join(",", response.Headers)); Thread.Sleep(Convert.ToInt32(ConfigurationManager.AppSettings["msBetweenApiCalls"]) * 2); } var container = ApiTools.GetBlobContainer("matches"); var blob = container.GetAppendBlobReference("matchIds.txt"); if (!blob.Exists()) { blob.CreateOrReplace(); } foreach (var game in response.Data.GameList) { // Only scrape Ranked games on Summoner's Rift if ((game.GameQueueConfigId == 4 || game.GameQueueConfigId == 42) && game.MapId == 11) { blob.AppendText(game.GameId.ToString() + "\n"); } } Logger.LogMessageToFile(MethodBase.GetCurrentMethod().DeclaringType.ToString(), String.Format("Scrape complete, {0} games scraped", response.Data.GameList.Count)); }
/// <summary> /// Loads an Champion from cache or from the API /// </summary> /// <param name="region">Region of data</param> /// <param name="championId">ID of champion</param> public static Champion GetChampion(string region, int championId) { // Two layer caching: File and CurrentCache. var cacheName = "champ_" + region + "_" + championId; var cached = HttpContext.Current.Cache[cacheName] as Champion; if (cached != null) { return(cached); } cached = CacheService <Champion> .GetFromCache("ChampionCache", region, championId); if (cached != null) { return(cached); } var client = ApiTools.ApiClient(region); var request = new RestRequest(UrlFormat + "/{championid}", Method.GET); request.AddUrlSegment("region", region); request.AddUrlSegment("method", "champion"); request.AddUrlSegment("championid", championId.ToString()); request.AddParameter("champData", "image"); request.AddApiKey(); var response = client.Execute <Champion>(request); // Write to both File and Current cache CacheService <Champion> .WriteToCache("ChampionCache", region, championId, response.Data); HttpContext.Current.Cache[cacheName] = response.Data; return(response.Data); }
/// <summary> /// Loads an Item from cache or from the API /// </summary> /// <param name="region">Region of data</param> /// <param name="itemId">ID of item</param> public static Item GetItem(string region, long itemId) { // Two layer caching: File and CurrentCache. var cacheName = "item_" + region + "_" + itemId; var cached = HttpContext.Current.Cache[cacheName] as Item; if (cached != null) { return(cached); } cached = CacheService <Item> .GetFromCache("ItemCache", region, (int)itemId); if (cached != null) { return(cached); } var client = ApiTools.ApiClient(region); var request = new RestRequest(UrlFormat + "/{itemid}", Method.GET); request.AddUrlSegment("region", region); request.AddUrlSegment("method", "item"); request.AddUrlSegment("itemid", itemId.ToString()); request.AddParameter("itemData", "consumed,from,gold,into,image"); request.AddApiKey(); var response = client.Execute <Item>(request); // Write to both File and Current cache CacheService <Item> .WriteToCache("ItemCache", region, (int)itemId, response.Data); HttpContext.Current.Cache[cacheName] = response.Data; return(response.Data); }
/// <summary> /// Logs a message to log.txt in the logs Azure Container /// </summary> /// <param name="callingClass">String indicating what class is logging the message</param> /// <param name="msg">Message to log</param> public static void LogMessageToFile(string callingClass, string msg) { try { var logLine = System.String.Format( "{0:G}: [{2}] - {1}.", System.DateTime.Now, msg, callingClass.Replace("LeagueStatisticallyBestItemset.", "")); var container = ApiTools.GetBlobContainer("logs"); var logBlob = container.GetAppendBlobReference("log.txt"); if (!logBlob.Exists()) { logBlob.CreateOrReplace(); } logBlob.AppendText(logLine + "\n"); } catch { // ignored } }
/// <summary> /// Returns an Object from the cache /// Overloaded method that allows custom Data names to be passed in /// </summary> /// <param name="cache">Name of Cache to load from</param> /// <param name="filename">Name of Data Blob</param> /// <returns></returns> public static T GetFromCache(string cache, string filename) { try { var container = ApiTools.GetBlobContainer(cache); var blob = container.GetBlockBlobReference(filename); if (!blob.Exists()) { return(default(T)); } var json = blob.DownloadText(); var toReturn = JsonConvert.DeserializeObject <T>(json); return(toReturn); } catch (Exception e) { Logger.LogMessageToFile("CacheService [" + cache + "]", e.Message); return(default(T)); } }