private async Task <ImageSource> LoadOfficialAsync(string imageUrl, ImageSource defaultImage) { // Remove the query part, remove the host part, remove image/Art/2DItems/, trim slashes var relevantPart = Regex.Replace(imageUrl, @"(\?.*)|(.*(\.net|\.com)/)|(image/Art/2DItems/)", "").Trim('/'); var match = Regex.Match(relevantPart, @"gen/image/.*?/([a-zA-Z0-9]*)/Item\.png"); if (match.Success) { // These names are too long. // They contain groups of 20 chars (as folders) and end with a unique identifier. relevantPart = $"gen/{match.Groups[1]}.png"; } var fileName = string.Format(DownloadedPathFormat, relevantPart); if (!File.Exists(fileName)) { if (!_options.DownloadMissingItemImages) { return(defaultImage); } var imgData = await _httpClient.GetByteArrayAsync(imageUrl).ConfigureAwait(false); CreateDirectories(fileName); WikiApiUtils.SaveImage(imgData, fileName, false); Log.Info($"Downloaded item image {fileName} to the file system."); } return(await Task.Run(() => ImageSourceFromPath(fileName)).ConfigureAwait(false)); }
private async Task ReadJson(string wikiClass) { // for items that have the given class ... var conditions = new ConditionBuilder { { RdfItemClass, wikiClass } }; // ... retrieve name and the icon url var task = WikiApiAccessor.AskAndQueryImageInforUrls(conditions); var results = (await task).ToList(); // download the images from the urls and save them foreach (var result in results) { var data = await HttpClient.GetByteArrayAsync(result.Url); foreach (var name in result.Names) { var fileName = name + ".png"; WikiApiUtils.SaveImage(data, Path.Combine(SavePath, fileName), true); } } Log.Info($"Retrieved {results.Count} images for class {wikiClass}."); }
private async Task ReadJson(string wikiClass) { // for items that have the given class ... var where = $"{CargoConstants.ItemClass}='{wikiClass}'"; // ... retrieve name and the icon url var task = WikiApiAccessor.GetItemImageInfosAsync(where); var results = (await task).ToList(); // download the images from the urls and save them foreach (var result in results) { var data = await HttpClient.GetByteArrayAsync(result.Url); foreach (var name in result.Names) { var fileName = name + ".png"; WikiApiUtils.SaveImage(data, Path.Combine(SavePath, fileName), true); } } Log.Info($"Retrieved {results.Count} images for class {wikiClass}."); }
private async Task <ImageSource> LoadOfficialAsync(string imageUrl, Task <ImageSource> defaultImage) { // Remove the query part, remove the host part, remove image/Art/2DItems/, trim slashes var relevantPart = Regex.Replace(imageUrl, @"(\?.*)|(.*(\.net|\.com)/)|(image/Art/2DItems/)", "").Trim('/'); var match = Regex.Match(relevantPart, @"gen/image/.*?/([a-zA-Z0-9]*)/Item\.png"); var isRelic = imageUrl.Contains("&relic=1"); if (match.Success) { // These names are too long. // They contain groups of 20 chars (as folders) and end with a unique identifier. relevantPart = $"gen/{match.Groups[1]}.png"; } else if (isRelic) { // Non-generated images for relics and normal uniques have the same url, the relic flag is in the // query part. As the query is removed, the file name needs to be adjusted for relics. relevantPart = relevantPart.Replace(".png", "Relic.png"); } var fileName = string.Format(DownloadedPathFormat, relevantPart); if (!File.Exists(fileName)) { if (!_options.DownloadMissingItemImages) { return(await defaultImage.ConfigureAwait(false)); } var imgData = await _httpClient.GetByteArrayAsync(imageUrl).ConfigureAwait(false); CreateDirectories(fileName); WikiApiUtils.SaveImage(imgData, fileName, false); Log.Info($"Downloaded item image {fileName} to the file system."); } return(BitmapImageFactory.Create(fileName)); }