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.
 
 
 
 
 
 

76 rader
1.7 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>Newport City Council</authority_name>
  23. <authority_short_name>Newport</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.newport.gov.uk/_dc/index.cfm?fuseaction=planapps.applist') or die "couldn't fetch index page"));
  44. $tree->eof;
  45. my $re = sprintf('Lists?\s+for %02d/%02d/%04d', $day, $month, $year);
  46. my ($day_p) = $tree->look_down(
  47. "_tag", "p",
  48. sub { $_[0]->as_text =~ /$re/i }
  49. );
  50. $day_p or no_results($year, $month, $day, "Cannot find day paragraph");
  51. my ($day_link) = $day_p->find_by_tag_name("a");
  52. $day_link or no_results($year, $month, $day, "Cannot find day link");
  53. my $day_absurl = $day_link->attr('href');
  54. my ($fh, $filename) = tempfile(SUFFIX => ".pdf");
  55. print $fh get($day_absurl);
  56. close($fh);
  57. print "Content-type: text/xml\n\n";
  58. system "./Newport.pl", $filename, $day_absurl and die "system failed: $|";
  59. unlink $filename or die "cannot unlink temporary file $filename: $!";