Perl script to test all posible dates backwards

Viewed 75

I have found this code online (Source)to download bing images.

This perl script download the todays Bing wallpaper image, how I can change it to test all possible dates backwards? It dosn't matter when it stops because I'm gonna stop manually.

I tried some options but I don't know perl and it's getting a little frustrating.

Thanks a lot!

#!/usr/bin/perl
$\="\n";
use strict;
use warnings;
use Data::Dumper;
use LWP::Simple;
use JSON;
use POSIX qw/strftime/;
 
my $base_url = "http://www.bing.com/";
my $json_url = "http://www.bing.com/HPImageArchive.aspx?format=js&n=1&mkt=en-US";
my $output_dir = "C:\\Downloads\\Wallpaper";
 
my $content = get($json_url)
  or die "Failed to retrieve $json_url! $! $@";
  
my $json = from_json($content)
  or die "Failed to parse json response! $! $@";
  
my $image_url = $base_url . $json->{images}[0]->{url};
print "Today's Image: $image_url";
 
my $date = strftime('%Y_%m_%d',localtime);
my $filename = "$output_dir\\$date.jpg";
print $filename;
 
my $status = getstore($image_url,$filename);
if($status == 200)
{
  print "HTTP Response OK.";
  my $size = -s $filename;
  if($size > 0)
  {
    print "Retrieved $size bytes.";
  }
  elsif($size == 0)
  {
    print "Seems we have retrieved a zero-byte image.";
  }
}
else
{
  print "HTTP Response: $status";
}```


2 Answers

Use a module for date-time manipulations. DateTime is one natural choice, as the most complete and rounded date-time library, with a very consistent interface and behavior.

Here is a very crude example, iterating over days backwards

use warnings;
use strict;
use feature 'say';

use DateTime;
use DateTime::Format::Strptime;

my $date_stop = shift // '2021-02-01';  # input format: yyyy-mm-dd 

my $dt = DateTime->today(time_zone => 'local');

my $dt_stop = DateTime::Format::Strptime->new(
    pattern => '%Y-%m-%d',
    time_zone => 'local',
    on_error => 'croak',
)->parse_datetime($date_stop)->truncate(to => 'day');


while ($dt >= $dt_stop) { 
    say $dt->ymd('-');
    $dt->subtract(days => 1); 
}

While this works as it stands there are some features and subtleties hiding in there. See docs.

I use local timezone; adjust as suitable. I truncate to days, setting time components to zero, by using today (which does that) and by explicitly truncating the object created from the format. Adjust if you need/want times, and note that this does affect comparisons (and calculations) so consider carefully what you need.

I use DateTime::Format::Strptime to show how you can use a variety of formats (almost all from strptime(3)) to initiate objects, what DateTime proper doesn't avail us.

This perl script download the todays Bing wallpaper image, how I can change it to test all possible dates backwards?

I read this as: I would like to download the images for the past days.
Unfortunately only the last 8 images are provided here. - You use a n big enough in the initial request, then loop over the results.
The dates are in the json already, so there is no need for a date module.

#!/usr/bin/perl
$\="\n";
use strict;
use warnings;
use Data::Dumper;
use LWP::Simple;
use JSON;
use POSIX qw/strftime/;
use Data::Dumper;
my $base_url = "http://www.bing.com/";
my $json_url = "http://www.bing.com/HPImageArchive.aspx?format=js&n=100&mkt=en-US";
my $output_dir = "C:\\Downloads\\Wallpaper";
 
my $content = get($json_url)
  or die "Failed to retrieve $json_url! $! $@";
  
my $json = from_json($content)
    or die "Failed to parse json response! $! $@";
print Dumper $json;

my $images = $json->{images};
for my $image (@$images) {
    my $image_url = $base_url . $image->{url};

    my $date = $image->{startdate};
    $date =~ s/(\d{4})(\d{2})(\d{2})/$1_$2_$3/;
    my $filename = "$output_dir\\$date.jpg";
    print $filename;
 
    my $status = getstore($image_url,$filename);
    if ($status == 200) {
        print "HTTP Response OK.";
        my $size = -s $filename;
        if ($size > 0) {
            print "Retrieved $size bytes.";
        } elsif ($size == 0) {
            print "Seems we have retrieved a zero-byte image.";
        }
    } else {
        print "HTTP Response: $status";
    }
}
Related