using System.Globalization; using System.Net; using System.Xml.Linq; using Google.Cloud.BigQuery.V2; using Microsoft.Azure.Functions.Worker; using Microsoft.Azure.Functions.Worker.Http; using Microsoft.Extensions.Logging; using Newtonsoft.Json.Linq; namespace OSAFeedXML { public class Function1 { private readonly ILogger _logger; private readonly IBigQuery _bq; private readonly string DomainRedirect, ShortDescription, Title; public Function1(ILoggerFactory loggerFactory, IBigQuery bq) { _logger = loggerFactory.CreateLogger(); DomainRedirect = Environment.GetEnvironmentVariable("DOMAIN_REDIRECT") ?? throw new Exception("Brak domeny przekierowania."); ShortDescription = Environment.GetEnvironmentVariable("SHORT_DESCRIPTION") ?? throw new Exception("Brak krótkiego opisu."); Title = Environment.GetEnvironmentVariable("TITLE") ?? throw new Exception("Brak tytułu."); _bq = bq; } [Function("getfeeds")] public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req) { try { var queryParams = System.Web.HttpUtility.ParseQueryString(req.Url.Query); var sourceParam = queryParams["source"]; var destinationList = new List(); Boolean.TryParse(queryParams["withoutbg"], out var withoutbg); if (sourceParam != null) { var sources = sourceParam.Split(','); destinationList = sources .Select(src => Enum.TryParse(typeof(Destinations), src, true, out var result) ? result : null) .Where(v => v != null) .Cast() .ToList(); } else { destinationList = new List() { Destinations.google, Destinations.facebook }; } var result = _bq.GetDataBigQuery(destinationList, withoutbg); var jsonArray = new JArray( ((BigQueryResults)result).Select(row => new JObject( row.Schema.Fields.Select(field => new JProperty(field.Name, JToken.FromObject(row[field.Name] ?? string.Empty))) )) ); JArray products = JArray.Parse(jsonArray.ToString()); XNamespace g = "http://base.google.com/ns/1.0"; XDocument xmlDoc = new XDocument( new XElement("rss", new XAttribute("version", "2.0"), new XAttribute(XNamespace.Xmlns + "g", g), new XElement("channel", new XElement("title", Title), new XElement("link", DomainRedirect), new XElement("description", ShortDescription), from product in products select new XElement("item", new XElement(g + "id", product["id"]?.ToString()), new XElement(g + "title", product["title"]?.ToString()), new XElement(g + "description", product["description"]?.ToString()), new XElement(g + "link", product["link"]?.ToString()), new XElement(g + "image_link", product["image_link"]?.ToString()), new XElement(g + "availability", product["availability"]?.ToString()), new XElement(g + "price", FormatPrice(product["price"] ?? 0)), new XElement(g + "brand", product["brand"]?.ToString()), new XElement(g + "gtin", product["gtin"]?.ToString()), new XElement(g + "mpn", product["mpn"]?.ToString()), new XElement(g + "product_type", product["product_type"]?.ToString()), new XElement(g + "google_product_category", product["google_product_category"]?.ToString()), new XElement(g + "unit_pricing_measure", product["unit_pricing_measure"]?.ToString()), new XElement(g + "condition", product["condition"]?.ToString()), product["gtin"]?.ToString() == "" ? new XElement(g + "identifier_exists", "no") : new XElement(g + "identifier_exists", "yes") ) ) ) ); var response = req.CreateResponse(HttpStatusCode.OK); response.Headers.Add("Content-Type", "text/xml; charset=utf-8"); response.WriteString(xmlDoc.ToString()); return response; } catch (Exception ex) { _logger.LogError(ex, ex.Message); return req.CreateResponse(HttpStatusCode.InternalServerError); } } static string FormatPrice(JToken priceToken) { if (priceToken == null || string.IsNullOrWhiteSpace(priceToken.ToString())) { return "0.00 PLN"; } decimal price = priceToken.Value(); return price.ToString("0.00", CultureInfo.InvariantCulture) + " PLN"; } } }