Perl 脚本向后测试所有可能的日期

问题描述

我在网上找到了这个代码 (Source) 来下载 bing 图片

这个 perl 脚本下载今天的 Bing 壁纸图像,我如何更改它以向后测试所有可能的日期?它什么时候停止并不重要,因为我会手动停止。

我尝试了一些选项,但我不知道 perl,这有点令人沮丧。

非常感谢!

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


解决方法

使用模块进行日期时间操作。 DateTime 是一种自然的选择,它是最完整、最全面的日期时间库,具有非常一致的界面和行为。

这是一个非常粗略的例子,向后迭代几天

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); 
}

虽然这行得通,但其中隐藏着一些功能和微妙之处。查看文档。

我使用 local 时区;适当调整。我 truncate 到天,通过使用 today (这样做)并通过显式截断从格式创建的对象,将时间分量设置为零。如果您需要/想要时间,请进行调整,并注意这确实会影响比较(和计算),因此请仔细考虑您的需求。

我使用 DateTime::Format::Strptime 来展示如何使用各种格式(几乎全部来自 strptime(3))来启动对象,而 DateTime 本身对我们没有用。

,

这个 perl 脚本下载今天的 Bing 壁纸图像,我如何更改它以向后测试所有可能的日期?

我读为:我想下载过去几天的图像。
不幸的是,这里只提供了最后 8 张图片。 - 您在初始请求中使用了足够大的 n,然后遍历结果。
日期已经在json中,所以不需要日期模块。

#!/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";
    }
}