级别: 新手上路
- 注册时间:
- 2006-09-02
- 在线时间:
- 0小时
- 发帖:
- 1
|
贴一个自己写的下载163匿名相册的程序,用perl写的,只提供命令行界面。 如果会perl的话,改起来或者增加功能都会很方便的,想要写图形界面应该也不难。 程序包括2个文件,一个是主程序(simple.pl),另外的一个是支持 主程序的库Album163.pm。 假如要下载的相册地址是 http://photo.163.com/photos/downaly/61372897/那么运行命令 simple.pl downaly 61372897 就可以了。 文件simple.pl的内容如下: #!/usr/bin/perl -w use strict; use LWP::UserAgent; use Album163; my ($uid, $aid) = @ARGV; unless (defined $uid) { print "input user name:"; $uid = ; chomp $uid; print "user name is '$uid'\n"; }
unless (defined $aid) { print "input album id:"; $aid = ; chomp $aid; print "album id is '$aid'\n"; }
my $a=Album163->new; #my $num=$a->get_info('galaxyangel', '54569921'); my $num=$a->get_info($uid, $aid);
my $dir=$a->get_album_info("title"); mkdir $dir;
print "will get $num files in '$dir'\n";
foreach (0 .. $num-1) { $a->save_image($_,$dir) and print "get part $_ OK\n"; } 1;
# end of file
文件Album163.pm的内容: # Copyright (C) 2006 reiv@rygh # # This library is free software; you can redistribute it and/or modify it under # the terms of the GNU Library General Public License as published by the Free # Software Foundation
package Album163; use strict; use LWP::UserAgent; use Encode;
use Data::Dumper;
sub new { my $class=shift;
my $uid=shift if @_; my $aid=shift if @_;
my $ua = LWP::UserAgent->new; $ua->timeout(10); $ua->env_proxy();
my $self = { UA => $ua, ALBUM => undef, INFO => undef, }; bless $self,$class; }
# uid and aid sub get_info($$) { my $self=shift; my ($uid, $aid)=@_;
my $ua=$self->{UA}; my $re=$ua->get("http://photo.163.com/js/photosinfo.php?user=$uid&aid=$aid"); my $js=encode('utf8',decode('gbk',$re->content));
my @a = split /\r?\n/, $js; my %album_info; my @id=(); my @data=(); foreach (@a) { chomp; next unless $_; my $tmp; # get Album info if (/var\s+gAlbumInfo\s*=\s*{(.+)}\s*/) { $tmp=$1; next unless $tmp; while ($tmp=~m/\s*'(.+?)':(\"?)(.+?)\2,?/g) { #print $1," => ",$3,"\n"; my ($key,$val)=($1,$3); $val=~s/^\s+//; $val=~s/\s+$//; $album_info{$key}=$val; } } elsif (/var\s+gPhotosIds\s*=\s*\[(.*)\]\s*;/) { $tmp=$1; next unless $tmp; while ($tmp=~m/(\d+),?/g) { push @id, $1 if $1; } } elsif (/var\s+datas\s*=\s*\[(.*)]\s*\;/) { $tmp=$1; next unless $tmp; while ($tmp=~m/\[(.+?),(.+?),\"(.+?)\",\"(.+?)\"\],?/g) { push @data, [$1, $2, $3, $4]; } } } return if (($#id != $#data) or ($#id == 0));
$self->{ALBUM}=\%album_info;
my @info=(); foreach (0 .. $#id) { my $sid=$data[$_]->[0]; my $ext=$data[$_]->[1]?"jpg":"gif"; my $pid=$id[$_]; my $url="http://img$sid.photo.163.com/$uid/$aid/$pid.$ext"; push @info, [ $url, @{$data[$_]} ]; } $self->{INFO}=\@info;
print Dumper(\%album_info); #print Dumper(\@info); $#info+1; }
# 1: the index of image, from 0~max # 2: the path to save, optional # 3: the file name to save, optional sub save_image { my $self=shift; return undef unless defined $self->{INFO}; return undef unless @_; my $idx=shift; my $url=$self->get_url($idx) or return 0;
my ($path, $name)=(".", $self->{INFO}[$idx][4]); $path = shift if @_; $name = shift if @_;
my $ua=$self->{UA}; $ua->get($url, ":content_file" => $path."/".$name); }
sub get_album_info($) { my $self=shift; my $key=shift; $self->{ALBUM}{$key}; }
# get url of image sub get_url($) { my $self=shift; return unless defined $self->{INFO}; my $idx=shift; return undef if ($idx<0 or $idx>$#{$self->{INFO}});
$self->{INFO}[$idx][0]; }
1; # end of file
|