#!/usr/bin/perl
# js.pl - JobServe Reformatter
# (c) James Powell - Jan 2001
use CGI qw/:standard/;
use CGI::Cookie;
use LWP;
use strict;
use URI::Escape;
use CGI::Carp qw(fatalsToBrowser);
use Time::localtime;
my $query = new CGI;
#my $query = new CGI(\*STDIN); # DEBUG
# get cookies
# {{{
my %cookies = fetch CGI::Cookie;
no strict "refs";
my @cooks = @{$cookies{jobsearch1}->{value}};
my %ckhash = @cooks;
my @jobids = @{$cookies{jobidbook}->{value}};
my %jobidhash = @jobids;
use strict "refs";
# }}}
if ($query->param('q')) {
my $ua = new LWP::UserAgent;
$ua->agent("AgentName/0.1 " . $ua->agent);
# Create a request
my $queryurl = sprintf('http://www.it.jobserve.com/jobserve/searchresults.asp?JobType=%s&d=%s&order=DateTime&page=%s&q=%s',
($query->param('JobType') || $query->param('jobtype')), $query->param('d'), $query->param('page'),
# $query->param('q')); # DEBUG
uri_escape($query->param('q'), "^A-Za-z"));
# print $queryurl; # DEBUG
my $req = new HTTP::Request GET => $queryurl;
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
# log it
if ($query->param('page') < 2) {
my $tm = localtime(time);
if (-d '/users/jamespo/www/jobserve/') {
open(LOG,">>/users/jamespo/www/jobserve/usage.txt") or die $!;
printf(LOG "%02d:%02d:%02d-%04d/%02d/%02d\t%s\t%s\n",
$tm->hour, $tm->min, $tm->sec, $tm->year+1900,
$tm->mon+1, $tm->mday, $ENV{'REMOTE_ADDR'}, $query->param('q'));
close(LOG);
}
}
# Check the outcome of the response
if ($res->is_success) {
my $jsreply = $res->content;
if (($query->param('page') == 1) && !$query->param('nw')) {
# save query in cookie
my $cookie = new CGI::Cookie(-name=>'jobsearch1',
-value=>{ JobType => $query->param('JobType'),
d => $query->param('d') },
-expires => '+30d'
);
my $cookie2 = new CGI::Cookie(-name => 'q',
-value => $query->param('q'),
-expires => '+30d'
);
print header(-cookie=>[$cookie, $cookie2]);
} else { print header; }
print &output_listing($jsreply);
# print "Query: " . $query->param('JobType') . " Q:" . $query->param('q') . "
"; # DEBUG
# print $queryurl; # DEBUG
} else {
&output_html('Request to jobserve failed');
}
} elsif ($query->param('jobid')) {
# details page
print header, &detailspage($query->param('jobid'));
} elsif ($query->param('jsform')) {
# form page
if ($query->param('jobidbook')) {
# bookmark to add
if ($query->param('jobidbook') == -1) {
# if you get passed -1, wipe the bookmarks cookie
%jobidhash = {};
} else {
$jobidhash{$query->param('jobidbook')} = $query->param('jobtitle');
}
my $cookie3 = new CGI::Cookie(-name=> 'jobidbook',
-value=> \%jobidhash,
# set negative exp. date if they have to be deleted
-expires => keys(%jobidhash) ? '+30d' : '-30d'
);
print header( -cookie => $cookie3), &jsform;
} else {
print header, &jsform;
}
} elsif ($query->param('blank')) {
&output_html('');
} else {
# frameset is default now
print header, &frameset;
}
# {{{ openwindow
sub openwindow {
my $rettxt = shift; # any stuff that comes before
$rettxt .= (!$query->param('nw')) ? sprintf(' New Window',
self_url . '&nw=1') : '';
return $rettxt;
}
# }}}
# {{{ output_html
sub output_html {
my $txt = shift;
print header;
print start_html(-title=>'Jobserve Reformatter Results', -bgcolor=>'white'),
$txt,
end_html;
}
# }}}
# rewrite details page
# {{{ detailspage
sub detailspage {
my $jobid = shift;
my $ua = new LWP::UserAgent;
$ua->agent("AgentName/0.1 " . $ua->agent);
# Create a request
my $queryurl = sprintf("http://www.it.jobserve.com/jobserve/JobDetail.asp?jobid=%s", $jobid);
my $req = new HTTP::Request GET => $queryurl;
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
# Check the outcome of the response
my $jsreply;
if ($res->is_success) {
$jsreply = $res->content;
}
# strip header & footer
$jsreply =~ s#^.*
$bookmarks | [Reset] |