Ejemplo n.º 1
0
        private async Task CreateCategoryNamesMapping(ZlShortCategory categories,
                                                      CategoryNamesMapping categoryNamesMapping)
        {
            if (categories.Categories is null)
            {
                return;
            }

            if (categoryNamesMapping.CategoryNamesMappings is null)
            {
                categoryNamesMapping.CategoryNamesMappings = new List <CategoryNamesMapping>();
            }

            var symbolsRegex               = new Regex("[^A-Za-z-а-яА-Яіїє.,_/0-9%$&*()+=`~@!# \t\n]");
            var notSlugAlphabet            = new Regex("[^a-zA-Z0-9\\s\\-]");
            var spaceRegex                 = new Regex("\\s");
            var enRegex                    = new Regex("[^A-Za-z\\s]");
            var spaceCharacterAtStartOrEnd = new Regex("^\\s*|\\s*$");

            foreach (var cl in categories.Categories)
            {
                var cnm = new CategoryNamesMapping();
                cnm.Name = symbolsRegex.Replace(await _googleTranslationService.TranslateTextAsync(cl.Label, "uk", "pl"), "");
                cnm.Slug = spaceRegex
                           .Replace(spaceCharacterAtStartOrEnd
                                    .Replace(notSlugAlphabet
                                             .Replace(_transliterationService.TransliterateUaToEnUtf(cnm.Name), ""), ""), "-")
                           .ToLower();
                cnm.PlSlug = cl.UrlKey;
                cnm.PlName = cl.Label;
                categoryNamesMapping.CategoryNamesMappings.Add(cnm);
            }

            for (int i = 0; i < categories.Categories.Count(); i++)
            {
                await CreateCategoryNamesMapping(categories.Categories.ElementAt(i), categoryNamesMapping.CategoryNamesMappings.ElementAt(i));
            }
        }
