SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jun 2010
    Location
    Canada
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Indeed feed not working/displaying

    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.

    Code:
    #!/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\n\n";
    print $template->output( %data );
    1;
    My other variation looks like this which does not work either...

    Code:
    #!/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\n\n";
    print $template->output( %data );
    1;
    test.tpl.html
    HTML Code:
    <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>

  2. #2
    SitePoint Member
    Join Date
    Jun 2010
    Location
    Canada
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a note that the below 'works', although it only displays the first record. print Dumper $data show all records fine,

    Code:
    #!/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\n\n";
    print $template->output( %data );
    1;

  3. #3
    SitePoint Member
    Join Date
    Jun 2010
    Location
    Canada
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    print Dumper $data shows results like this...

    Code:
    $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...
    Code:
    $VAR1 = [
              {
                'date' => 'Fri, 15 Jun 2012 00:55:54 GMT',
                'url' => 'http://www.indeed.com/...
                'company' => 'Test & Measurement Fluke',
                'jobtitle' => 'TEST ENGINEER I'
              }
            ];
    $VAR2 = '
    ';

  4. #4
    SitePoint Member
    Join Date
    Jun 2010
    Location
    Canada
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    SitePoint Zealot Sogo7's Avatar
    Join Date
    May 2011
    Posts
    129
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    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
    Last edited by Mittineague; Jun 27, 2012 at 21:55. Reason: removing unnecessary link
    Lovelogic.net Personal Projects Pit - Spammers welcome


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •