/// <summary> /// レシピ材料インスタンス作成 /// </summary> /// <param name="name">材料名</param> /// <param name="amount">分量</param> /// <returns>レシピ材料インスタンス</returns> public override IRecipeIngredient CreateIngredientInstance(int id, string name, string amount) { var ingredient = new CookienRecipeIngredient(this); ingredient.Id.Value = id; ingredient.Name.Value = name; ingredient.AmountText.Value = amount; return(ingredient); }
/// <summary> /// レシピダウンロード /// </summary> /// <returns></returns> public override async Task DownloadRecipeAsync() { try { var htmlString = await this._httpClient.GetStringAsync(this.Url.Value); var htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(htmlString); this.Title.Value = htmlDoc.QuerySelector("#content header h1").InnerText.Trim(); using (var stream = await this._httpClient.GetStreamAsync(new Uri(htmlDoc.QuerySelector("#content img[itemprop=image]").Attributes["src"].Value))) using (var ms = new MemoryStream()) { stream.CopyTo(ms); this.Photo.Value = ms.ToArray(); } this.Description.Value = htmlDoc.QuerySelector("#content span[itemprop=description]").InnerText.Trim(); this.Ingredients.Clear(); var ingredients = new List <CookienRecipeIngredient>(); foreach (var ingredient in htmlDoc.QuerySelectorAll("#r_contents p")) { var item = new CookienRecipeIngredient(this); item.Id.Value = ingredients.Count + 1; // 自動採番 item.AmountText.Value = ingredient.QuerySelector("span").InnerText.Trim(); ingredient.RemoveChild(ingredient.QuerySelector("span")); item.Name.Value = ingredient.InnerText; ingredients.Add(item); } this.Ingredients.AddRange(ingredients); this.Steps.Clear(); var steps = htmlDoc .QuerySelectorAll("#ins_contents > div") .Select((x, index) => { var step = new CookienRecipeStep(this.Settings, this.Logger); step.Number.Value = index + 1; step.StepText.Value = x.QuerySelector("p.ins_des").InnerText.Trim(); return(step); }); foreach (var step in steps) { this.Steps.Add(step); } this.ShelfLife.Value = new TimeSpan(int.Parse(htmlDoc.QuerySelector("#content .recipe_info .recipe_info_right span.recipe_info_bold").InnerText.Trim()), 0, 0, 0); var memoIndex = 1; this.CookienMemos.Clear(); while (true) { var memo = htmlDoc.QuerySelector($"#memo{memoIndex}"); if (memo == null) { break; } this.CookienMemos.Add(new CookienMemo(memoIndex, memo.InnerText.Trim(), memo.NextSiblingElement().InnerText.Trim())); memoIndex++; } this.Yield.Value = Regex.Replace(htmlDoc.QuerySelector("#r_contents h1").InnerText.Trim(), "^.*?((.*?))$", "$1"); } catch (Exception ex) { this._failedNotification.OnNext((this, Behavior.Download, ex)); return; } this._completedNotification.OnNext((this, Behavior.Download)); }