Пример #1
0
        /// <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 RakutenRecipeIngredient(this);

            ingredient.Id.Value         = id;
            ingredient.Name.Value       = name;
            ingredient.AmountText.Value = amount;
            return(ingredient);
        }
Пример #2
0
        /// <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("#topicPath [itemprop=title] strong").InnerText.Trim();
                using (var stream = await this._httpClient.GetStreamAsync(new Uri(htmlDoc.QuerySelector("#detailContents .rcpPhotoBox img").Attributes["src"].Value)))
                    using (var ms = new MemoryStream()) {
                        stream.CopyTo(ms);
                        this.Photo.Value = ms.ToArray();
                    }
                this.Description.Value = htmlDoc.QuerySelector("#detailContents .ownerCom .summary").InnerText.Trim();

                this.Author.Value = new RakutenRecipeAuthor {
                    Name = HttpUtility.HtmlDecode(htmlDoc.QuerySelector("#recipe_owner_mypage_link").InnerText).Trim(),
                    Url  = new Uri(this.Url.Value, htmlDoc.QuerySelector("#recipe_owner_mypage_link").Attributes["href"].Value).AbsoluteUri
                };

                this.Yield.Value = htmlDoc.QuerySelector("#detailContents .materialBox [itemprop=recipeYield]")?.InnerText.Trim();

                this.Ingredients.Clear();
                var ingredients = new List <RakutenRecipeIngredient>();
                foreach (var ingredient in htmlDoc.QuerySelectorAll("#detailContents .materialBox li[itemprop=ingredients]"))
                {
                    var item = new RakutenRecipeIngredient(this);
                    item.Id.Value         = ingredients.Count + 1;             // 自動採番
                    item.Name.Value       = ingredient.QuerySelector(".name").InnerText.Trim();
                    item.AmountText.Value = ingredient.QuerySelector(".amount").InnerText;

                    ingredients.Add(item);
                }
                this.Ingredients.AddRange(ingredients);
                this.Steps.Clear();
                var steps = htmlDoc
                            .QuerySelectorAll("#detailContents .stepBox")
                            .Select(async(x, index) => {
                    var RakutenRecipeStep          = new RakutenRecipeStep(this.Settings, this.Logger);
                    RakutenRecipeStep.Number.Value = index + 1;
                    var photoUrl = x.QuerySelector(".stepPhoto img")?.Attributes["src"].Value;
                    if (photoUrl != null)
                    {
                        using (var stream = await this._httpClient.GetStreamAsync(new Uri(photoUrl)))
                            using (var ms = new MemoryStream()) {
                                stream.CopyTo(ms);
                                RakutenRecipeStep.Photo.Value = ms.ToArray();
                            }
                    }
                    RakutenRecipeStep.StepText.Value = x.QuerySelector(".stepMemo").InnerText.Trim();

                    return(RakutenRecipeStep);
                });
                foreach (var step in steps)
                {
                    this.Steps.Add(await step);
                }
                this.History.Value         = htmlDoc.QuerySelector("#detailContents .howtoPointBox:first-child p")?.InnerText.Trim();
                this.Advice.Value          = htmlDoc.QuerySelector("#detailContents .howtoPointBox:last-child p")?.InnerText.Trim();
                this.RakutenRecipeId.Value = Regex.Replace(HttpUtility.HtmlDecode(htmlDoc.QuerySelector("#detailContents .rcpId").InnerText.Trim()), "^.+:.", "");
                this.PublishedDate.Value   = htmlDoc.QuerySelector("#publish_day_itemprop").InnerText.Trim();
            } catch (Exception ex) {
                this._failedNotification.OnNext((this, Behavior.Download, ex));
                return;
            }

            this._completedNotification.OnNext((this, Behavior.Download));
        }