/home/pas/bin/get_bingpics


#!/usr/bin/perl
#
# get Bing pics not previously downloaded
#

use strict;
use warnings;
use LWP::Simple;
use JSON::PP;
use Const::Fast;
use URI;

use version; our $VERSION = qv('v2017.11.10');

const my $NPICS     => 16;
const my $BING      => 'http://bing.com';
const my $BING_JSON => $BING
    . '/HPImageArchive.aspx?format=js&idx=0&n='
    . $NPICS
    . '&mkt=en-US';
const my $PICDIR    => '/media/sf_Pictures';

my $bing_json = get $BING_JSON;
defined $bing_json or die "Getting $BING_JSON failed, sorry.\n";
my $img_ref = decode_json($bing_json)->{images};

my $result;
for my $img ( @{$img_ref} ) {
    my $bing_img_url = $BING . $img->{'url'};
    my $pic_filename = pop @{ [ URI->new($bing_img_url)->path_segments ] };
    print "$pic_filename ... ";
    if ( !-e "$PICDIR/$pic_filename" ) {
        print "Downloading\n";
        ( $result = getstore( $bing_img_url, "$PICDIR/$pic_filename" ) )
            || die "Getstore of $bing_img_url failed: $result\n";
    }
    else {
        print "previously downloaded\n";
    }
}