Ejemplo n.º 1
0
        public List <Review> GetAllReviews1()
        {
            using (DigikalaMongoDBRepository db = new DigikalaMongoDBRepository())
            {
                string xml = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>" + "\n";
                xml += "<Reviews>\n";

                var reviews = db.GetAllReviewsLabel();
                foreach (var review in reviews)
                {
                    xml += $"    <Review rid=\"{review.rid}\">\n";
                    xml += "        <sentences>\n";
                    for (int i = 0; i < review.sentences.Count; i++)
                    {
                        xml += $@"            <sentence id=""{review.rid}:{i}"">" + "\n";
                        xml += $"                <text>{review.sentences[i].Text}</text>\n";
                        if (review.sentences[i].Opinions.Any())
                        {
                            xml += "                <Opinions>\n";
                            foreach (var op in review.sentences[i].Opinions)
                            {
                                xml += @$ "                    <Opinion target=" "{op.category}" " category=" "{op.category}#{op.aspect}" " polarity=" "{op.polarity}" " />" + "\n";
                            }
                            xml += "                </Opinions>\n";
                        }
                        xml += $"            </sentence>\n";
                    }
                    xml += "        </sentences>\n";
                    xml += "    </Review>\n";
                }
                xml += "</Reviews>";
                File.WriteAllText(@"C:\Users\Administrator\Desktop\1.xml", xml);
                return(reviews);
            }
        }
Ejemplo n.º 2
0
 public List <sentence> GetTopSentences()
 {
     using (DigikalaMongoDBRepository db = new DigikalaMongoDBRepository())
     {
         return(db.GetTopSentences());
     }
 }
Ejemplo n.º 3
0
 public bool AddReviewToDB_NewMethod(AddReviewToDBParam param)
 {
     try
     {
         using (DigikalaMongoDBRepository db = new DigikalaMongoDBRepository())
         {
             Review review = new Review(param.review);
             db.AddReviewNew(review);
             if (param.AutoOff)
             {
                 db.SetTaggedProduct(param.id, param.tagger);
             }
         }
         return(true);
     }
     catch (Exception ex)
     {
         LogDTO log = new LogDTO()
         {
             _id         = ObjectId.GenerateNewId(DateTime.Now).ToString(),
             DateTime    = DateTime.Now,
             Description = ex.Message,
             ProjectId   = (int)ProjectNames.Services,
             Url         = "DKP: " + param.review.ProductID,
             MethodName  = "AddReviewToDB_NewMethod",
             Title       = $"AddReviewToDB_NewMethod Error, Tagger: {param.tagger}"
         };
         Logger.AddLog(log);
         return(false);
     }
 }
Ejemplo n.º 4
0
 public void CrawledProducts(string[] ids)
 {
     using (DigikalaMongoDBRepository db = new DigikalaMongoDBRepository())
     {
         db.CrwaledProducts(ids);
     }
 }
Ejemplo n.º 5
0
        public void AddBasePages(List <B5_Url> dtos)
        {
            var pageBases = new List <DigikalaPageBaseDTO>();

            foreach (var dto in dtos)
            {
                try
                {
                    pageBases.Add(new DigikalaPageBaseDTO(dto));
                }
                catch (Exception ex)
                {
                    LogDTO log = new LogDTO()
                    {
                        DateTime    = DateTime.Now,
                        Description = "Error Convert Model, Error= " + ex.ToString(),
                        Title       = "Convert To Standard DTO, Digikala",
                        MethodName  = "AddBasePages",
                        ProjectId   = 1,
                        Url         = dto.loc
                    };
                    Logger.AddLog(log);
                }
            }
            dtos.Clear();
            using (var digi = new DigikalaMongoDBRepository())
            {
                digi.AddDigikalaBasePages(pageBases);
            }
            pageBases.Clear();
        }
Ejemplo n.º 6
0
 public List <Review> GetLabelReviews()
 {
     using (DigikalaMongoDBRepository db = new DigikalaMongoDBRepository())
     {
         var allReviews = db.GetAllReviewsLabel();
         return(allReviews);
     }
 }
Ejemplo n.º 7
0
 public async Task <T> GetAllReviewObjects <T>(string cat)
 {
     using (DigikalaMongoDBRepository db = new DigikalaMongoDBRepository())
     {
         var ss = db.GetAllReviews(cat);
         return((T)Convert.ChangeType(ss, typeof(List <DigikalaProduct>)));
     }
 }
Ejemplo n.º 8
0
 public async Task <T> GetAllReviews <T>()
 {
     using (DigikalaMongoDBRepository db = new DigikalaMongoDBRepository())
     {
         var ss = db.GetAllReviews();
         return((T)Convert.ChangeType(ss, typeof(string[])));
     }
 }
Ejemplo n.º 9
0
 public async Task <T> GetFirstProductByCategory <T>(GetFirstProductByCategoryParam param)
 {
     using (DigikalaMongoDBRepository db = new DigikalaMongoDBRepository())
     {
         var result = db.GetFirstProductByCategory(param);
         return((T)Convert.ChangeType(result, typeof(DigikalaProductDTO)));
     }
 }
Ejemplo n.º 10
0
 public string GetSatatusReview()
 {
     using (DigikalaMongoDBRepository db = new DigikalaMongoDBRepository())
     {
         var product   = db.GetCountReview();
         var Sentences = db.GetCountSentences();
         return($"Products: {product} , Sentences: {Sentences}");
     }
 }
Ejemplo n.º 11
0
        public void AddProducts <T>(T dto)
        {
            AddProductsDigikala digikalaProducts = (AddProductsDigikala)Convert.ChangeType(dto, typeof(AddProductsDigikala));

            using (DigikalaMongoDBRepository db = new DigikalaMongoDBRepository())
            {
                db.AddDigikalaProducts(digikalaProducts.digikalaProducts.Select(x => ConvertProductDTOToEntity(x)).ToList());
            }
        }
Ejemplo n.º 12
0
        public void AddProduct <T>(T dto)
        {
            DigikalaProductDTO digikalaProduct = (DigikalaProductDTO)Convert.ChangeType(dto, typeof(DigikalaProductDTO));

            using (DigikalaMongoDBRepository db = new DigikalaMongoDBRepository())
            {
                db.AddDigikalaProduct(ConvertProductDTOToEntity(digikalaProduct));
            }
        }
Ejemplo n.º 13
0
        public async Task <T> GetAllBasePage <T>()
        {
            List <DigikalaPageBaseDTO> dtos = new List <DigikalaPageBaseDTO>();

            using (DigikalaMongoDBRepository db = new DigikalaMongoDBRepository())
            {
                dtos = db.GetAllBasePage();
            }
            GetAllBasePageDigikalaResult result = new GetAllBasePageDigikalaResult()
            {
                BasePages = dtos
            };

            return((T)Convert.ChangeType(result, typeof(GetAllBasePageDigikalaResult)));
        }
