#!/usr/bin/perl -w
#BY Weigun http://blog.chinaunix.net/u2/70443/
use strict;
use LWP::Simple;
use Win32::OLE;
use Data::Dumper;
my $r_urls=&get_url_list; #hash
my $r_file_name = get_content($r_urls); #hash
&is_new($r_urls,$r_file_name);
my $r_dlurls=&parse($r_file_name); #hash
#print Dumper $r_dlurls,"\n";
my $thunder = Win32::OLE->new('ThunderAgent.Agent.1');
for my $key(keys %$r_dlurls)
{
&dl_by_xl($thunder,$r_dlurls,$key);
}
print "press any key to exit\n";
<STDIN>;
sub get_url_list
{
my $list = 'web.txt';
#my @urls;
my $h_urls={};
my $i=0;
open IN,"<",$list;
while(<IN>)
{
chomp;
next if /^#/;
next if /^\s+$/;
#push @urls,$_;
$h_urls->{++$i}=$_;
}
close IN;
#return \@urls;
return $h_urls;
}
sub get_content
{
my $r_urls=shift;
my $i=0;
my $name_hash={};
mkdir 'web',oct(0777) unless -e 'web' and -d _;
chdir 'web' or die "can't chdir:$!\n";
for(keys %$r_urls)
{
warn "get $_ error:$!\n" if getstore($r_urls->{$_},$_.'.html') != 200;
$name_hash->{$_}=$_.'.html';
}
return $name_hash;
}
sub parse
{
my $file_name = shift;
my $dlurl_hash={};
my $i=0;
for my $key(keys %$file_name)
{
open IN,"<",$file_name->{$key};
while(<IN>)
{
s/\s+//;
#chomp;
if(/.*a href=\'(.*)\'.*>(.*)<\/a><\/li><\/ul>/)
{
$dlurl_hash->{++$i}=$1;
#print $url_hash->{$2},"\n";
last;
}
else
{
next;
}
}
}
close IN;
return $dlurl_hash;
}
#my $code = mirror('http://www.yahoo.com.cn','/path/file.html');
#
#mirror函数将比较远程和本地文件的一致性,然后返回一个状态号,比如文件相同
#将返回304,如果本地文件同步成功将返回200。
#更新时间:2010-1-22 8:55:10 下载次数:加载中
sub is_new
{
my ($r_url,$r_name)=@_;
for my $key(keys %$r_name)
{
my $mod_time = &parse_date($r_name->{$key});
if($mod_time ne &get_date)
{
delete $r_name->{$key};
next;
}
}
}
sub parse_date
{
my $name=shift;
my $time;
open FH,"<",$name;
while()
{
if(/.*更新时间.*?\/b>(.*?)\s.*/)
{
$time=$1;
last;
}
}
close FH;
return $time;
}
sub get_date
{
my($day,$mon,$year)=(localtime)[3..5];
$day = ($day < 10)? "0$day":$day;
$mon = ($mon < 9)? ($mon+1):($mon+1);
$year += 1900;
return $year.'-'.$mon.'-'.$day;
}
sub dl_by_xl
{
my ($obj,$r_name,$key)=@_;
#下面是迅雷AddTask()的参数,我只用了3个,$url $filename $path
#my ($url,$filename,$comments,$refer_url,$start_mode,$only_from_origin,$origin_thread_count);
my ($url,$filename);
$url=$r_name->{$key};
($filename)=$url=~/.*\/(.*\..*)/;
#print $filename,"\n";
my $path='f:\\movies\\';
$obj->AddTask($url,$filename,$path,$url,$url,1,0,5) ;
$obj-> CommitTasks2(1);
print 'Task Commit
}
|