|
楼主 |
发表于 2011-8-29 22:39:59
|
显示全部楼层
ls不好意思,三楼的代码有些问题。一楼的应该能正常用。我改进了下,你看现在这个能用不?- #!/usr/bin/perl -w
- use strict;
- use Digest::MD5 qw/md5_hex/;
- sub download {
- my ($id, $ans, $file, $dir) = @_, my $cnt = 0;
- for (my $i = 0; defined ($_ = $file->[$i]); $i++) {
- if (/^<\/stream>/) {
- $cnt++;
- }
- elsif ($cnt == $ans && /url="(.+)"/) {
- next if fork();
- while (1) {
- qx/wget -P $dir -U Xeslaro -t 3 '$1'/;
- exit unless $?;
- @$file = api_rest($id);
- $file->[$i] =~ /url="(.+)"/;
- }
- }
- }
- while (wait() != -1) {
- }
- }
- sub api_rest {
- $_ = shift;
- /id_(.+)\.html/;
- while (1) {
- my $time = time();
- my $auth = md5_hex((time().' XOA== MWZlNWE4Y2Q4OWQ0NjEyMWJjZTJmMWNiYTVhNzQwZGM='));
- my $url = "http://api.youku.com/api_rest?method=video.getvideofile&pid=XOA==&ctime=$time&auth=$auth&videoid=$1";
- my @file = qx/wget -U Xeslaro -O - -t 1 '$url'/;
- return @file if (!$? && grep(/<\/stream>/, @file) >= 1);
- }
- }
- foreach (@ARGV) {
- my $dir = "/tmp/youku".time();
- qx{
- if [[ ! -d $dir ]]; then
- mkdir $dir
- else
- rm -r $dir
- fi
- };
- my @file = api_rest($_), my $name, my $cnt=0, my $ans=0, my $min;
- my %rank = (
- hd2 => 0,
- mp4 => 1,
- flv => 2,
- flvhd => 3,
- '3gphd' => 4,
- '3gp' => 5,
- );
- foreach (@file) {
- if (/CDATA\[(.+)\]\]/) {
- $name = $1;
- }
- elsif (/type="(.+)">/) {
- if (!$cnt) {
- $min = $rank{$1};
- }
- elsif ($rank{$1} < $min) {
- $ans = $cnt;
- }
- $cnt++;
- }
- }
- if (fork()) {
- print "downloading $name begins...\n";
- next;
- }
- download($_, $ans, \@file, $dir);
- @file = qx/ls -l $dir\/*.*/;
- $file[0] =~ /\.(.+)$/;
- if (@file > 1) {
- qx/mencoder $dir\/*.* -oac mp3lame -ovc copy -of lavf -lavfopts format=$1 -o '$name'.$1/;
- }
- else {
- qx/mv $dir\/*.* '$name'.$1/;
- }
- qx/rm -r $dir/;
- print "$name downloaded...\n";
- exit;
- } continue {
- sleep 1;
- }
- while (wait() != -1) {
- }
复制代码 |
|