Search code examples
perldownloadbing

Perl script to test all posible dates backwards


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";
}```



Solution

  • 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";
        }
    }