예제 #1
0
        /// <summary>
        /// Get the demographics data for all zipcodes in the list
        /// </summary>
        /// <param name="zipCodes"></param>
        /// <param name="saveToDb">Save each demographic as it is retrieved</param>
        /// <param name="randomWaitSeconds">Sleep for between 0 and randomWaitSeconds between each request</param>
        public static List <DemographicsModel> GetDemographics(List <int> zipCodes, [Optional] bool saveToDb, [Optional] int randomWaitSeconds)
        {
            List <DemographicsModel> results = new List <DemographicsModel>();

            DemographicsModel result = null;

            foreach (int zipCode in zipCodes)
            {
                // Sleep for a random number of seconds between 0 and randomWaitSeconds. Default is 0.
                Random random  = new Random();
                int    seconds = random.Next(0, randomWaitSeconds);
                System.Threading.Thread.Sleep(seconds * 1000);

                result = DemographicsQuery.GetDemographic(zipCode);
                if (null != result)
                {
                    results.Add(result);
                    if (saveToDb == true)
                    {
                        DemographicsData.UpdateDatabase(new List <DemographicsModel> {
                            result
                        });
                    }
                }
                else
                {
                    // We hit a ? and are blocked
                    break;
                }
            }

            Console.WriteLine("Queried a total of {0} zip codes", results.Count);
            return(results);
        }
예제 #2
0
        public static DemographicsModel GetDemographic(int zipCode)
        {
            if (zipCode > 99999)
            {
                Console.WriteLine("Zip Code {0} cannot be more than 5 digits. Skipping it.", zipCode);
                return(null);
            }

            string page = DemographicsQuery.GetPage(zipCode);

            if (page.Equals("?"))
            {
                Console.WriteLine("Request for {0} contains '?' which means that this IP address is blocked by zipwho.com. Use www.whatismyip.com to see your IP address.", zipCode);
                return(null);
            }

            DemographicsModel result = DemographicsQuery.ParsePage(page, zipCode);

            return(result);
        }