Automatically exported from code.google.com/p/planningalerts
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Enfield.cgi 3.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. }