Ejemplo n.º 14
0
        public async Task <T> GetProduct <T>(string url)
        {
            try
            {
                string content = string.Empty;
                var    res     = client.GetHttp(url, true, user_agent);
                if (res.Success)
                {
                    content = res.Content;
                }
                if (res.HttpStatusCode == (int)HttpStatusCode.NotFound)
                {
                    using (DigikalaMongoDBRepository db = new DigikalaMongoDBRepository())
                    {
                        db.RemoveBasePage(url);
                    }
                }
                DigikalaProductDTO dto = new DigikalaProductDTO();

                if (string.IsNullOrEmpty(content))
                {
                    System.Threading.Thread.Sleep(200);
                    content = await GetPage1(url);
                }
                if (string.IsNullOrEmpty(content))
                {
                    System.Threading.Thread.Sleep(400);
                    content = await GetPage(url);
                }

                if (string.IsNullOrEmpty(content))
                {
                    return((T)Convert.ChangeType(null, typeof(DigikalaProductDTO)));
                }

                dto.Url = url;
                dto.DKP = getDKPWithUrl(url);

                var doc = new HtmlDocument();
                doc.LoadHtml(content);
                var body = doc.DocumentNode.SelectSingleNode("//body");
                var main = body.SelectSingleNode("//main");

                #region Container
                var divContainer = main.SelectSingleNode("//div[@id='content']//div[@class='o-page c-product-page']//div[@class='container']");
                var _cats        = divContainer.SelectNodes("//div[@class='c-product__nav-container']//nav//ul//li[@property='itemListElement']");
                dto.Categories = _cats.Any() && _cats.Count() > 0 ? _cats.Select(x => x.InnerText).ToList() : new List <string>();
                ////
                dto.Title = divContainer.SelectNodes("//div[@class='c-product__nav-container']//nav//ul//li//span[@property='name']").Last().InnerText;

                var article_info = divContainer.SelectSingleNode("//article//section[@class='c-product__info']");
                var title_fa_1   = article_info.SelectSingleNode("//div[@class='c-product__headline']//h1[@class='c-product__title']").ChildNodes["#Text"].InnerText.Replace("\n", "").Trim();
                var enTitle      = article_info.SelectSingleNode("//div[@class='c-product__headline']//h1[@class='c-product__title']//span[@class='c-product__title-en']");
                if (enTitle != null)
                {
                    dto.TitleEN = enTitle.InnerHtml.Replace("\n", "").Trim();
                }

                var product__guaranteed = article_info.SelectSingleNode("//div[@class='c-product__headline']//div[@class='c-product__guaranteed']//span");
                if (product__guaranteed != null)
                {
                    using (HtmlHelper html = new HtmlHelper())
                    {
                        dto.Guaranteed = short.Parse(html.NumberEN(product__guaranteed.InnerText.Replace("\n", "").Replace("  ", "").Trim().Replace("بیش از", "").Replace("نفر از خریداران این محصول را پیشنهاد داده‌اند", "").Trim()));
                    }
                }

                var    productWrapper     = article_info.SelectSingleNode("//div[@class='c-product__attributes js-product-attributes']//div[@class='c-product__config']//div[@class='c-product__config-wrapper']");
                string brandElementQuery1 = "//div[@class='c-product__directory']//ul//li//a[@class='btn-link-spoiler product-brand-title']";
                string brandElementQuery2 = "//div[@class='c-product__directory']//ul//li//span[@class='product-brand-title']";
                dto.Brand = productWrapper.SelectSingleNode(brandElementQuery1) != null?productWrapper.SelectSingleNode(brandElementQuery1).InnerText : (productWrapper.SelectSingleNode(brandElementQuery2) != null ? productWrapper.SelectSingleNode(brandElementQuery2).InnerText : "");

                dto.Category = productWrapper.SelectSingleNode("//div[@class='c-product__directory']//ul//li//a[@class='btn-link-spoiler']").InnerText;
                List <string> colors   = new List <string>();
                bool          isColors = productWrapper.SelectNodes("//div[@class='c-product__variants']") != null;
                if (isColors)
                {
                    colors.AddRange(productWrapper.SelectNodes("//div[@class='c-product__variants']//ul//li").Select(x => x.InnerText).ToList());
                    dto.Colors = colors;
                }

                if (productWrapper.SelectNodes("//div[@class='c-product__params js-is-expandable']//ul//li") != null)
                {
                    var feature_list = productWrapper.SelectNodes("//div[@class='c-product__params js-is-expandable']//ul//li").Select(x => new { name = x.FirstChild.InnerText.Replace(":", "").Trim(), val = x.LastChild.InnerText.Replace("\n", "").Trim() }).ToList();
                }

                var    c_box         = article_info.SelectSingleNode("//div[@class='c-product__attributes js-product-attributes']//div[@class='c-product__summary js-product-summary']//div[@class='c-box']");
                string priceOffQuery = "//div[@class='c-product__seller-info js-seller-info']" +
                                       "//div[@class='js-seller-info-changable c-product__seller-box']" +
                                       "//div[@class='c-product__seller-row c-product__seller-row--price']" +
                                       "//div[@class='c-product__seller-price-real']";
                string priceQuery = "//div[@class='c-product__seller-info js-seller-info']" +
                                    "//div[@class='js-seller-info-changable c-product__seller-box']" +
                                    "//div[@class='c-product__seller-row c-product__seller-row--price']" +
                                    "//div[@class='c-product__seller-price-prev js-rrp-price u-hidden']";
                var priceElement = article_info.SelectSingleNode(priceQuery);
                var isExistPrice = priceElement != null;
                if (isExistPrice && priceElement.InnerText.Trim() != "")
                {
                    using (HtmlHelper html = new HtmlHelper())
                    {
                        dto.Price = Int64.Parse(html.NumberEN(article_info.SelectSingleNode(priceQuery).InnerText.Replace("\n", "").Replace(",", "").Trim()));
                    }
                }
                else
                {
                    var priceOff = article_info.SelectSingleNode(priceOffQuery);
                    if (priceOff != null)
                    {
                        using (HtmlHelper html = new HtmlHelper())
                        {
                            dto.Price = Int64.Parse(html.NumberEN(priceOff.InnerText
                                                                  .Replace("\n", "").Replace("\r", "").Replace(",", "").Replace("تومان", "").Replace("  ", " ").Trim()));
                        }
                    }
                }
                #endregion

                #region Tabs
                string tabsQuery =
                    "//div[@id='tabs']" +
                    "//div[@class='c-box c-box--tabs p-tabs__content']" +
                    "//div[@class='c-params']" +
                    "//article" +
                    "//section";
                if (main.SelectNodes(tabsQuery) != null)
                {
                    var tabSections = main.SelectNodes(tabsQuery).ToArray();
                    List <ProductFeaturesDTO> features = new List <ProductFeaturesDTO>();
                    foreach (var feat in tabSections)
                    {
                        ProductFeaturesDTO p = new ProductFeaturesDTO();
                        p.Title = feat.ChildNodes[0].InnerText;
                        var _features = feat.ChildNodes[1].ChildNodes
                                        .Select(x => new string[]
                        {
                            GetFeatureKey(x.ChildNodes[0].InnerHtml),
                            x.ChildNodes[1].InnerText.Replace("\n", "").Replace("          ", " ").Replace("     ", " ").Replace("     ", " ").Replace("  ", " ").Replace("  ", " ").Trim(),
                        }).ToArray();
                        int        _lastFillNumber = 0;
                        int        _TotalCount     = _features.Count(x => x[0] != "");
                        string[][] _tempFeature    = new string[_features.Count()][];
                        for (int i = 0; i < _features.Length; i++)
                        {
                            string currentKey = _features[i][0];
                            if (!string.IsNullOrEmpty(currentKey) || i == 0)
                            {
                                _lastFillNumber = i;
                                _tempFeature[i] = new string[] { currentKey, _features[i][1] };
                            }
                            else
                            {
                                List <string> _featuePlus = _tempFeature[_lastFillNumber].ToList();
                                _featuePlus.Add(_features[i][1]);
                                _tempFeature[_lastFillNumber] = _featuePlus.ToArray();
                            }
                        }
                        p.Features = _tempFeature.Where(x => x != null).ToList();
                        features.Add(p);
                    }
                    dto.Features = features;
                }
                #endregion

                dto = commentConcat(dto, url);
                //var jjj = JsonConvert.SerializeObject(dto);
                return((T)Convert.ChangeType(dto, typeof(DigikalaProductDTO)));
            }
            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  = "Digikala - Digikala Helper - GetProduct",
                    Title       = "GetProduct"
                };
                Logger.AddLog(log);
                return((T)Convert.ChangeType(null, typeof(DigikalaProductDTO)));
            }
        }