Automatically exported from code.google.com/p/planningalerts
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

140 řádky
5.0 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 Dacorum planning search
  9. our $SearchURL = "http://www.dacorum.gov.uk/default.aspx?page=1495";
  10. our $InfoURL = "http://www.dacorum.gov.uk/Default.aspx?page=1497&ID=";
  11. our $CommentURL = "http://www.dacorum.gov.uk/Default.aspx?page=2847&ID=";
  12. # We're a CGI script...
  13. my $query = CGI->new();
  14. # Construct an LWP user agent
  15. our $UA = LWP::UserAgent->new(env_proxy => 1,
  16. cookie_jar => {},
  17. requests_redirectable => [ 'GET', 'HEAD', 'POST' ]);
  18. # Post the URL to get an initial blank form
  19. my ($state, $eventvalidation) = get_state(do_post());
  20. # Do the search
  21. my $page = do_post({"__VIEWSTATE" => $state,
  22. "searchcriteria" => "Search",
  23. "Template\$ctl10\$ctl00\$rbSearchType2" => "rbOther",
  24. "Template\$ctl10\$ctl00\$tbApplicationReference" => "",
  25. "Template\$ctl10\$ctl00\$tbHouseNumber" => "",
  26. "Template\$ctl10\$ctl00\$tbStreetName" => "",
  27. "Template\$ctl10\$ctl00\$tbTownVillage" => "",
  28. "Template\$ctl10\$ctl00\$tbPostCode" => "",
  29. "Template\$ctl10\$ctl00\$tbApplicant" => "",
  30. "Template\$ctl10\$ctl00\$tbAgent" => "",
  31. "Template\$ctl10\$ctl00\$tbRegistrationFromDay" => "02",
  32. "Template\$ctl10\$ctl00\$tbRegistrationFromMon" => "10",
  33. "Template\$ctl10\$ctl00\$tbRegistrationFromYear" => "2008",
  34. "Template\$ctl10\$ctl00\$tbRegistrationToDay" => "02",
  35. "Template\$ctl10\$ctl00\$tbRegistrationToMon" => "10",
  36. "Template\$ctl10\$ctl00\$tbRegistrationToYear" => "2008",
  37. "Template\$ctl10\$ctl00\$tbDecisionFromDay" => "",
  38. "Template\$ctl10\$ctl00\$tbDecisionFromMon" => "",
  39. "Template\$ctl10\$ctl00\$tbDecisionFromYear" => "",
  40. "Template\$ctl10\$ctl00\$tbDecisionToDay" => "",
  41. "Template\$ctl10\$ctl00\$tbDecisionToMon" => "",
  42. "Template\$ctl10\$ctl00\$tbDecisionToYear" => "",
  43. "Template\$ctl10\$ctl00\$tbAppRecFromDay" => "",
  44. "Template\$ctl10\$ctl00\$tbAppRecFromMon" => "",
  45. "Template\$ctl10\$ctl00\$tbAppRecFromYear" => "",
  46. "Template\$ctl10\$ctl00\$tbAppRecToDay" => "",
  47. "Template\$ctl10\$ctl00\$tbAppRecToMon" => "",
  48. "Template\$ctl10\$ctl00\$tbAppRecToYear" => "",
  49. "Template\$ctl10\$ctl00\$tbAppDecFromDay" => "",
  50. "Template\$ctl10\$ctl00\$tbAppDecFromMon" => "",
  51. "Template\$ctl10\$ctl00\$tbAppDecFromYear" => "",
  52. "Template\$ctl10\$ctl00\$tbAppDecToDay" => "",
  53. "Template\$ctl10\$ctl00\$tbAppDecToMon" => "",
  54. "Template\$ctl10\$ctl00\$tbAppDecToYear" => "",
  55. "Template\$ctl10\$ctl00\$btnSearch" => "Search",
  56. "__EVENTVALIDATION" => $eventvalidation
  57. });
  58. # Output an HTTP response header
  59. print $query->header(-type => "text/xml");
  60. # Create an XML output stream
  61. my $Writer = XML::Writer->new(DATA_MODE => 1);
  62. # Output the XML header data
  63. $Writer->xmlDecl("UTF-8");
  64. $Writer->startTag("planning");
  65. $Writer->dataElement("authority_name", "Dacorum Borough Council");
  66. $Writer->dataElement("authority_short_name", "Dacorum");
  67. $Writer->startTag("applications");
  68. # Find the result table
  69. my $table = $page->look_down("_tag" => "table", "class" => "FormDataGrid");
  70. # Process each row of the results
  71. foreach my $row ($table->look_down("_tag" => "tr"))
  72. {
  73. $Writer->dataElement("test", "in for loop");
  74. my @cells = $row->look_down("_tag" => "td");
  75. if ($cells[0]->attr("class") eq "FormGridDataItem" ||
  76. $cells[0]->attr("class") eq "FormGridAlternatingDataItem")
  77. {
  78. my $reference = $cells[0]->as_trimmed_text;
  79. my $address = $cells[1]->as_trimmed_text;
  80. my $description = $cells[2]->as_trimmed_text;
  81. my $date = $cells[3]->as_trimmed_text;
  82. my $postcode;
  83. if ($address =~ /\s+([A-Z]+\d+\s+\d+[A-Z]+)$/)
  84. {
  85. $postcode = $1;
  86. }
  87. $Writer->startTag("application");
  88. $Writer->dataElement("council_reference", $reference);
  89. $Writer->dataElement("address", $address);
  90. $Writer->dataElement("postcode", $postcode);
  91. $Writer->dataElement("description", $description);
  92. $Writer->dataElement("info_url", $InfoURL . $reference);
  93. $Writer->dataElement("comment_url", $CommentURL . $reference);
  94. $Writer->dataElement("date_received", $date);
  95. $Writer->endTag("application");
  96. }
  97. }
  98. # Finish off XML output
  99. $Writer->endTag("applications");
  100. $Writer->endTag("planning");
  101. $Writer->end();
  102. exit 0;
  103. # Extract the state from a page so we can repost it
  104. sub get_state
  105. {
  106. my $page = shift;
  107. my $viewstate = $page->look_down("_tag" => "input", "name" => "__VIEWSTATE");
  108. my $eventvalidation = $page->look_down("_tag" => "input", "name" => "__EVENTVALIDATION");
  109. return ($viewstate->attr("value"), $eventvalidation->attr("value"));
  110. }
  111. # Post to the planning search page
  112. sub do_post
  113. {
  114. my $response = $UA->post($SearchURL, @_);
  115. die $response->status_line unless $response->is_success;
  116. return HTML::TreeBuilder->new_from_content($response->content);
  117. }