Ejemplo n.º 2
0
        private async Task <(ProductRequest, List <ProductVariationRequest>)> CreateProductToInsert(Dictionary <ArticleInfo, ArticleInfo> articlesDict,
                                                                                                    IEnumerable <string> allowedAttributeCategories, IEnumerable <string> invalidAttributeNames, IEnumerable <IEnumerable <Product> > scrapyProducts,
                                                                                                    decimal zlotyRate, decimal markUp, string plZalandoUrl, int sizeAttributeId, int brandAttributeId)
        {
            try {
                var articlePair = articlesDict.First();

                var status = articlePair.Value == null ? "pending" : "publish";

                var isVariableProduct = articlesDict.Count() > 1 || articlePair.Key.Units.Count() > 1;

                var sku = isVariableProduct ? articlePair.Key.ModelId : articlePair.Key.Id;

                string name;
                if (articlePair.Value?.Name != null)
                {
                    name = await _googleTranslationService.TranslateTextAsync(articlePair.Value?.Name, "uk", "en");
                }
                else if (articlePair.Key?.Name != null)
                {
                    name = await _googleTranslationService.TranslateTextAsync(articlePair.Key?.Name, "uk", "pl");
                }
                else
                {
                    throw new ArgumentNullException(nameof(name));
                }

                var notSlugAlphabet            = new Regex("[^a-zA-Z0-9\\s]");
                var spaceCharacter             = new Regex("\\s");
                var spaceCharacterAtStartOrEnd = new Regex("^\\s*|\\s*$");

                var slug = (spaceCharacter
                            .Replace(spaceCharacterAtStartOrEnd
                                     .Replace(notSlugAlphabet
                                              .Replace(_transliteratioinService.TransliterateUaToEnUtf(name), ""), ""), "-") + "-" + sku)
                           .ToLower();
                if (string.IsNullOrEmpty(slug))
                {
                    throw new ArgumentNullException(nameof(slug));
                }

                var type = isVariableProduct ? "variable" : "simple";

                var categories = scrapyProducts
                                 .SelectMany(el => el.ToList())
                                 .FirstOrDefault(el => isVariableProduct ? string.Equals(el.ModelSku, sku) : string.Equals(el.Sku, sku))
                                 .ProductCategories
                                 .Select(el => new ProductCategoryRequest {
                    Id = el.Category.WoocommerceId
                })
                                 .Distinct()
                                 .ToList();

                string description;
                if (articlePair.Value == null)
                {
                    description = string.Join(Environment.NewLine, articlePair.Key.Attributes
                                              .Where(el => allowedAttributeCategories.Contains(el.Category))
                                              .Where(el => !el.Data.Any(x => invalidAttributeNames.Contains(x.Name)))
                                              .SelectMany(el => el.Data.Select(x => x.Title)));
                }
                else
                {
                    var descriptionList = articlePair.Value.Attributes
                                          .Where(el => allowedAttributeCategories.Contains(el.Category))
                                          .Where(el => !el.Data.Any(x => invalidAttributeNames.Contains(x.Name)))
                                          .SelectMany(el => el.Data.Select(x => (string)(x.Name + ": " + Convert.ToString(x.Values))))
                                          .Select(el => _googleTranslationService.TranslateTextAsync(el, "uk", "en"));

                    var translatedDescription = await Task.WhenAll(descriptionList);

                    description = string.Join(Environment.NewLine, translatedDescription);
                }

                var images = articlePair.Key.Media.Images
                             .Select(el => new ProductImageRequest
                {
                    Src = el.Sources.Color
                          .Split("?")
                          .FirstOrDefault(),
                    Name = "",
                    Alt  = ""
                });
                var validImages = new List <ProductImageRequest>();
                foreach (var img in images)
                {
                    try
                    {
                        using (var client = new HttpClient())
                        {
                            var response = await client.GetAsync(img.Src);

                            if (response.StatusCode == System.Net.HttpStatusCode.OK)
                            {
                                validImages.Add(img);
                            }
                        }
                    }
                    catch { }
                }

                var popularity = (int)Math.Ceiling(scrapyProducts
                                                   .SelectMany(el => el.ToList())
                                                   .FirstOrDefault(el => isVariableProduct ? string.Equals(el.ModelSku, sku) : string.Equals(el.Sku, sku))
                                                   .ProductCategories
                                                   .Average(el => el.CategoryIndex));

                var popularityStr       = popularity.ToString();
                var popularityStrLength = popularityStr.Length;
                for (int i = 0; i < 9 - popularityStrLength; i++)
                {
                    popularityStr = "0" + popularityStr;
                }


                if (!isVariableProduct)
                {
                    var unit = articlePair.Key.Units.First();

                    var price = decimal.Parse(unit.DisplayPrice.OriginalPrice.Value) * (1 + markUp) * zlotyRate;

                    var productToInsert = new ProductRequest
                    {
                        Name              = name,
                        Slug              = slug,
                        Sku               = sku,
                        Type              = type,
                        Status            = status,
                        Description       = description,
                        CatalogVisibility = "visible",
                        RegularPrice      = price.ToString(),
                        Categories        = categories,
                        Images            = new List <ProductImageRequest>(),
                        ManageStock       = true,
                        StockQuantity     = unit.Stock,
                        StockStatus       = unit.Stock > 0 ? "instock" : "outofstock",
                        Attributes        = new List <ProductAttributeRequest> {
                            new ProductAttributeRequest
                            {
                                Name    = "color",
                                Options = new List <string> {
                                    articlePair.Key.Color
                                },
                                Variation = false,
                                Visible   = true
                            },
                            new ProductAttributeRequest
                            {
                                Id      = sizeAttributeId,
                                Options = new List <string> {
                                    unit.Size.Local
                                },
                                Variation = false,
                                Visible   = true
                            },
                            new ProductAttributeRequest
                            {
                                Id      = brandAttributeId,
                                Options = new List <string> {
                                    articlePair.Key.Brand.Name
                                },
                                Variation = false,
                                Visible   = true
                            }
                        },
                        MetaData = new List <ProductMetaDataRequest>
                        {
                            new ProductMetaDataRequest
                            {
                                Key   = "zalando_url",
                                Value = $"{plZalandoUrl}/katalog?q={sku}"
                            },
                            new ProductMetaDataRequest
                            {
                                Key   = "original_price",
                                Value = price.ToString()
                            },
                            new ProductMetaDataRequest
                            {
                                Key   = "delivery_time",
                                Value = articlePair.Key.DeliveryTime
                            },
                            new ProductMetaDataRequest
                            {
                                Key   = "pl_name",
                                Value = articlePair.Key.Name
                            },
                            new ProductMetaDataRequest
                            {
                                Key   = "popularity",
                                Value = popularityStr
                            },
                            new ProductMetaDataRequest
                            {
                                Key   = "fifu_list_url",
                                Value = string.Join('|', validImages.Select(el => el.Src))
                            }
                        }
                    };

                    return(productToInsert, null);
                }

                var productRequest = new ProductRequest
                {
                    Name              = name,
                    Slug              = slug,
                    Sku               = sku,
                    Type              = type,
                    Status            = status,
                    Description       = description,
                    CatalogVisibility = "visible",
                    Categories        = categories,
                    Images            = new List <ProductImageRequest>(),
                    MetaData          = new List <ProductMetaDataRequest>
                    {
                        new ProductMetaDataRequest
                        {
                            Key   = "pl_name",
                            Value = articlePair.Key.Name
                        },
                        new ProductMetaDataRequest
                        {
                            Key   = "popularity",
                            Value = popularityStr
                        },
                        new ProductMetaDataRequest
                        {
                            Key   = "fifu_list_url",
                            Value = string.Join('|', validImages)
                        }
                    },
                    Attributes = new List <ProductAttributeRequest> {
                        new ProductAttributeRequest {
                            Name    = "color",
                            Options = articlesDict
                                      .Select(el => el.Key.Color)
                                      .ToList(),
                            Variation = true,
                            Visible   = true
                        },
                        new ProductAttributeRequest
                        {
                            Id        = sizeAttributeId,
                            Variation = true,
                            Visible   = true,
                            Options   = articlesDict.Keys
                                        .SelectMany(el => el.Units)
                                        .Select(el => el.Size.Local)
                                        .Distinct()
                                        .ToList()
                        },
                        new ProductAttributeRequest
                        {
                            Id        = brandAttributeId,
                            Variation = false,
                            Visible   = true,
                            Options   = new List <string> {
                                articlePair.Key.Brand.Name
                            }
                        }
                    },
                    DefaultAttributes = new List <ProductDefaultAttributeRequest>
                    {
                        new ProductDefaultAttributeRequest
                        {
                            Name   = "color",
                            Option = articlePair.Key.Color
                        },
                        new ProductDefaultAttributeRequest
                        {
                            Id     = sizeAttributeId,
                            Option = articlePair.Key.Units
                                     .FirstOrDefault()
                                     .Size
                                     .Local
                        }
                    }
                };

                var productVariationsRequest = new List <ProductVariationRequest>();
                foreach (var pair in articlesDict)
                {
                    foreach (var unit in pair.Key.Units)
                    {
                        var variationImage = "";
                        foreach (var image in pair.Key.Media.Images)
                        {
                            try
                            {
                                var link = image.Sources.Color.Split("?").FirstOrDefault();
                                using (var client = new HttpClient())
                                {
                                    var response = await client.GetAsync(link);

                                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                                    {
                                        variationImage = link;
                                        break;
                                    }
                                }
                            }
                            catch { }
                        }

                        productVariationsRequest.Add(new ProductVariationRequest
                        {
                            Sku           = unit.Id,
                            RegularPrice  = (decimal.Parse(unit.OriginalPrice.Value) * (markUp + 1) * zlotyRate).ToString(),
                            Status        = status,
                            ManageStock   = true,
                            StockQuantity = unit.Stock,
                            StockStatus   = unit.Stock > 0 ? "instock" : "outofstock",
                            Image         = new ProductVariationImageRequest(),
                            Attributes    = new List <ProductVariationAttributesRequest>
                            {
                                new ProductVariationAttributesRequest
                                {
                                    Name   = "color",
                                    Option = pair.Key.Color
                                },
                                new ProductVariationAttributesRequest
                                {
                                    Id     = sizeAttributeId,
                                    Option = unit.Size.Local
                                }
                            },
                            MetaData = new List <ProductVariationMetaDateRequest>
                            {
                                new ProductVariationMetaDateRequest
                                {
                                    Key   = "zalando_url",
                                    Value = $"{plZalandoUrl}/katalog?q={pair.Key.Id}"
                                },
                                new ProductVariationMetaDateRequest
                                {
                                    Key   = "original_price",
                                    Value = unit.OriginalPrice.Value
                                },
                                new ProductVariationMetaDateRequest
                                {
                                    Key   = "delivery_time",
                                    Value = articlePair.Key.DeliveryTime
                                },
                                new ProductVariationMetaDateRequest
                                {
                                    Key   = "fifu_list_url",
                                    Value = variationImage
                                }
                            }
                        });
                    }
                }

                return(productRequest, productVariationsRequest);
            }
            catch (Exception e)
            {
                Console.WriteLine($"impossible to create product: exeption = {e.ToString()}");
            }
            return(null, null);
        }