home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / bart-1.3b2.tar.gz / bart-1.3b2.tar / bart-1.3b2 / data / grab-fares < prev    next >
Text File  |  2000-01-12  |  3KB  |  152 lines

  1. #!/usr/bin/perl -w
  2.  
  3. # Perl script to get system fares from the web, so I don't have to
  4. # type them in myself.  Uses lynx to download from the web.  This
  5. # script assumes below that the excursion fare is $3.80.
  6.  
  7. # Copyright (c) 1999, 2000 Michael Wittman
  8. # Permission is hereby granted, free of charge, to any person obtaining a
  9. # copy of this software and associated documentation files (the "Software"),
  10. # to deal in the Software without restriction, including without limitation
  11. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. # and/or sell copies of the Software, and to permit persons to whom the
  13. # Software is furnished to do so, subject to the following conditions:
  14. # The above copyright notice and this permission notice shall be included
  15. # in all copies or substantial portions of the Software.
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. # OTHER DEALINGS IN THE SOFTWARE.
  23.  
  24. use strict;
  25.  
  26. # the following line sets $data_dir to the directory that this script is in
  27. my $data_dir = ($0 =~ m@^(?:(.*)/)?[^/]+$@)[0] || ".";
  28. my $fares_file = "$data_dir/fares";
  29. my $base_url = "http://www.transitinfo.org/cgi-bin/all_times?";
  30. my @fields = ("C", "D", "ALL", "TIME", "ATIME", "E");
  31. my @values = ("BA", "SA", "N", "1:35am", "2:00am", "");
  32.  
  33.  
  34.  
  35. my @stations = split '\n',
  36. "12ST
  37. 16ST
  38. 19ST
  39. 24ST
  40. ASHBY
  41. BALPK
  42. BFAIR
  43. BRK
  44. C-VLY
  45. CIVIC
  46. COLIS
  47. COLMA
  48. CONCD
  49. DALY
  50. DUBPL
  51. DEL-N
  52. PLAZA
  53. EMBAR
  54. FREMT
  55. FRTVL
  56. GLNPK
  57. HAY
  58. LAFAY
  59. LAKEM
  60. MACAR
  61. MONTG
  62. N-BRK
  63. N-CNC
  64. ORNDA
  65. PITTS
  66. PHILL
  67. POWEL
  68. RICH
  69. ROCKR
  70. SLEAN
  71. SHAY
  72. UCITY
  73. W-CRK
  74. W-OAK
  75. ";
  76.  
  77. sub sanitize {
  78.    local($_) = shift;
  79.    s/[^\w]|_/sprintf "%%%X", unpack "c", $&/ge;
  80.    $_;
  81. }
  82.  
  83. sub form_get_string {
  84.    my ($fields_ref,$values_ref) = @_;
  85.    my (@fields) = @$fields_ref;
  86.    my (@values) = @$values_ref;
  87.    my (@pairs);
  88.    my $i;
  89.  
  90.    for ($i = 0; $i < @fields; $i++) {
  91.       my ($field,$value) = ($fields[$i], $values[$i]);
  92.       push @pairs, join("=",&sanitize($field),&sanitize($value));
  93.    }
  94.  
  95.    join "&", @pairs;
  96. }
  97.  
  98. sub get_fare {
  99.    my $url = shift;
  100.    my $fare;
  101.    local($_);
  102.  
  103.    # print "\nlynx -source $url\n";
  104.    open(URL,"lynx -source \"$url\" |") || die "can't run lynx on url: $url\n";
  105.    while (defined($_ = <URL>)) {
  106.       if (/Fare:/) {
  107.      ($fare) = /\$(\d+\.\d\d)/;
  108.      last;
  109.       }
  110.    }
  111.    close(URL);
  112.  
  113.    $fare;
  114. }
  115.  
  116. sub my_print {
  117.    print @_;
  118.    print FARES @_;
  119. }
  120.  
  121. sub my_printf {
  122.    printf @_;
  123.    printf FARES @_;
  124. }
  125.  
  126. my ($i,$j);
  127.  
  128. $| = 1;
  129. open(FARES,"> $fares_file") || die "can't open fares file: $fares_file\n";
  130. for ($i = 0; $i < @stations; $i++) {
  131.    my_print "$stations[$i]";
  132.    for ($j = $i; $j < @stations; $j++) {
  133.       my $fare;
  134.       
  135.       if ($i == $j) {
  136.      $fare = 3.80;
  137.       }
  138.       else {
  139.      my (@flds) = (@fields, "FromStation", "ToStation");
  140.      my (@vals) = (@values, $stations[$i], $stations[$j]);
  141.      my $url = $base_url.&form_get_string(\@flds,\@vals);
  142.      $fare = &get_fare($url);
  143.       }
  144.       my_printf " %4.2f", $fare;
  145.    }
  146.    my_print "\n";
  147. }
  148. close(FARES);
  149.