Created
March 7, 2019 20:17
-
-
Save nijssen/1d50d691f70396e4e1ec1a5ac9961888 to your computer and use it in GitHub Desktop.
UCSC class search creator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use WWW::Mechanize; | |
use List::MoreUtils qw/:all/; | |
use JSON qw/encode_json/; | |
use constant PISA_URL => "https://pisa.ucsc.edu/class_search/index.php"; | |
my $json = JSON->new->allow_nonref; | |
my $www = WWW::Mechanize->new; | |
$www->cookie_jar({}); | |
# Get the form to find the term info, then submit form | |
$www->get(PISA_URL); | |
my @terminfo_input = $www->find_all_inputs(name => 'binds[:term]'); | |
die "no terms found" unless @terminfo_input; | |
my @k = $terminfo_input[0]->possible_values; | |
my @v = $terminfo_input[0]->value_names; | |
# my %terms = zip @k, @v; | |
# delete $terms{$_} foreach grep { /summer/i } @v; | |
# my $terminfo = $terms{$terminfo_input[0]->value}; | |
my ($strm, $terminfo); | |
foreach (@k) { | |
$terminfo = shift @v; | |
next if $terminfo =~ /summer/i; | |
$strm = $_; | |
last; | |
} | |
$www->submit_form( | |
with_fields => { | |
"binds[:reg_status]" => "all", | |
"binds[:term]" => $strm | |
}, | |
strict_forms => 0 | |
); | |
$www->submit_form( | |
form_name => "resultsForm", | |
fields => { | |
rec_dur => 2 ** 31, | |
action => "update_segment" | |
} | |
); | |
# Get all links | |
my %classes = (); | |
my @class_links = $www->find_all_links(url_regex => qr/class_data/, id_regex => qr/class_id_/); | |
foreach (@class_links) { | |
my $key = $_->url_abs; | |
#my @info = split /[\s\xA0]+/, $_->text, 3; | |
#next unless @info > 1; | |
#$classes{$key} = \@info; | |
$classes{$key} = $_->text; | |
} | |
# join entries together | |
my @entries = map { join "^", ($classes{$_}, $_, "", "") } | |
sort { $classes{$a} cmp $classes{$b} } | |
keys %classes; | |
print "// Generated at " . scalar localtime . "\n"; | |
print "var terminfo = ", $json->encode($terminfo), ";\n"; | |
print "var s = ", $json->encode(\@entries), ";\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment