Automatically exported from code.google.com/p/planningalerts
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Dacorum.cgi 4.9 KiB

17 anos atrás
17 anos atrás
17 anos atrás
17 anos atrás
17 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. my @cells = $row->look_down("_tag" => "td");
  74. if ($cells[0]->attr("class") eq "FormGridDataItem" ||
  75. $cells[0]->attr("class") eq "FormGridAlternatingDataItem")
  76. {
  77. my $reference = $cells[0]->as_trimmed_text;
  78. my $address = $cells[1]->as_trimmed_text;
  79. my $description = $cells[2]->as_trimmed_text;
  80. my $date = $cells[3]->as_trimmed_text;
  81. my $postcode;
  82. if ($address =~ /\s+([A-Z]+\d+\s+\d+[A-Z]+)$/)
  83. {
  84. $postcode = $1;
  85. }
  86. $Writer->startTag("application");
  87. $Writer->dataElement("council_reference", $reference);
  88. $Writer->dataElement("address", $address);
  89. $Writer->dataElement("postcode", $postcode);
  90. $Writer->dataElement("description", $description);
  91. $Writer->dataElement("info_url", $InfoURL . $reference);
  92. $Writer->dataElement("comment_url", $CommentURL . $reference);
  93. $Writer->dataElement("date_received", $date);
  94. $Writer->endTag("application");
  95. }
  96. }
  97. # Finish off XML output
  98. $Writer->endTag("applications");
  99. $Writer->endTag("planning");
  100. $Writer->end();
  101. exit 0;
  102. # Extract the state from a page so we can repost it
  103. sub get_state
  104. {
  105. my $page = shift;
  106. my $viewstate = $page->look_down("_tag" => "input", "name" => "__VIEWSTATE");
  107. my $eventvalidation = $page->look_down("_tag" => "input", "name" => "__EVENTVALIDATION");
  108. return ($viewstate->attr("value"), $eventvalidation->attr("value"));
  109. }
  110. # Post to the planning search page
  111. sub do_post
  112. {
  113. my $response = $UA->post($SearchURL, @_);
  114. die $response->status_line unless $response->is_success;
  115. return HTML::TreeBuilder->new_from_content($response->content);
  116. }