Automatically exported from code.google.com/p/planningalerts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

99 lines
2.8 KiB

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use CGI qw(:cgi);
  5. use HTML::TreeBuilder;
  6. use LWP::UserAgent;
  7. use XML::Writer;
  8. # The master URLs for the North Hertfordshire planning search
  9. our $SearchURL = "http://www.north-herts.gov.uk/dcdataonline/Pages/acolnetcgi.gov?ACTION=UNWRAP&RIPNAME=Root.pgesearch";
  10. # We're a CGI script...
  11. my $query = CGI->new();
  12. # Get the date to fetch
  13. my $date = $query->param("day") . "/" . $query->param("month") . "/" . $query->param("year");
  14. # Construct an LWP user agent
  15. our $UA = LWP::UserAgent->new(env_proxy => 1);
  16. # Fetch the search page
  17. my $page = do_get($SearchURL);
  18. # Find the form submission URL
  19. my $form = $page->look_down("_tag" => "form", name => "frmSearch");
  20. my $url = URI->new_abs($form->attr("action"), $SearchURL);
  21. # Do the search
  22. $page = do_post($url, {"regdate1" => $date, "regdate2" => $date});
  23. # Output an HTTP response header
  24. print $query->header(-type => "text/xml");
  25. # Create an XML output stream
  26. my $Writer = XML::Writer->new(DATA_MODE => 1);
  27. # Output the XML header data
  28. $Writer->xmlDecl("UTF-8");
  29. $Writer->startTag("planning");
  30. $Writer->dataElement("authority_name", "North Hertfordshire District Council");
  31. $Writer->dataElement("authority_short_name", "North Hertfordshire");
  32. $Writer->startTag("applications");
  33. # Process each table of the results
  34. foreach my $table ($page->look_down("_tag" => "table", "class" => "results-table"))
  35. {
  36. my @rows = map { $_->look_down("_tag" => "td") } $table->look_down("_tag" => "tr");
  37. my $reference = $rows[0]->as_trimmed_text;
  38. my $infourl = $rows[0]->look_down("_tag" => "a")->attr("href");
  39. my $date = $rows[1]->as_trimmed_text;
  40. my $address = $rows[3]->as_trimmed_text;
  41. my $description = $rows[4]->as_trimmed_text;
  42. my $postcode;
  43. if ($address =~ /\s+([A-Z]+\d+\s+\d+[A-Z]+)$/)
  44. {
  45. $postcode = $1;
  46. }
  47. $Writer->startTag("application");
  48. $Writer->dataElement("council_reference", $reference);
  49. $Writer->dataElement("address", $address);
  50. $Writer->dataElement("postcode", $postcode);
  51. $Writer->dataElement("description", $description);
  52. $Writer->dataElement("info_url", $infourl);
  53. $Writer->dataElement("comment_url", "mailto:service\@north-herts.gov.uk?subject=Comment on Planning Application");
  54. $Writer->dataElement("date_received", $date);
  55. $Writer->endTag("application");
  56. }
  57. # Finish off XML output
  58. $Writer->endTag("applications");
  59. $Writer->endTag("planning");
  60. $Writer->end();
  61. exit 0;
  62. # Make a GET request
  63. sub do_get
  64. {
  65. my $response = $UA->get(@_);
  66. die $response->status_line unless $response->is_success;
  67. return HTML::TreeBuilder->new_from_content($response->content);
  68. }
  69. # Make a POST request
  70. sub do_post
  71. {
  72. my $response = $UA->post(@_, Content_Type => "form-data");
  73. die $response->status_line unless $response->is_success;
  74. return HTML::TreeBuilder->new_from_content($response->content);
  75. }