Automatically exported from code.google.com/p/planningalerts
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

83 lines
2.0 KiB

  1. #!/usr/bin/perl -w
  2. use strict;
  3. use HTML::TreeBuilder;
  4. use File::Temp qw(tempfile);
  5. use LWP::Simple;
  6. use POSIX;
  7. use Encode;
  8. use CGI;
  9. use CGI::Carp;
  10. sub sanity_check {
  11. my ($var) = @_;
  12. defined $var or return 0;
  13. $var =~ /^[0-9]+$/ or return 0;
  14. return 1;
  15. }
  16. sub no_results {
  17. my ($y, $m, $d, $reason) = @_;
  18. print <<NIL;
  19. Content-type: text/xml
  20. <?xml version="1.0" encoding="UTF-8"?>
  21. <planning>
  22. <authority_name>Highland Council</authority_name>
  23. <authority_short_name>Highland</authority_short_name>
  24. <applications>
  25. </applications>
  26. </planning>
  27. NIL
  28. die "$y/$m/$d failed: $reason\n";
  29. }
  30. my $cgi = new CGI;
  31. my $year = $cgi->param("year");
  32. my $month = $cgi->param("month");
  33. my $day = $cgi->param("day");
  34. unless (sanity_check($year) && sanity_check($month) && sanity_check($day)) {
  35. print <<ERROR;
  36. Content-type: text/plain
  37. Need year, month, day parameters
  38. ERROR
  39. exit 0;
  40. }
  41. my $tree = HTML::TreeBuilder->new;
  42. # $tree->parse_file('weekly-planning-bw-lists.htm');
  43. $tree->parse(decode_utf8(get('http://www.highland.gov.uk/yourenvironment/planning/planningapplications/weekly-planning-bw-lists.htm') or die "couldn't fetch index page"));
  44. $tree->eof;
  45. my $monthyear_re = strftime('%B[ \xa0]%Y', 0, 0, 0, 1, $month-1, $year-1900);
  46. my ($month_h2) = $tree->look_down(
  47. "_tag", "h2",
  48. sub { $_[0]->as_text =~ /$monthyear_re/ }
  49. );
  50. $month_h2 or no_results($year, $month, $day, "Cannot find month header");
  51. my $month_list = $month_h2->right;
  52. my $day_re = qr/^Planning Applications.* $day[a-z]+$/;
  53. my ($day_link) = $month_list->look_down(
  54. "_tag", "a",
  55. sub { $_[0]->as_text =~ /$day_re/ }
  56. );
  57. $day_link or no_results($year, $month, $day, "Cannot find day link");
  58. my $day_absurl = "http://www.highland.gov.uk".$day_link->attr('href');
  59. my ($fh, $filename) = tempfile(SUFFIX => ".pdf");
  60. print $fh get($day_absurl);
  61. close($fh);
  62. print "Content-type: text/xml\n\n";
  63. system "./Highland.pl", $filename, $day_absurl and die "system failed: $|";
  64. unlink $filename or die "cannot unlink temporary file $filename: $!";