Exemple #1
0
        private void addInfoToComp(Component comp, HtmlDocument dom)
        {
            HtmlNode pn = dom.GetElementbyId("reportPartNumber");

            if (pn == null)
            {
                return;
            }
            comp.VerderComponentNumber = pn.InnerText.Trim(new char[] { '\r', '\t', '\n', ' ' });

            HtmlNode top = dom.GetElementbyId("product-overview");

            if (top == null)
            {
                return;
            }
            HtmlNode s1 = DOMUtils.getNextChildNodeType(top, "tbody", 0);

            if (s1 == null)
            {
                s1 = top;
            }

            foreach (HtmlNode hn in s1.ChildNodes)
            {
                if (hn.Name.Equals("tr"))
                {
                    HtmlNode td = DOMUtils.getNextChildNodeType(hn, "td", 0);
                    if (td == null)
                    {
                        continue;
                    }
                    HtmlNode h1 = DOMUtils.getNextChildNodeType(td, "h1", 0);
                    if (h1 != null)
                    {
                        comp.ManufacturerNumber = h1.InnerText.Trim(new char[] { '\r', '\t', '\n', ' ' });
                        continue;
                    }
                    HtmlNode h2 = DOMUtils.getNextChildNodeType(td, "h2", 0);
                    if (h2 != null)
                    {
                        HtmlNode span1 = DOMUtils.getNextChildNodeType(h2, "span", 0);
                        if (span1 == null)
                        {
                            continue;
                        }
                        HtmlNode span2 = DOMUtils.getNextChildNodeType(span1, "span", 0);
                        if (span2 == null)
                        {
                            continue;
                        }
                        comp.Manufacturer = span2.InnerText.Trim(new char[] { '\r', '\t', '\n', ' ' });
                        continue;
                    }
                }
            }

            comp.PriceString = "";
            HtmlNode pricedoc = dom.GetElementbyId("priceProcurement");

            if (pricedoc == null)
            {
                return;
            }
            HtmlNode p0 = DOMUtils.getNextChildNodeWithClass(pricedoc, "div", "catalog-pricing");

            if (p0 == null)
            {
                return;
            }
            HtmlNode p1 = DOMUtils.getNextChildNodeWithClass(p0, "table", "product-dollars");

            if (p1 == null)
            {
                return;
            }
            HtmlNode p2 = DOMUtils.getNextChildNodeType(p1, "tbody", 0);

            if (p2 == null)
            {
                p2 = p1;
            }
            foreach (HtmlNode hn in p2.ChildNodes)
            {
                if (hn.Name.Equals("tr"))
                {
                    HtmlNode amt = DOMUtils.getNextChildNodeType(hn, "td", 0);
                    if (amt == null)
                    {
                        continue;
                    }
                    HtmlNode upr = DOMUtils.getNextChildNodeType(hn, "td", 1);
                    if (upr == null)
                    {
                        continue;
                    }
                    HtmlNode upr2 = DOMUtils.getNextChildNodeType(hn, "span", 0);
                    if (upr2 != null)
                    {
                        upr = upr2;
                    }

                    try
                    {
                        String   amo = amt.InnerText.Trim(new char[] { '\r', '\t', '\n', ' ' }).Replace(".", "");
                        String[] pri = upr.InnerText.Trim(new char[] { '\r', '\t', '\n', ' ' }).Split(' ');

                        int    am = int.Parse(amo);
                        double pr = double.Parse(pri[pri.Length - 1]);

                        CompPrice cp = new CompPrice();
                        cp.Amount = am;
                        cp.Price  = pr;
                        comp.Prices.Add(cp);
                    }
                    catch (Exception e) { }
                }
            }
        }
Exemple #2
0
        public override Component getComponentFromNumber(string number)
        {
            Component comp    = Component.getFromVenderNumber(VenderName, number);
            bool      excists = false;

            if (comp != null)
            {
                if (comp.CheckedAt.AddDays(1).CompareTo(DateTime.Now) > 0)
                {
                    return(comp);
                }
                excists = true;
            }
            if (!excists)
            {
                comp = new Component();
            }

            HtmlDocument dom = DOMUtils.getDOMFromLink("https://benl.rs-online.com/web/c/?sra=oss&r=t&searchTerm=" + number);

            HtmlNode prices  = dom.GetElementbyId("break-prices-list");
            HtmlNode genInfo = dom.GetElementbyId("pagecell");

            if (prices == null || genInfo == null)
            {
                return(null);
            }

            comp.PriceString = "";
            foreach (HtmlNode p in prices.ChildNodes)
            {
                String amount = null;
                String price  = null;
                if (p.ChildNodes.Count > 2)
                {
                    foreach (HtmlNode cn in p.ChildNodes)
                    {
                        String classValue = null;
                        foreach (HtmlAttribute a in cn.Attributes)
                        {
                            if (a.Name.Equals("class"))
                            {
                                classValue = a.Value;
                            }
                        }

                        if (classValue != null)
                        {
                            if (classValue.StartsWith("breakRange"))
                            {
                                amount = cn.InnerHtml;
                            }
                            else if (classValue.StartsWith("unitPrice"))
                            {
                                price = cn.InnerHtml;
                            }
                        }
                    }
                }
                if (amount != null && price != null)
                {
                    try
                    {
                        String[] amo = amount.Trim(new char[] { '\r', '\t', '\n', ' ' }).Split(' ');
                        String[] pri = price.Trim(new char[] { '\r', '\t', '\n', ' ' }).Split(' ');

                        int    am = int.Parse(amo[0]);
                        double pr = double.Parse(pri[0]);

                        CompPrice cp = new CompPrice();
                        cp.Amount = am;
                        cp.Price  = pr;
                        comp.Prices.Add(cp);
                    }
                    catch (Exception e) {}
                }
            }

            addInfoToComp(comp, genInfo);
            comp.Link       = "https://benl.rs-online.com/web/c/?sra=oss&r=t&searchTerm=" + number;
            comp.Name       = number;
            comp.Vendername = VenderName;
            comp.CheckedAt  = DateTime.Now;

            if (excists)
            {
                Component.update(comp);
            }
            else
            {
                Component.add(comp);
            }

            return(comp);
        }
