Exemplo n.º 1
0
        public void Supplement()
        {
            List <OrthoProduct> prods = GetProducts();
            List <string>       names = new List <string>();

            foreach (OrthoProduct prod in prods)
            {
                names.Add(prod.Name);
                prod.Name = prod.Name.Replace("™", "").Replace("®", "");
            }
            names.Sort();
            List <InvSupplement> inv = null;

            if (File.Exists(supplement))
            {
                inv = JsonConvert.DeserializeObject <List <InvSupplement> >(File.ReadAllText(supplement));
            }
            else
            {
                CleanupInventory(master, cleaned);
                inv = JsonConvert.DeserializeObject <List <InvSupplement> >(File.ReadAllText(cleaned)).Where(i => i.Company == "Ortho Molecular").ToList();
            }
            foreach (InvSupplement i in inv)
            {
                i.productNumber = "";
                if (i.Company == "Ortho Molecular" && i.Active)
                {
                    if (string.IsNullOrEmpty(i.Size))
                    {
                        string[] s = i.Supplement.Split(" - ");
                        i.Name = s[0];
                        if (s.Length >= 3)
                        {
                            i.Dosage = s[1];
                            i.Size   = s[2];
                            i.Size   = i.Size.Replace("caps", "C").Replace("tabs", "T").Replace("serv", "Serv").Replace("pkts", "Serv").Replace("oz", " oz.");
                        }
                        if (s.Length == 2)
                        {
                            i.Size = s[1];
                            i.Size = i.Size.Replace("caps", "C").Replace("tabs", "T").Replace("serv", "Serv").Replace("pkts", "Serv").Replace("oz", " oz.");
                        }
                    }

                    if (i.Size != null)
                    {
                        if (i.Active)
                        {
                            OrthoProduct prod = prods.FirstOrDefault(p => p.Name == i.Name);

                            if (prod == null)
                            {
                                System.Console.WriteLine($"No Matching Ortho Product - {i.Name}");
                            }
                            else
                            {
                                OrthoProduct.Variant v;
                                if (i.Flavor == null)
                                {
                                    OrthoProduct.Option op = prod.Options.FirstOrDefault(o => o.variantTypeName == "Flavor");
                                    if (op != null && op.values.Count == 1)
                                    {
                                        i.Flavor = op.values[0];
                                    }
                                }

                                if (i.Flavor == null)
                                {
                                    v = prod.Variants.FirstOrDefault(xv => xv.Option1 == i.Size && xv.Option2 == null);
                                }
                                else
                                {
                                    v = prod.Variants.FirstOrDefault(xv => (xv.Option1 == i.Size || xv.Option2 == i.Size) && (xv.Option1 == i.Flavor || xv.Option2 == i.Flavor));
                                }
                                if (v == null)
                                {
                                    System.Console.WriteLine($"No Matching OrthoProduct Variant - {i.Name} - {i.Size} - {i.Flavor}");
                                }
                                else
                                {
                                    i.productNumber = v.ProductNumber;
                                }
                            }
                        }
                    }
                    else
                    {
                        System.Console.WriteLine($"No Size OrthoProduct Product - {i.Name}");
                    }
                }
            }
            File.WriteAllText(supplement, JsonConvert.SerializeObject(inv, Formatting.Indented,
                                                                      new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));
        }
Exemplo n.º 2
0
        private OrthoProduct GetProduct(string path, string name, WebClient wc)
        {
            System.Console.Write(name);
            OrthoProduct product = new OrthoProduct();
            HtmlDocument doc     = GetPage(path, wc);
            HtmlNode     node;

            node             = doc.DocumentNode.SelectSingleNode("//meta[@property='og:image']");
            product.ImageUrl = node.Attributes["content"].Value;


            product.Name             = name;
            product.BriefDescription = doc.DocumentNode.SelectSingleNode("//p[@id='pShortDesc']")?.OuterHtml.Trim();

            node = doc.DocumentNode.SelectSingleNode("//h3[contains(., 'OVERVIEW')]/..");
            node?.RemoveChild(node.FirstChild);
            product.Description = node?.InnerHtml.Trim();

            node = doc.DocumentNode.SelectSingleNode("//h3[contains(., 'CLINICAL APPLICATIONS')]/..");
            node?.RemoveChild(node.FirstChild);
            product.Benefits = node?.InnerHtml.Trim();

            HtmlNode nc = doc.DocumentNode.SelectSingleNode("//script[contains(., 'IdevSelections(')]");

            if (nc != null)
            {
                OrthoProduct.Option op = new OrthoProduct.Option
                {
                    variantTypeName = "Size"
                };
                string json = nc.InnerText;
                json = json.Substring(json.IndexOf('{'));
                json = json.Substring(0, json.LastIndexOf('}') + 1);
                Example ex = JsonConvert.DeserializeObject <Example>(json);

                if (ex.Classifications.Count == 0)
                {
                    HtmlNodeCollection nc2 = doc.DocumentNode.SelectNodes("//span[@class='bottleSize']");
                    if (nc2 != null)
                    {
                        foreach (HtmlNode n2 in nc2)
                        {
                            op.values.Add(n2.InnerText.Trim());
                        }
                    }
                    else
                    {
                        System.Console.Write(" Error");
                    }
                }

                if (ex.Classifications.Count > 1)
                {
                    System.Console.Write(" Error2");
                }

                foreach (Classification c in ex.Classifications)
                {
                    foreach (S s in c.s)
                    {
                        op.values.Add(s.s);
                    }
                }
                product.Options.Add(op);
            }
            else
            {
                System.Console.Write(" Error3");
            }

            foreach (OrthoProduct.Option vt in product.Options)
            {
                foreach (string value in vt.values)
                {
                    OrthoProduct.Variant v = new OrthoProduct.Variant
                    {
                        Option1 = value
                    };
                    product.Variants.Add(v);
                }
            }
            if (product.Variants.Count == 1)
            {
                product.Variants[0].ImageUrl = product.ImageUrl;
            }
            System.Console.WriteLine(" - Complete");
            return(product);
        }