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.
 
 
 
 
 
 

122 lines
3.3 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 Enfield planning search
  9. our $SearchURL = "http://forms.enfield.gov.uk/swiftlg/apas/run/WPHAPPCRITERIA";
  10. our $InfoURL = "http://forms.enfield.gov.uk/swiftlg/apas/run/WPHAPPDETAIL.DisplayUrl?theApnID=";
  11. # We're a CGI script...
  12. my $query = CGI->new();
  13. # Get the date to fetch
  14. my $date = $query->param("day") . "/" . $query->param("month") . "/" . $query->param("year");
  15. # Construct an LWP user agent
  16. our $UA = LWP::UserAgent->new(env_proxy => 1);
  17. # Do the search
  18. my $page = do_post($SearchURL,
  19. {"REGFROMDATE.MAINBODY.WPACIS.1." => $date,
  20. "REGTODATE.MAINBODY.WPACIS.1." => $date,
  21. "SEARCHBUTTON.MAINBODY.WPACIS.1." => "Search"});
  22. # Output an HTTP response header
  23. print $query->header(-type => "text/xml");
  24. # Create an XML output stream
  25. my $Writer = XML::Writer->new(DATA_MODE => 1);
  26. # Output the XML header data
  27. $Writer->xmlDecl("UTF-8");
  28. $Writer->startTag("planning");
  29. $Writer->dataElement("authority_name", "Enfield Council");
  30. $Writer->dataElement("authority_short_name", "Enfield");
  31. $Writer->startTag("applications");
  32. # Output any applications on the first page
  33. output_applications($page);
  34. # Loop over any additional results pages
  35. foreach my $link ($page->look_down("_tag" => "a", "href" => qr/^WPHAPPSEARCHRES\.displayResultsURL/))
  36. {
  37. # Fetch this page...
  38. $page = do_get(URI->new_abs($link->attr("href"), $SearchURL));
  39. # ...and output the applications from it
  40. output_applications($page);
  41. }
  42. # Finish off XML output
  43. $Writer->endTag("applications");
  44. $Writer->endTag("planning");
  45. $Writer->end();
  46. exit 0;
  47. # Make a GET request
  48. sub do_get
  49. {
  50. my $response = $UA->get(@_);
  51. die $response->status_line unless $response->is_success;
  52. return HTML::TreeBuilder->new_from_content($response->content);
  53. }
  54. # Make a POST request
  55. sub do_post
  56. {
  57. my $response = $UA->post(@_);
  58. die $response->status_line unless $response->is_success;
  59. return HTML::TreeBuilder->new_from_content($response->content);
  60. }
  61. # Output applications from a results page
  62. sub output_applications
  63. {
  64. my $page = shift;
  65. # Find the result table
  66. my $table = $page->look_down("_tag" => "table", "class" => "apas_tbl");
  67. # Process each row of the results
  68. foreach my $row ($table->look_down("_tag" => "tr"))
  69. {
  70. my @cells = $row->look_down("_tag" => "td");
  71. if (@cells >= 3)
  72. {
  73. my $reference = $cells[0]->as_trimmed_text;
  74. my $description = $cells[1]->as_trimmed_text;
  75. my $address = $cells[2]->as_trimmed_text;
  76. my $postcode;
  77. if ($address =~ /\s+([A-Z]+\d+\s+\d+[A-Z]+)$/)
  78. {
  79. $postcode = $1;
  80. }
  81. $Writer->startTag("application");
  82. $Writer->dataElement("council_reference", $reference);
  83. $Writer->dataElement("address", $address);
  84. $Writer->dataElement("postcode", $postcode);
  85. $Writer->dataElement("description", $description);
  86. $Writer->dataElement("info_url", $InfoURL . $reference);
  87. $Writer->dataElement("date_received", $date);
  88. $Writer->endTag("application");
  89. }
  90. }
  91. return;
  92. }