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.
 
 
 
 
 
 

164 line
4.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. # Month names
  9. our %Months = ( 1 => "Jan", 2 => "Feb", 3 => "Mar", 4 => "Apr",
  10. 5 => "May", 5 => "Jun", 7 => "Jul", 8 => "Aug",
  11. 9 => "Sep", 10 => "Oct", 11 => "Nov", 12 => "Dec" );
  12. # The master URLs for the Enfield planning search
  13. our $StartURL = "http://www.southsomerset.gov.uk/index.jsp?articleid=1925&page_name=startsearch";
  14. our $SearchURL = "http://www.southsomerset.gov.uk/index.jsp?articleid=1925&page_name=searchresults";
  15. # We're a CGI script...
  16. my $query = CGI->new();
  17. # Get the date to fetch
  18. my $date = $query->param("day") . "-" . $Months{$query->param("month")} . "-" . $query->param("year");
  19. # Construct an LWP user agent
  20. our $UA = LWP::UserAgent->new(env_proxy => 1, cookie_jar => {});
  21. # Post acceptance of terms and conditions to get a cookie
  22. do_post($StartURL, {"acceptTC" => "on"});
  23. # Do the search
  24. my $page = do_post($SearchURL,
  25. {"startdate" => "12-Nov-2007", #$date,
  26. "enddate" => $date,
  27. "datesearch" => "applications",
  28. "timeframe" => "yearonly",
  29. "btnsubmit" => "search",
  30. "address" => "",
  31. "area" => "",
  32. "caseno" => "",
  33. "decision" => "",
  34. "location" => "",
  35. "parish" => "",
  36. "postcode" => "",
  37. "recentweeks" => "",
  38. "ward" => ""});
  39. # Output an HTTP response header
  40. print $query->header(-type => "text/xml");
  41. # Create an XML output stream
  42. my $Writer = XML::Writer->new(DATA_MODE => 1);
  43. # Output the XML header data
  44. $Writer->xmlDecl("UTF-8");
  45. $Writer->startTag("planning");
  46. $Writer->dataElement("authority_name", "South Somerset District Council");
  47. $Writer->dataElement("authority_short_name", "South Somerset");
  48. $Writer->startTag("applications");
  49. # Output any applications on the first page
  50. output_applications($page);
  51. # Loop over any additional results pages
  52. while (my $link = $page->look_down("_tag" => "a", sub { $_[0]->as_text eq "Next Page" }))
  53. {
  54. # Fetch this page...
  55. $page = do_get(URI->new_abs($link->attr("href"), $SearchURL));
  56. # ...and output the applications from it
  57. output_applications($page);
  58. }
  59. # Finish off XML output
  60. $Writer->endTag("applications");
  61. $Writer->endTag("planning");
  62. $Writer->end();
  63. exit 0;
  64. # Make a GET request
  65. sub do_get
  66. {
  67. my $response = $UA->get(@_);
  68. die $response->status_line unless $response->is_success;
  69. return HTML::TreeBuilder->new_from_content($response->content);
  70. }
  71. # Make a POST request
  72. sub do_post
  73. {
  74. my $response = $UA->post(@_);
  75. die $response->status_line unless $response->is_success;
  76. return HTML::TreeBuilder->new_from_content($response->content);
  77. }
  78. # Output applications from a results page
  79. sub output_applications
  80. {
  81. my $page = shift;
  82. my $reference;
  83. my $address;
  84. my $postcode;
  85. my $description;
  86. my $date_received;
  87. my $info_url;
  88. # Find the result table
  89. my $table = $page->look_down("_tag" => "div", "class" => "mainText")->look_down("_tag" => "table");
  90. # Process each row of the results
  91. foreach my $row ($table->look_down("_tag" => "tr"))
  92. {
  93. my @cells = $row->look_down("_tag" => "td");
  94. if (@cells == 1 && $cells[0]->look_down("_tag" => "hr"))
  95. {
  96. if (defined($reference))
  97. {
  98. $Writer->startTag("application");
  99. $Writer->dataElement("council_reference", $reference);
  100. $Writer->dataElement("address", $address);
  101. $Writer->dataElement("postcode", $postcode);
  102. $Writer->dataElement("description", $description);
  103. $Writer->dataElement("info_url", $info_url);
  104. $Writer->dataElement("date_received", $date_received);
  105. $Writer->endTag("application");
  106. }
  107. undef $reference;
  108. undef $address;
  109. undef $postcode;
  110. undef $description;
  111. undef $date_received;
  112. undef $info_url
  113. }
  114. elsif (@cells == 1 && defined($reference))
  115. {
  116. $description = $cells[0]->as_trimmed_text;
  117. $description =~ s/^Proposal:\s*//;
  118. }
  119. elsif (@cells == 5 && $cells[0]->as_trimmed_text =~ /^\d+/)
  120. {
  121. $reference = $cells[0]->as_trimmed_text;
  122. $date_received = $cells[1]->as_trimmed_text;
  123. $address = $cells[2]->as_trimmed_text;
  124. $info_url = URI->new_abs($cells[4]->look_down("_tag" => "a")->attr("href"), $SearchURL);
  125. if ($address =~ /\s+([A-Z]+\d+\s+\d+[A-Z]+)$/)
  126. {
  127. $postcode = $1;
  128. }
  129. }
  130. }
  131. return;
  132. }