示例#1
0
 public Snappfood(SnappfoodDTO dto)
 {
     this._id            = ObjectId.GenerateNewId(DateTime.Now);
     this.Url            = dto.Url;
     this.Tagger         = dto.Tagger;
     this.TagDate        = dto.TagDate;
     this.CreateDateTime = dto.CreateDateTime;
     this.isTagged       = dto.isTagged;
     this.status         = dto.status;
     this.Reserve        = dto.Reserve;
     this.pageSize       = dto.data.pageSize;
     this.count          = dto.data.count;
     this.Reviews        = new List <SnappReview>();
     if (dto.data.comments != null && dto.data.comments.Count() > 0)
     {
         foreach (var item in dto.data.comments.Where(r => !string.IsNullOrEmpty(r.commentText)))
         {
             Reviews.Add(new SnappReview(item));
         }
     }
 }
示例#2
0
        public async Task <T> GetProduct <T>(string url)
        {
            try
            {
                #region GetProduct
                SnappfoodDTO product = new SnappfoodDTO();
                string       id      = getSnappfoodIdByUrl(url);
                string       tmp_url = getSnappfoodCommentLink(id, 0);
                using (IHttpClientHelper clientHelper = new RestSharpHelper())
                {
                    var resultClient = clientHelper.GetHttp(tmp_url, true, user_agent);
                    product = JsonConvert.DeserializeObject <SnappfoodDTO>(resultClient.Content);
                    double pageCount = 0;
                    pageCount = Math.Round((double)(product.data.count / product.data.pageSize));
                    if (product.data.comments != null && product.data.comments.Any() && pageCount > 0)
                    {
                        for (int i = 1; i <= pageCount; i++)
                        {
                            try
                            {
                                System.Threading.Thread.Sleep(25);
                                string url1          = getSnappfoodCommentLink(id, i);
                                var    resultClient1 = clientHelper.GetHttp(url1, true, user_agent);
                                if (!resultClient1.Success)
                                {
                                    System.Threading.Thread.Sleep(500);
                                    resultClient1 = clientHelper.GetHttp(url1, true, user_agent);
                                }
                                var comments1 = JsonConvert.DeserializeObject <SnappfoodDTO>(resultClient1.Content);
                                comments1.data.comments.ForEach(x => x.replies = null);
                                comments1.data.comments.ForEach(x => x.foods   = new List <Food>());
                                if (comments1 != null && comments1.data.comments != null & comments1.data.comments.Any())
                                {
                                    product.data.comments.AddRange(comments1.data.comments);
                                }
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                    //var cm = product.data.comments.Select(x => x.commentText.Replace("\n", ", ")).ToArray();
                    //var cmss = string.Join("\n", cm);
                    product.Url            = url;
                    product.CreateDateTime = DateTime.Now;
                    product._id            = ObjectId.GenerateNewId(DateTime.Now).ToString();
                    product.Reserve        = false;
                    product.status         = true;
                    product.isTagged       = false;
                    product.Tagger         = "_";
                    product.TagDate        = DateTime.Now.AddYears(-10);
                }
                #endregion

                return((T)Convert.ChangeType(product, typeof(SnappfoodDTO)));
            }
            catch (Exception ex)
            {
                LogDTO log = new LogDTO()
                {
                    _id         = ObjectId.GenerateNewId().ToString(),
                    DateTime    = DateTime.Now,
                    Description = ex.Message.ToString(),
                    ProjectId   = (int)ProjectNames.Services,
                    Url         = url,
                    MethodName  = "Snappfood - Snappfood Helper - GetProduct",
                    Title       = "GetProduct"
                };
                Logger.AddLog(log);
                return((T)Convert.ChangeType(null, typeof(Snappfood)));
            }
        }