On 6/17/2004 8:41 PM, bruce wrote:

randy...

here's the rewritten app...

It's still hard to see exactly what your talking about. Try the following. If it does not work as expected, please insert some assertions and comments where the results differ from your expectations:


#############################################################
#
# uminnesota.pl. test perl app to extract/parse class
#       schedule from the onestop2.umn.edu registrar site...
#
#
# todo.. need to create the getinfo routine to parse the
#        class information, and write it out
#
#############################################################
#!/usr/bin/perl -w

use strict;

use HTML::TreeBuilder;
use LWP::UserAgent;
use URI::Escape;

my $ua = new LWP::UserAgent;

$ua->timeout(30);
$ua->agent("Mozilla/4.0 (compatible; MSIE 4.01; Windows95)" . $ua->agent);

#
# get the top level university class site
#
my $base_url = "http://onestop2.umn.edu/courseinfo/classschedule_selectsubject.jsp?institution=UMNTC";;


my $base_search_url = "http://onestop2.umn.edu/courseinfo/viewClassScheduleTermAndSubject.do";;


# get semesters my (%s1) = &getsemesters($base_url);

# get depts
my (%d1) = &getdepts($base_url);


&get($base_search_url, \%s1, \%d1);

print "we're here...\n";
die;



#################################
#
#
#################################
sub getsemesters {
    my ($url0) = @_;
    my (%sem_);

    my $req = new HTTP::Request GET => $url0;

    $req->content_type('application/x-www-form-urlencoded');

    my $res = $ua->request($req);
    my $q1c = $res->content;

    my $_tree = HTML::TreeBuilder->new_from_content($res->content); # tree

    # get the specific select
    my @_sel = $_tree->look_down("_tag"=>"select", "name"=>"searchTerm");

    #get the option/semester list
    my @_option = $_sel[0]->look_down("_tag"=>"option");
    foreach $a (@_option) {
        $sem_{$a->attr('value')} = $a->as_text();
    }

    return(%sem_);
}


# # return the hash containing the dept list # # if necessary, might need to also provide the # short name for the dept.. # sub getdepts { my ($url) = @_; my (%depts_);

    my $req = new HTTP::Request GET => $url;

    $req->content_type('application/x-www-form-urlencoded');

    my $res = $ua->request($req);
    my $q1c = $res->content;

    my $_tree = HTML::TreeBuilder->new_from_content($res->content); # tree

# get the specific select
my @_sel = $_tree->look_down("_tag"=>"select", "name"=>"searchSubject");


    #get the option/semester list
    my @_option = $_sel[0]->look_down("_tag"=>"option");
    foreach $a (@_option) {
        $depts_{$a->attr('value')} = $a->as_text();
    }

    return(%depts_);
}

sub get {
    my $url = shift;
    my %semester = %{ +shift };
    my %dept     = %{ +shift };

    my %query = (
        searchFullYearEnrollmentOnly => 'false',
        institution   => 'UMNTC',
        Submit        => 'View',
        searchTerm    => '',
        searchSubject => '',
    );

    foreach my $semester_key (keys(%semester)) {
        foreach my $dept_key (keys(%dept)) {
            print " sem_key = $semester_key   deptkey  $dept_key\n";
            $query{searchTerm} = uri_escape( $semester_key );
            $query{searchSubject} = uri_escape( $dept_key );

            my $ua = new LWP::UserAgent;
            $ua->timeout(30);
            $ua->agent("Mozilla/4.0 (compatible; MSIE 4.01; Windows95)" .
                       $ua->agent);

            my $res = $ua->post( $url, \%query );

            my $q1c = $res->content;
            print $q1c;
        }
    }
}




Reply via email to