oldzy
June 20, 2012, 2:41am
1
I am trying to setup a perl script to read/display a JSON feed from Indeed. It works fine in a PHP script, but not this setup. I need to use Perl in this instance, and also using Text::FastTemplate. Any ideas? I am fairly new to Perl. Thanks.
#!/usr/bin/perl
use JSON qw( decode_json );
use Data::Dumper;
use LWP::Simple;
use Text::FastTemplate;
$dir = "/templates/jobs";
$useragent = "$ENV{'HTTP_USER_AGENT'}";
$userip = "$ENV{'REMOTE_ADDR'}";
$url = "http://api.indeed.com/ads/apisearch?publisher=xxxxxxxx&q=&l=&sort=&radius=&st=&jt=&start=&limit=10&fromage=10&filter=1&latlong=1&co=us&chnl=&userip=$userip&useragent=$useragent&v=2&format=json";
$jobs = get($url);
$data = decode_json($jobs);
foreach $job ($data->{'results'}->['result']) {
$date = $job->{'date'};
$jobtitle = $job->{'jobtitle'};
$joburl = $job->{'url'};
$company = $job->{'company'};
my (%data) = (
'DATE' => $date,
'JOBTITLE' => $jobtitle,
'JOBURL' => $joburl,
'COMPANY' => $company
);
}
my $template = new Text::FastTemplate(file=> 'test.tpl.html', path=> $dir);
print "Content-Type: text/html\
\
";
print $template->output( %data );
1;
My other variation looks like this which does not work either…
#!/usr/bin/perl
use JSON qw( decode_json );
use Data::Dumper;
use LWP::Simple;
use Text::FastTemplate;
$dir = "/templates/jobs";
$useragent = "$ENV{'HTTP_USER_AGENT'}";
$userip = "$ENV{'REMOTE_ADDR'}";
$url = "http://api.indeed.com/ads/apisearch?publisher=xxxxxxxx&q=&l=&sort=&radius=&st=&jt=&start=&limit=10&fromage=10&filter=1&latlong=1&co=us&chnl=&userip=$userip&useragent=$useragent&v=2&format=json";
$jobs = get($url);
$data = decode_json($jobs);
foreach $job ($data->{'results'}->['result']) {
$date = $job->{'date'};
$jobtitle = $job->{'jobtitle'};
$joburl = $job->{'url'};
$company = $job->{'company'};
$jobz[$#jobz + 1] = {
'JOB' => 1,
'DATE' => $date,
'JOBTITLE' => $jobtitle,
'JOBURL' => $joburl,
'COMPANY' => $company
};
}
my $template = new Text::FastTemplate(file=> 'test.tpl.html', path=> $dir);
my (%data) = (
'JOBZ' => $jobz
);
print "Content-Type: text/html\
\
";
print $template->output( %data );
1;
test.tpl.html
<html>
<body>
<table>
#for ##JOBZ##
#if ##JOB##
<tr>
<td>##DATE##</td>
<td><a href="##JOBURL##">##JOBTITLE##</a></td>
<td>##COMPANY##</td>
</tr>
#endif
#endfor
</table>
</body>
</html>
oldzy
June 20, 2012, 3:15am
2
Just a note that the below ‘works’, although it only displays the first record. print Dumper $data show all records fine,
#!/usr/bin/perl
use JSON qw( decode_json );
use Data::Dumper;
use LWP::Simple;
use Text::FastTemplate;
$dir = "/templates/jobs";
$useragent = "$ENV{'HTTP_USER_AGENT'}";
$userip = "$ENV{'REMOTE_ADDR'}";
$url = "http://api.indeed.com/ads/apisearch?publisher=xxxxxxxx&q=&l=&sort=&radius=&st=&jt=&start=&limit=10&fromage=10&filter=1&latlong=1&co=us&chnl=&userip=$userip&useragent=$useragent&v=2&format=json";
$jobs = get($url);
$data = decode_json($jobs);
foreach $job ($data->{'results'}->['result']) {
$date = $job->{'date'};
$jobtitle = $job->{'jobtitle'};
$joburl = $job->{'url'};
$company = $job->{'company'};
}
my $template = new Text::FastTemplate(file=> 'test.tpl.html', path=> $dir);
my (%data) = (
'DATE' => $date,
'JOBTITLE' => $jobtitle,
'JOBURL' => $joburl,
'COMPANY' => $company
);
print "Content-Type: text/html\
\
";
print $template->output( %data );
1;
oldzy
June 20, 2012, 3:22am
3
print Dumper $data shows results like this…
$VAR1 = {
'location' => '',
'query' => 'test',
'version' => 2,
'end' => 1,
'dupefilter' => bless( do{\\(my $o = 1)}, 'JSON::backportPP::Boolean' ),
'pageNumber' => 0,
'highlight' => $VAR1->{'dupefilter'},
'totalResults' => 68632,
'results' => [
{
'date' => 'Fri, 15 Jun 2012 00:55:54 GMT',
'url' => 'http://www.indeed.com/viewjob?......
'company' => 'Test & Measurement Fluke',
'jobtitle' => 'TEST ENGINEER I'
}
],
'start' => 1
};
$VAR2 = '
';
print Dumper $data->{‘results’} show results like this…
$VAR1 = [
{
'date' => 'Fri, 15 Jun 2012 00:55:54 GMT',
'url' => 'http://www.indeed.com/...
'company' => 'Test & Measurement Fluke',
'jobtitle' => 'TEST ENGINEER I'
}
];
$VAR2 = '
';
oldzy
June 20, 2012, 7:44am
4
Seems fine now, and is looping through all the results, and not just showing the first. I first moved the dynamic test.tpl.html code (deleting the #for and #if statements as well) into the foreach loop in the perl script itself using a ‘$job .=’ varname, then changed the foreach line to… foreach $job (@ {$data->{‘results’}}) { , and changed ‘my (%data)’ accordingly.
Now, just have to figure out how to properly shorten the long date string.
Sogo7
June 28, 2012, 12:21am
5
echo date("F, jS, Y", strtotime("2010-12-07 12:00:00"));
gives you … December, 7th, 2010
http://php.net/manual/en/function.date.php provides all the help for any format variations you may prefer.
but the basic trick is the strtotime string -to -time it takes the human readable time , converts to unix timestamp allowing it to be reformatted with the regular date command.
Just had a quick look at indeed.com … any idea how much they pay?
I ask as I’m running something similiar over at Lovelogic.net