/// <summary> /// Download image by name. /// https://azurlane.koumakan.jp/Template: + id /// </summary> /// <param name="id">Image name (without "Template:" and without extension)</param> /// <returns></returns> public override async Task Download(string id) { Status = Statuses.InProgress; string imagePath = GetImageFolder(id) + "/" + id; if (File.Exists(imagePath)) { File.Delete(imagePath); } string tag = await GetIconsTag("Template:" + id); string url = GetIconsUrl(tag); if (!string.IsNullOrEmpty(url)) { using (CargoContext cargoContext = new CargoContext()) { Icon icon = new Icon { Name = id, FileName = await DownloadImageByUrl(url, id + ".png") }; if (await cargoContext.Icons.FindAsync(icon.Name) == null) { cargoContext.Icons.Add(icon); await cargoContext.SaveChangesAsync(); } } } Status = Statuses.DownloadComplete; }
public async Task CreateRelationshipGirlDrop(WhereToGetShipGirl wtg, ShipGirl shipGirl, string note, CargoContext cargoContext) { ShipGirlWhereToGetShipGirl mtm = new ShipGirlWhereToGetShipGirl { FK_ShipGirl = shipGirl, FK_WhereToGetShipGirl = wtg, Note = note }; if (cargoContext.ShipGirlWhereToGetShipGirl.Count(e => e.FK_ShipGirl.ShipID == mtm.FK_ShipGirl.ShipID && e.FK_WhereToGetShipGirl.Name == mtm.FK_WhereToGetShipGirl.Name) == 0) { cargoContext.ShipGirlWhereToGetShipGirl.Add(mtm); await cargoContext.SaveChangesAsync(); } }
/// <summary> /// Download all Skills and save them. /// </summary> public override async Task Download() { Status = Statuses.InProgress; StatusDataMessage = "Downloading data."; StatusImageMessage = "Pending."; List <SkillJsonWrapper> wrappedSkills; try { string responseJson = await GetData("ship_skills, ships", SkillFields, "ship_skills._pageName=ships._pageName", ""); wrappedSkills = JsonConvert.DeserializeObject <List <SkillJsonWrapper> >(responseJson); } catch (JsonException) { Status = Statuses.ErrorInDeserialization; Logger.Write($"Failed to desirialize skills.", this.GetType().ToString()); return; } catch { Status = Statuses.DownloadError; Logger.Write($"Failed to get data for skills from server.", this.GetType().ToString()); return; } TotalImageCount = wrappedSkills.Count; TotalDataCount = wrappedSkills.Count; using (CargoContext cargoContext = new CargoContext()) { StatusImageMessage = "Adding icons to download queue."; foreach (SkillJsonWrapper wrappedSkill in wrappedSkills) { downloadBlock.Post(wrappedSkill.Skill.Icon); } downloadBlock.Complete(); StatusImageMessage = "Downloading icons."; StatusDataMessage = "Saving data."; foreach (SkillJsonWrapper wrappedSkill in wrappedSkills) { if (await cargoContext.Skills .CountAsync(e => e.Name == wrappedSkill.Skill.Name) == 0) { wrappedSkill.Skill.FK_ShipGirl = await cargoContext.ShipGirls.FindAsync(wrappedSkill.Skill.ShipID); wrappedSkill.Skill.Detail = RemoveHTML(wrappedSkill.Skill.Detail); SavePaths(wrappedSkill.Skill); cargoContext.Skills.Add(wrappedSkill.Skill); } lock (locker) { CurrentDataCount++; } } await cargoContext.SaveChangesAsync(); } StatusDataMessage = "Complete."; downloadBlock.Completion.Wait(); Status = Statuses.DownloadComplete; }
/// <summary> /// Download all Skills of ShipGirl and update them or save if they doesn't exist. /// </summary> /// <param name="shipGirl">ShipGirl which skills need update.</param> /// <param name="cargoContext">DB context</param> public async Task Download(ShipGirl shipGirl, CargoContext cargoContext) { Status = Statuses.InProgress; List <SkillJsonWrapper> wrappedSkills; try { string responseJson = await GetData("ship_skills, ships", SkillFields, "ship_skills._pageName=ships._pageName", "ships.ShipID=\'" + shipGirl.ShipID + "\'"); wrappedSkills = JsonConvert.DeserializeObject <List <SkillJsonWrapper> >(responseJson); } catch (JsonException) { Status = Statuses.ErrorInDeserialization; Logger.Write($"Failed to desirialize skills for shipgirl. Shipirl ID: {shipGirl.ShipID}", this.GetType().ToString()); return; } catch { Status = Statuses.DownloadError; Logger.Write($"Failed to get skills data for shipgir from server. Shipgirl ID: {shipGirl.ShipID}", this.GetType().ToString()); return; } if (wrappedSkills.Count == 0) { Status = Statuses.EmptyResponse; return; } TotalImageCount = wrappedSkills.Count; foreach (Skill skill in shipGirl.Skills) { string imagePath = GetImageFolder(skill.Icon) + "/" + skill.Icon; if (File.Exists(imagePath)) { File.Delete(imagePath); } } cargoContext.Skills.RemoveRange(shipGirl.Skills); await cargoContext.SaveChangesAsync(); foreach (SkillJsonWrapper wrappedSkill in wrappedSkills) { downloadBlock.Post(wrappedSkill.Skill.Icon); } downloadBlock.Complete(); foreach (SkillJsonWrapper wrappedSkill in wrappedSkills) { wrappedSkill.Skill.FK_ShipGirl = shipGirl; wrappedSkill.Skill.Detail = RemoveHTML(wrappedSkill.Skill.Detail); SavePaths(wrappedSkill.Skill); if (await cargoContext.Skills .CountAsync(e => e.Name == wrappedSkill.Skill.Name) == 0) { cargoContext.Skills.Add(wrappedSkill.Skill); await cargoContext.SaveChangesAsync(); } else { await cargoContext.Update(wrappedSkill.Skill); } } downloadBlock.Completion.Wait(); Status = Statuses.DownloadComplete; }
/// <summary> /// Download one Skill by name and update it or save if it doesn't exist. /// </summary> /// <param name="id">Name of skill.</param>s public override async Task Download(string id) { Status = Statuses.InProgress; List <SkillJsonWrapper> wrappedSkills; try { string responseJson = await GetData("ship_skills, ships", SkillFields, "ship_skills._pageName=ships._pageName", "ship_skills.Name=\'" + id + "\'"); wrappedSkills = JsonConvert.DeserializeObject <List <SkillJsonWrapper> >(responseJson); } catch (JsonException) { Status = Statuses.ErrorInDeserialization; Logger.Write($"Failed to desirialize skill. ID: {id}", this.GetType().ToString()); return; } catch { Status = Statuses.DownloadError; Logger.Write($"Failed to get data for skill from server. ID: {id}", this.GetType().ToString()); return; } SkillJsonWrapper wrappedSkill = wrappedSkills.FirstOrDefault(); if (wrappedSkill == null) { Status = Statuses.EmptyResponse; return; } TotalImageCount = wrappedSkills.Count; using (CargoContext cargoContext = new CargoContext()) { downloadBlock.Post(wrappedSkill.Skill.Icon); downloadBlock.Complete(); wrappedSkill.Skill.FK_ShipGirl = await cargoContext.ShipGirls.FindAsync(wrappedSkill.Skill.ShipID); wrappedSkill.Skill.Detail = RemoveHTML(wrappedSkill.Skill.Detail); SavePaths(wrappedSkill.Skill); if (await cargoContext.Skills .CountAsync(e => e.Name == wrappedSkill.Skill.Name) == 0) { cargoContext.Skills.Add(wrappedSkill.Skill); await cargoContext.SaveChangesAsync(); } else { await cargoContext.Update(wrappedSkill.Skill); } } //downloadBlock.Completion.Wait(); Status = Statuses.DownloadComplete; }
/// <summary> /// Download ShipGirl's drop locations and update them or save if they doesn't exist. /// </summary> /// <param name="id">ShipGirl's id</param> public override async Task Download(string id) { Status = Statuses.InProgress; List <WTGShipGirlJsonWrapper> wrappedDrops; try { string responseJson = await GetData("shipDrops", DropFields, "shipDrops.ID=\'" + id + "\'"); wrappedDrops = JsonConvert.DeserializeObject <List <WTGShipGirlJsonWrapper> >(responseJson); } catch (JsonException) { Status = Statuses.ErrorInDeserialization; Logger.Write($"Failed to desirialize WTG for shipgril. Shipgirl ID: {id}", this.GetType().ToString()); return; } catch { Status = Statuses.DownloadError; Logger.Write($"Failed to get WTG data for shipgirl from server. Shipgirl ID: {id}", this.GetType().ToString()); return; } WTGShipGirlJsonWrapper wrappedDrop = wrappedDrops.FirstOrDefault(); if (wrappedDrop == null) { Status = Statuses.EmptyResponse; return; } // Adding location if didn't exists and creating connection between drop location and ship girl using (CargoContext cargoContext = new CargoContext()) { // get dropped ship girl ShipGirl shipGirl = await cargoContext.ShipGirls.FindAsync(wrappedDrop.WtgShipGirlJson.ID); if (shipGirl != null) { // Remove old connections var oldConnections = await cargoContext.ShipGirlWhereToGetShipGirl .Where(e => e.FK_ShipGirl.ShipID == shipGirl.ShipID).ToListAsync(); cargoContext.ShipGirlWhereToGetShipGirl.RemoveRange(oldConnections); await cargoContext.SaveChangesAsync(); // get all drops locations Dictionary <string, string> dictionary = wrappedDrop.WtgShipGirlJson.GetDrops(); if (dictionary.Count > 0) { foreach (string key in dictionary.Keys) { // get location WhereToGetShipGirl whereToGetShipGirl = await cargoContext.WhereToGetShipGirls.FindAsync(key); // check if it exists, if not create if (whereToGetShipGirl == null) { whereToGetShipGirl = cargoContext.WhereToGetShipGirls.Add(new WhereToGetShipGirl { Name = key }); cargoContext.SaveChanges(); } // create connection await CreateRelationshipGirlDrop(whereToGetShipGirl, shipGirl, dictionary[key], cargoContext); } } } } Status = Statuses.DownloadComplete; }
public IconDownloader(int threadsCount = 0) : base(threadsCount) { DownloadTitle = "Downloading Icons..."; if (!Directory.Exists(IconsFolderPath)) { Directory.CreateDirectory(IconsFolderPath); } async Task <string> UrlGetterFunction(string iconName) { string iconTag = await GetIconsTag(iconName); if (iconTag != "") { string iconUrl = GetIconsUrl(iconTag); if (iconUrl != null) { using (CargoContext cargoContext = new CargoContext()) { string name = iconUrl.Split('/').Last(); Icon icon = new Icon { Name = iconName.Replace("Template:", ""), FileName = GetImageFolder(name) + "/" + name }; if (await cargoContext.Icons.FindAsync(icon.Name) == null) { cargoContext.Icons.Add(icon); await cargoContext.SaveChangesAsync(); } } return(iconUrl); } } return(null); } async Task UrlDownloaderFunction(string iconUrl) { await DownloadImageByUrl(iconUrl, iconUrl?.Split('/').Last()); lock (locker) { CurrentImageCount++; } } urlGetterBlock = threadsCount <= 0 ? new TransformBlock <string, string>(UrlGetterFunction) : new TransformBlock <string, string>(UrlGetterFunction, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = threadsCount }); urlDownloaderBlock = threadsCount <= 0 ? new ActionBlock <string>(UrlDownloaderFunction) : new ActionBlock <string>(UrlDownloaderFunction, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = threadsCount }); urlGetterBlock.LinkTo(urlDownloaderBlock); urlGetterBlock.Completion.ContinueWith(delegate { urlDownloaderBlock.Complete(); }); }
/// <summary> /// Download all Equipment and save it. /// </summary> public override async Task Download() { Status = Statuses.InProgress; StatusDataMessage = "Downloading data."; StatusImageMessage = "Pending."; List <EquipmentJsonWrapper> wrappedEquipment; try { string responseJson = await GetData("equipment", EquipmentFields, ""); wrappedEquipment = JsonConvert.DeserializeObject <List <EquipmentJsonWrapper> >(responseJson); } catch (JsonException) { Status = Statuses.ErrorInDeserialization; Logger.Write($"Failed to deserialize equipment.", this.GetType().ToString()); return; } catch { Status = Statuses.DownloadError; Logger.Write($"Failed to get data for equipment from server.", this.GetType().ToString()); return; } TotalDataCount = wrappedEquipment.Count; TotalImageCount = wrappedEquipment.Count; using (CargoContext cargoContext = new CargoContext()) { StatusImageMessage = "Adding images to download queue."; // Prevent loading duplicate images. HashSet <string> loadingImages = new HashSet <string>(); foreach (EquipmentJsonWrapper wrpEquipment in wrappedEquipment) { string loadingImage = wrpEquipment.Equipment.Image; if (loadingImages.Add(loadingImage)) { downloadBlock.Post(loadingImage); } else { lock (locker) { CurrentImageCount++; } } } downloadBlock.Complete(); StatusImageMessage = "Downloading images."; StatusDataMessage = "Saving data."; foreach (EquipmentJsonWrapper wrpEquipment in wrappedEquipment) { if (await cargoContext.ShipGirlsEquipment .CountAsync(e => e.Name == wrpEquipment.Equipment.Name && e.Stars == wrpEquipment.Equipment.Stars) == 0) { Nationality nationality = cargoContext.Nationalities.Find(wrpEquipment.Equipment.Nationality); if (nationality == null) { nationality = new Nationality { Name = wrpEquipment.Equipment.Nationality, FK_Icon = cargoContext.Icons.Find(wrpEquipment.Equipment.Nationality) }; cargoContext.Nationalities.Add(nationality); cargoContext.SaveChanges(); } wrpEquipment.Equipment.FK_Nationality = nationality; CreateRelationships(wrpEquipment.Equipment, cargoContext); SavePaths(wrpEquipment.Equipment); wrpEquipment.Equipment.DropLocation = Refactor(wrpEquipment.Equipment.DropLocation); wrpEquipment.Equipment.Notes = Refactor(wrpEquipment.Equipment.Notes); wrpEquipment.Equipment.Name = Refactor(wrpEquipment.Equipment.Name); cargoContext.ShipGirlsEquipment.Add(wrpEquipment.Equipment); } lock (locker) { CurrentDataCount++; } } await cargoContext.SaveChangesAsync(); } StatusDataMessage = "Complete."; downloadBlock.Completion.Wait(); // downloadBlock.Completion.ContinueWith(e => { Status = Statuses.DownloadComplete; }); Status = Statuses.DownloadComplete; }
/// <summary> /// Download one piece of Equipment by name and update it or save if it doesn't exist. /// </summary> /// <param name="id">Name of equipment.</param> public override async Task Download(string id) { Status = Statuses.InProgress; List <EquipmentJsonWrapper> wrappedEquipment; try { string responseJson = await GetData("equipment", EquipmentFields, "equipment.Name=\'" + id + "\'"); wrappedEquipment = JsonConvert.DeserializeObject <List <EquipmentJsonWrapper> >(responseJson); } catch (JsonException) { Status = Statuses.ErrorInDeserialization; Logger.Write($"Failed to desirialize equipment. ID: {id}", this.GetType().ToString()); return; } catch { Status = Statuses.DownloadError; Logger.Write($"Failed to get data for equipment from server. ID: {id}", this.GetType().ToString()); return; } EquipmentJsonWrapper wrpEquipment = wrappedEquipment.FirstOrDefault(); if (wrpEquipment == null) { Status = Statuses.EmptyResponse; return; } TotalImageCount = wrappedEquipment.Count; using (CargoContext cargoContext = new CargoContext()) { downloadBlock.Post(wrpEquipment.Equipment.Image); downloadBlock.Complete(); Nationality nationality = cargoContext.Nationalities.Find(wrpEquipment.Equipment.Nationality); if (nationality == null) { nationality = new Nationality { Name = wrpEquipment.Equipment.Nationality, FK_Icon = cargoContext.Icons.Find(wrpEquipment.Equipment.Nationality) }; cargoContext.Nationalities.Add(nationality); cargoContext.SaveChanges(); } wrpEquipment.Equipment.FK_Nationality = nationality; CreateRelationships(wrpEquipment.Equipment, cargoContext); wrpEquipment.Equipment.DropLocation = Refactor(wrpEquipment.Equipment.DropLocation); wrpEquipment.Equipment.Notes = Refactor(wrpEquipment.Equipment.Notes); wrpEquipment.Equipment.Name = Refactor(wrpEquipment.Equipment.Name); SavePaths(wrpEquipment.Equipment); if (await cargoContext.ShipGirlsEquipment .CountAsync(e => e.Name == wrpEquipment.Equipment.Name) == 0) { cargoContext.ShipGirlsEquipment.Add(wrpEquipment.Equipment); await cargoContext.SaveChangesAsync(); } else { await cargoContext.Update(wrpEquipment.Equipment); } } //downloadBlock.Completion.Wait(); Status = Statuses.DownloadComplete; }