Example #1
0
        private static bool TryParseRss20Item(XElement itemElement, ExtensionManifestDirectory extensionManifestDirectory, out Rss20Item parsedItem)
        {
            parsedItem = default;

            if (itemElement == null)
            {
                return(false);
            }

            parsedItem             = new Rss20Item();
            parsedItem.Title       = itemElement.Element("title")?.Value.Trim();
            parsedItem.Link        = itemElement.Element("link")?.Value.Trim();
            parsedItem.Description = itemElement.Element("description")?.Value.Trim();
            parsedItem.Author      = itemElement.Element("author")?.Value.Trim();
            parsedItem.Comments    = itemElement.Element("comments")?.Value.Trim();

            foreach (var categoryElement in itemElement.Elements("category"))
            {
                if (TryParseRss20Category(categoryElement, out var parsedCategory))
                {
                    parsedItem.Categories.Add(parsedCategory);
                }
            }

            foreach (var enclosureElement in itemElement.Elements("enclosure"))
            {
                if (TryParseRss20Enclosure(enclosureElement, out var parsedEnclosure))
                {
                    parsedItem.Enclosures.Add(parsedEnclosure);
                }
            }

            if (TryParseRss20Timestamp(itemElement.Element("pubDate"), out var parsedPubDate))
            {
                parsedItem.PubDate = parsedPubDate;
            }

            if (TryParseRss20Source(itemElement.Element("source"), out var parsedSource))
            {
                parsedItem.Source = parsedSource;
            }

            if (TryParseRss20Guid(itemElement.Element("guid"), out var parsedGuid))
            {
                parsedItem.Guid = parsedGuid;
            }

            // extensions
            ExtensibleEntityParser.ParseXElementExtensions(itemElement, extensionManifestDirectory, parsedItem);

            return(true);
        }
Example #2
0
        protected Dictionary <int, Rss20Item> CreateProductRssItems(ProductVariant productVariant, IList <RssFeedSettingLocale> locales)
        {
            decimal price, salePrice;
            string  imageUrl = null;
            var     product  = productVariant.Product;
            var     dic      = new Dictionary <int, Rss20Item>();


            var categories = _categoryService.GetProductCategoriesByProductId(product.Id).Select(x => x.Category).ToList();

            CalculateProductPrices(productVariant, StoreCurrency, out price, out salePrice);
            var brand   = product.ProductManufacturers.OrderBy(x => x.DisplayOrder).FirstOrDefault();
            var picture = product.GetDefaultProductPicture(_pictureService);

            if (picture != null)
            {
                imageUrl = _pictureService.GetPictureUrl(picture, _rssFeedSettings.ImageSize).Replace("https://", "http://");
            }

            foreach (var L in locales)
            {
                var lang = _languageService.GetLanguageById(L.LanguageId);;
                var item = new Rss20Item()
                {
                    ProductId      = product.Id,
                    Title          = product.GetLocalized(x => x.Name, lang.Id),
                    Description    = product.GetLocalized(x => x.FullDescription, lang.Id),
                    Price          = FormatPrice(price, StoreCurrency),
                    SalePrice      = FormatPrice(salePrice, StoreCurrency),
                    Link           = GetProductUrl(productVariant, lang, L.Link),
                    MPN            = productVariant.Sku,
                    Gtin           = productVariant.Sku,
                    Condition      = "new",
                    Image          = imageUrl,
                    Availability   = productVariant.StockQuantity > 0 ? "in stock" : "out of stock",
                    ExpirationDate = DateTime.Now.AddDays(29).ToString("yyyy-MM-dd"),
                    CallForPrice   = productVariant.CallForPrice,
                };

                item.ProductType.AddRange(GetCategorieBreadCrumbs(categories, lang));

                if (!string.IsNullOrEmpty(item.Description))
                {
                    item.Description = RssFeedHelper.HtmlToPlainText(item.Description);
                }

                if (brand != null)
                {
                    item.Brand = brand.Manufacturer.GetLocalized(x => x.Name, lang.Id);
                }

                dic[L.LanguageId] = item;
            }

            //CleanInvalidXmlChars(item.Title);
            //CleanInvalidXmlChars(item.MPN);
            //CleanInvalidXmlChars(item.Gtin);
            //CleanInvalidXmlChars(item.Brand);
            //CleanInvalidXmlChars(item.Description);

            return(dic);
        }
Example #3
0
        private static bool TryFormatRss20Item(Rss20Item itemToFormat, XNamespaceAliasSet namespaceAliases, ExtensionManifestDirectory extensionManifestDirectory, out XElement itemElement)
        {
            itemElement = default;

            if (itemToFormat == null)
            {
                return(false);
            }

            itemElement = new XElement("item");

            if (TryFormatOptionalTextElement(itemToFormat.Title, "title", out var titleElement))
            {
                itemElement.Add(titleElement);
            }

            if (TryFormatOptionalTextElement(itemToFormat.Link, "link", out var linkElement))
            {
                itemElement.Add(linkElement);
            }

            if (TryFormatOptionalTextElement(itemToFormat.Description, "description", out var descriptionElement))
            {
                itemElement.Add(descriptionElement);
            }

            if (TryFormatOptionalTextElement(itemToFormat.Author, "author", out var authorElement))
            {
                itemElement.Add(authorElement);
            }

            if (TryFormatOptionalTextElement(itemToFormat.Comments, "comments", out var commentsElement))
            {
                itemElement.Add(commentsElement);
            }

            foreach (var categoryToFormat in itemToFormat.Categories)
            {
                if (TryFormatRss20Category(categoryToFormat, out var categoryElement))
                {
                    itemElement.Add(categoryElement);
                }
            }

            foreach (var enclosureToFormat in itemToFormat.Enclosures)
            {
                if (TryFormatRss20Enclosure(enclosureToFormat, out var enclosureElement))
                {
                    itemElement.Add(enclosureElement);
                }
            }

            if (TryFormatRss20Timestamp(itemToFormat.PubDate, "pubDate", out var pubDateElement))
            {
                itemElement.Add(pubDateElement);
            }

            if (TryFormatRss20Source(itemToFormat.Source, out var sourceElement))
            {
                itemElement.Add(sourceElement);
            }

            if (TryFormatRss20Guid(itemToFormat.Guid, out var guidElement))
            {
                itemElement.Add(guidElement);
            }

            // extensions
            if (ExtensibleEntityFormatter.TryFormatXElementExtensions(itemToFormat, namespaceAliases, extensionManifestDirectory, out var extensionElements))
            {
                itemElement.AddRange(extensionElements);
            }

            return(true);
        }