示例#1
0
        //Get Chicken Ordinances
        protected void GetLaws_Click(object sender, EventArgs e)
        {
            keydowno_backyard_farmerEntities db = new keydowno_backyard_farmerEntities();

            string source = PerformRequest("http://www.backyardchickens.com/atype/3/Laws");

            //Get number of records.
            int start = source.IndexOf("<div class=\"actionbar shazam\">");
            int end = source.IndexOf("law / ordinance", start);

            string pagingBlock = source.Substring(start, end - start);

            start = pagingBlock.IndexOf(" of ");
            end = pagingBlock.IndexOf("\">", start);

            string recordsNumberBlock = pagingBlock.Substring(start + " of ".Length, end - start - " of ".Length);
            int recordsNumber = int.Parse(recordsNumberBlock);

            List<string> articleLinks = new List<string>(recordsNumber);

            //Get data for every page.
            for (int pageBlock = 0; pageBlock < recordsNumber; pageBlock += 10)
            {
                string innerUrl = "http://www.backyardchickens.com/atype/3/Laws/page/" + pageBlock.ToString();
                source = PerformRequest(innerUrl);

                GetLawArticleLinks(source, articleLinks);
            }

            foreach (var link in articleLinks)
            {
                try
                {
                    GetSpecificLawTable(link, db);
                }
                //That site is just awful.
                catch (Exception)
                {
                }
            }

            lblProgress.Text = String.Format("{0} of {1} records processed.", articleLinks.Count, recordsNumber);
            ResultLaws.Text = articleLinks[articleLinks.Count - 1];
        }
示例#2
0
        //Get the law table for a specific location.
        void GetSpecificLawTable(string link, keydowno_backyard_farmerEntities db)
        {
            string source = PerformRequest(link);

            int start = source.IndexOf("<table");
            int end = source.IndexOf("</table>");

            if (start != -1)
            {
                string tableBlock = source.Substring(start, end - start);

                ChickenLaws law = new ChickenLaws();

                start = tableBlock.IndexOf(">", tableBlock.IndexOf("<td", tableBlock.IndexOf("<td") + 1));
                end = tableBlock.IndexOf("</td>", start);
                string areChickensAllowed = tableBlock.Substring(start + ">".Length, end - start - ">".Length);
                areChickensAllowed = StripHtmlTags(areChickensAllowed);

                start = tableBlock.IndexOf(">", tableBlock.IndexOf("<td", tableBlock.IndexOf("<td", start + 1) + 1));
                end = tableBlock.IndexOf("</td>", start);
                string maxChickensAllowed = tableBlock.Substring(start + ">".Length, end - start - ">".Length);
                maxChickensAllowed = StripHtmlTags(maxChickensAllowed);

                start = tableBlock.IndexOf(">", tableBlock.IndexOf("<td", tableBlock.IndexOf("<td", start + 1) + 1));
                end = tableBlock.IndexOf("</td>", start);
                string roostersAllowed = tableBlock.Substring(start + ">".Length, end - start - ">".Length);
                roostersAllowed = StripHtmlTags(roostersAllowed);

                start = tableBlock.IndexOf(">", tableBlock.IndexOf("<td", tableBlock.IndexOf("<td", start + 1) + 1));
                end = tableBlock.IndexOf("</td>", start);
                string permitRequired = tableBlock.Substring(start + ">".Length, end - start - ">".Length);
                permitRequired = StripHtmlTags(permitRequired);

                start = tableBlock.IndexOf(">", tableBlock.IndexOf("<td", tableBlock.IndexOf("<td", start + 1) + 1));
                end = tableBlock.IndexOf("</td>", start);
                string coopRestrictions = tableBlock.Substring(start + ">".Length, end - start - ">".Length);
                coopRestrictions = StripHtmlTags(coopRestrictions);

                start = tableBlock.IndexOf(">", tableBlock.IndexOf("<td", tableBlock.IndexOf("<td", start + 1) + 1));
                end = tableBlock.IndexOf("</td>", start);
                string contacts = tableBlock.Substring(start + ">".Length, end - start - ">".Length);
                contacts = StripHtmlTags(contacts);

                start = tableBlock.IndexOf(">", tableBlock.IndexOf("<td", tableBlock.IndexOf("<td", start + 1) + 1));
                end = tableBlock.IndexOf("</td>", start);
                string info = tableBlock.Substring(start + ">".Length, end - start - ">".Length);
                info = StripHtmlTags(info);

                start = tableBlock.IndexOf(">", tableBlock.IndexOf("<td", tableBlock.IndexOf("<td", start + 1) + 1));
                end = tableBlock.IndexOf("</td>", start);
                string contactLink = tableBlock.Substring(start + ">".Length, end - start - ">".Length);
                contactLink = StripHtmlTags(contactLink);

                start = tableBlock.IndexOf(">", tableBlock.IndexOf("<td", tableBlock.IndexOf("<td", start + 1) + 1));
                end = tableBlock.IndexOf("</td>", start);
                string lastUpdate = tableBlock.Substring(start + ">".Length, end - start - ">".Length);
                lastUpdate = StripHtmlTags(lastUpdate);

                int linkStart = link.LastIndexOf("/");
                string googleJsonParam = link.Substring(linkStart + 1, link.Length - (linkStart + 1));
                googleJsonParam = googleJsonParam.Replace('-', '+');
                googleJsonParam = googleJsonParam.Replace("+chicken+ordinance", "");
                string googleJson = PerformRequest(String.Format("http://maps.googleapis.com/maps/api/geocode/json?address={0}&sensor=true", googleJsonParam));
                int locationStart = googleJson.IndexOf("formatted_address");
                if (locationStart != -1)
                {
                    int locationEnd = googleJson.IndexOf("\",", locationStart);
                    googleJson = googleJson.Substring(locationStart + "formatted_address\" : \"".Length, locationEnd - locationStart - "formatted_address\" : \"".Length);
                }
                else
                {
                    googleJson = googleJsonParam.Replace('+', ' ');
                }

                law.Location = googleJson;
                law.AreChickensAllowed = areChickensAllowed;
                law.ChickensAllowed = maxChickensAllowed;
                law.RoostersAllowed = roostersAllowed;
                law.PermitRequired = permitRequired;
                law.CoopRestrictions = coopRestrictions;
                law.Contacts = contacts;
                law.Info = info;
                law.Link = contactLink;
                law.LastUpdate = lastUpdate;

                db.ChickenLaws.Add(law);
                db.SaveChanges();
            }
            else
            {
                return;
            }

            //We have to somehow match the town/region/state with our existing ZipCodes table data.

            //Code to insert html table elements into Database.
        }