Exemple #3
0
        public override Component getComponentFromNumber(string number)
        {
            Component comp    = Component.getFromVenderNumber(VenderName, number);
            bool      excists = false;

            if (comp != null)
            {
                if (comp.CheckedAt.AddDays(1).CompareTo(DateTime.Now) > 0)
                {
                    return(comp);
                }
                excists = true;
            }
            if (!excists)
            {
                comp = new Component();
            }

            var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.mouser.com/api/V1/search/partnumber?apiKey=5e08dd87-13f3-444a-b64e-b91bc4cc2175");

            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Accept      = "application/json";
            httpWebRequest.Method      = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = new JavaScriptSerializer().Serialize(new
                {
                    SearchByPartRequest = new{
                        mouserPartNumber = number
                    }
                });

                streamWriter.Write(json);
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(MouserSearchPartReturnJSON));
            Stream respStream = httpResponse.GetResponseStream();
            MouserSearchPartReturnJSON mspr = (MouserSearchPartReturnJSON)ser.ReadObject(respStream);

            if (mspr.SearchResults.Parts.Count <= 0)
            {
                return(comp);
            }
            MouserMouserPartReturnJSON part = mspr.SearchResults.Parts[0];


            comp.Name                  = part.ManufacturerPartNumber;
            comp.Manufacturer          = part.Manufacturer;
            comp.ManufacturerNumber    = part.ManufacturerPartNumber;
            comp.Vendername            = VenderName;
            comp.VerderComponentNumber = part.MouserPartNumber;
            comp.PriceString           = "";
            foreach (MouserPricebreakReturnJSON price in part.PriceBreaks)
            {
                CompPrice cp = new CompPrice();
                cp.Amount = price.Quantity;
                cp.Price  = double.Parse(price.Price.Remove(price.Price.Length - 1).Replace(',', '.'), CultureInfo.InvariantCulture);
                comp.Prices.Add(cp);
            }
            comp.Link      = part.ProductDetailUrl;
            comp.CheckedAt = DateTime.Now;

            if (excists)
            {
                Component.update(comp);
            }
            else
            {
                Component.add(comp);
            }

            return(comp);
        }
        public override Component getComponentFromNumber(string number)
        {
            Component comp    = Component.getFromVenderNumber(VenderName, number);
            bool      excists = false;

            if (comp != null)
            {
                if (comp.CheckedAt.AddDays(1).CompareTo(DateTime.Now) > 0)
                {
                    return(comp);
                }
                excists = true;
            }
            if (!excists)
            {
                comp = new Component();
            }

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(
                "https://api.element14.com/catalog/products?callInfo.responseDataFormat=json&term=id:" + number +
                "&storeInfo.id=be.farnell.com&callInfo.apiKey=j8effcajubg2ug2vyekzhs6f&resultsSettings.responseGroup=medium");

            httpWebRequest.Accept = "application/json";
            httpWebRequest.Method = "GET";

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(FarnellProductSearchReturnJSON));
            Stream respStream = httpResponse.GetResponseStream();
            FarnellProductSearchReturnJSON mspr = (FarnellProductSearchReturnJSON)ser.ReadObject(respStream);

            if (mspr.premierFarnellPartNumberReturn.products.Count <= 0)
            {
                return(comp);
            }
            FarnellProductJSON product = mspr.premierFarnellPartNumberReturn.products[0];

            comp.Name                  = product.displayName;
            comp.Manufacturer          = product.vendorName;
            comp.ManufacturerNumber    = product.translatedManufacturerPartNumber;
            comp.Vendername            = VenderName;
            comp.VerderComponentNumber = number;
            comp.PriceString           = "";
            foreach (FarnellPriceJSON price in product.prices)
            {
                CompPrice cp = new CompPrice();
                cp.Amount = price.from;
                cp.Price  = price.cost;
                comp.Prices.Add(cp);
            }
            comp.Link      = "https://be.farnell.com/search?st=" + number;
            comp.CheckedAt = DateTime.Now;

            if (excists)
            {
                Component.update(comp);
            }
            else
            {
                Component.add(comp);
            }

            return(comp);
        }