///***************************************************/
        ///**** Public Methods                            ****/
        ///***************************************************/

        public static void SetHashFragment(IEnumerable <IBHoMObject> objs, List <string> exceptions = null, bool useDefaultExceptions = true)
        {
            if (useDefaultExceptions)
            {
                Compute.SetDefaultExceptions(ref exceptions);
            }

            // Calculate and set the object hashes
            foreach (var obj in objs)
            {
                string hash = BH.Engine.Diffing.Compute.DiffingHash(obj, exceptions, useDefaultExceptions);

                HashFragment existingFragm = obj.GetHashFragment();

                if (existingFragm != null)
                {
                    obj.Fragments.RemoveAll(fr => (fr as HashFragment) != null);
                    obj.Fragments.Add(new HashFragment(hash, existingFragm.Hash));
                }
                else
                {
                    obj.Fragments.Add(new HashFragment(hash, null));
                }
            }
        }
Пример #2
0
        public static T SetHashFragment <T>(T obj, string hash) where T : IBHoMObject
        {
            // Clone the current object to preserve immutability
            T obj_cloned = BH.Engine.Base.Query.DeepClone(obj);

            HashFragment existingFragm = obj_cloned.GetHashFragment();

            obj_cloned.Fragments.AddOrReplace(new HashFragment(hash, existingFragm?.CurrentHash));

            return(obj_cloned);
        }
        /***************************************************/

        public int GetHashCode(T obj)
        {
            if (typeof(IBHoMObject).IsAssignableFrom(typeof(T)))
            {
                IBHoMObject  bHoMObject   = (IBHoMObject)obj;
                HashFragment hashFragment = bHoMObject.GetHashFragment();
                if (!string.IsNullOrWhiteSpace(hashFragment?.CurrentHash))
                {
                    return(hashFragment.CurrentHash.GetHashCode());
                }
            }

            return(obj.DiffingHash(DiffConfig).GetHashCode());
        }
Пример #4
0
        public static void SetHashFragment(IEnumerable <IBHoMObject> objs, DiffConfig diffConfig = null)
        {
            // Set configurations if diffConfig is null
            diffConfig = diffConfig == null ? new DiffConfig() : diffConfig;

            // Calculate and set the object hashes
            foreach (var obj in objs)
            {
                string hash = BH.Engine.Diffing.Compute.DiffingHash(obj, diffConfig);

                HashFragment existingFragm = obj.GetHashFragment();

                obj.Fragments.AddOrReplace(new HashFragment(hash, existingFragm?.Hash));
            }
        }
Пример #5
0
        public static T SetHashFragment <T>(T obj, DiffConfig diffConfig = null) where T : IBHoMObject
        {
            // Clone the current object to preserve immutability
            T obj_cloned = BH.Engine.Base.Query.DeepClone(obj);

            // Set configurations if diffConfig is null
            diffConfig = diffConfig == null ? new DiffConfig() : diffConfig;

            // Calculate and set the object hashes
            string hash = BH.Engine.Diffing.Compute.DiffingHash(obj_cloned, diffConfig);

            HashFragment existingFragm = obj_cloned.GetHashFragment();

            obj_cloned.Fragments.AddOrReplace(new HashFragment(hash, existingFragm?.CurrentHash));

            return(obj_cloned);
        }
Пример #6
0
        public ExecutionResult ImportFromSite(string state, int page, bool next = false)
        {
            int totalPages = 0;

            log.Info(string.Format("Carregando pagina {0} - {1}", page, state));
            HashFragment   hashFragment = JsonConvert.DeserializeObject <HashFragment>("{\"precomaximo\":\"2147483647\",\"parametrosautosuggest\":[{\"Bairro\":\"\",\"Zona\":\"\",\"Cidade\":\"\",\"Agrupamento\":\"\",\"Estado\":\"" + state + "\"}],\"pagina\":\"" + page + "\",\"ordem\":\"Relevancia\",\"paginaOrigem\":\"ResultadoBusca\",\"semente\":\"0\"}");
            string         enc          = HttpUtility.UrlEncode(JsonConvert.SerializeObject(hashFragment));
            string         url          = "http://www.zapimoveis.com.br/Busca/RetornarBuscaAssincrona/";
            string         jsonContent  = "tipoOferta=1&paginaAtual=1&ordenacaoSelecionada=&pathName=&hashFragment=" + enc;
            Uri            target       = new Uri(url);
            HttpWebRequest request      = (HttpWebRequest)WebRequest.Create(url);

            request.Method = "POST";
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            Byte[] byteArray = encoding.GetBytes(jsonContent);
            request.ContentLength = byteArray.Length;
            request.ContentType   = @"application/x-www-form-urlencoded; charset=UTF-8";
            //request.Referer = "http://www.zapimoveis.com.br/venda/apartamentos/rs+porto-alegre/";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0";
            request.KeepAlive = true;
            using (Stream dataStream = request.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Dispose();
            }

            try
            {
                ResultContainer result;
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")))
                        {
                            StringBuilder json = new StringBuilder();
                            while (reader.Peek() >= 0)
                            {
                                json.Append(reader.ReadLine());
                            }

                            result = JsonConvert.DeserializeObject <ResultContainer>(json.ToString());
                            json.Clear();
                            json       = null;
                            totalPages = result.Resultado.QuantidadePaginas;
                            if (page == 1)
                            {
                                log.Info(String.Format("*** Total de Paginas {0} {1}", result.Resultado.QuantidadePaginas, state));
                            }
                            _zapService.Insert(result).Wait();
                            log.Info(String.Format("Pagina {0} de {1} {2} - OK", page, totalPages, state));
                        }
                        responseStream.Dispose();
                        GC.Collect();
                        Thread.Sleep(1000);     //give disk hardware time to recover
                    }
                    response.Dispose();
                    GC.Collect();
                    Thread.Sleep(1000);     //give disk hardware time to recover
                }
                //
                if (page == result.Resultado.QuantidadePaginas)
                {
                    return new ExecutionResult()
                           {
                               GotoNext = true
                           }
                }
                ;
                else
                {
                    result = null;

                    page++;
                    return(new ExecutionResult()
                    {
                        Page = page, State = state
                    });
                }
            }
            catch (WebException ex)
            {
                log.Error(ex.ToString());
                log.Info(String.Format("Retentando {0} de {1} {2}", page, totalPages, state));
                return(new ExecutionResult()
                {
                    Page = page, State = state
                });
            }
        }
        /***************************************************/

        // Modify in-place.
        private static bool SetHashFragment(IBHoMObject obj, string hash)
        {
            HashFragment existingFragm = obj.GetHashFragment();

            return(obj.Fragments.AddOrReplace(new HashFragment(hash, existingFragm?.CurrentHash)));
        }