7 #sub POE::Kernel::ASSERT_DEFAULT () { 1 }
8 use POE
qw(Component::Client::HTTP);
13 use Digest
::MD5
'md5_hex';
14 use Time
::HiRes
'time';
16 # TODO: allow loading all those variables from a configuration file
18 # Required for using the script. If you're using this script,
19 # you already know what these are for and what you should put in here
23 # HTTP proxy. Must be in the form 'http://host:port'.
24 # Port is required even if 80.
25 # May work for SOCKS proxies by using the 'socks://' protocol, untested.
26 my $proxy = $ENV{http_proxy
};
30 # The main purpose of this script.
31 # Only files that pass any of the allow filters and fail all of the deny filters
34 # These are hashes to arrayrefs. Each key is used to filter a field in the $info
35 # hashref. Each element of the arrayrefs is treated as a case-sensitive regexp.
36 # The fields are title, url and category. There are other fields, but they are
37 # not really filterable through this method. You are welcome to try though.
38 # Example: title => ['Kara_no_Kyoukai', 'Black Lagoon\[niizk\]']
40 # Use [0-9A-Fa-f]{8} for CRC32s
41 # Use \d{2}v?\d? for episode numbers (catches v2s and v3s as well)
42 # Remember to escape all '.', '[' and ']' on filenames with a '\'.
43 my %file_allow_filters = ( title
=> [] );
44 my %file_deny_filters = ( url
=> [] );
46 # HTTP Digest auth hack
47 my $realm = "DeathWolf's Auto-Torrent Loving Realm";
48 my $A1 = md5_hex
("$username:$realm:$password");
49 my $A2 = md5_hex
("GET:/~DeathWolf/Recent/Latest-Files.html");
50 my $cnonce = int(time());
56 $www_auth =~ /nonce=\"(.*?)\"/;
59 my $header = "Digest username=\"$username\", ";
60 $header .= "realm=\"$realm\", nonce=\"$nonce\", ";
61 $header .= "uri=\"/~DeathWolf/Recent/Latest-Files.html\", ";
62 $header .= "qop=\"auth\", nc=$reqid, cnonce=\"$cnonce\", ";
63 my $resp_digest = md5_hex
("$A1:$nonce:$reqid:$cnonce:auth:$A2");
64 $header .= "response=\"$resp_digest\"";
68 use constant CHUNK_SIZE
=> 16384;
70 POE
::Component
::Client
::HTTP
->spawn(
73 Timeout
=> 45, # TODO: make it configurable
74 Streaming
=> CHUNK_SIZE
,
79 my $str = decode_utf8
(shift);
80 use bytes
; $str =~ s/%([0-9A-Fa-f]{2})/@{[chr(hex($1))]}/g; no bytes
;
81 decode_entities
($str);
85 ### Windows Unicode support wrappers
88 ### NOTE: ALL file manipulation files defined here *ONLY WORK WITH ABSOLUTE PATHS*
90 require Win32API
::File
; Win32API
::File
->import qw(:FuncW :Func :MOVEFILE_ :GENERIC_ :FILE_ FILE_END :FILE_SHARE_ OPEN_ALWAYS);
91 require Win32
::API
; Win32
::API
->import;
93 # Enable output of UTF-8 text
95 my $SetConsoleOutputCP = Win32
::API
->new('kernel32.dll', 'SetConsoleOutputCP',
96 'I', '') or die "SetConsoleOutputCP: $^E";
98 # if ($SetConsoleOutputCP->Call(65001)) {
99 # die "Error " . Win32::GetLastError () . " - " . Win32::FormatMessage (Win32::GetLastError ());
102 my $GetStdHandle = Win32
::API
->new('kernel32.dll', 'GetStdHandle',
103 'I', 'I') or die "GetStdHandle: $^E";
104 my $WriteConsoleW = Win32
::API
->new('kernel32.dll', 'WriteConsoleW',
105 'IPIPP', 'I') or die "WriteConsoleW: $^E";
107 use constant STD_OUTPUT_HANDLE
=> 0xfffffff5;
109 my $stdout = $GetStdHandle->Call(STD_OUTPUT_HANDLE
);
111 # Use Win32::API to load needed functions not defined by Win32API::File
113 use constant ERROR_NO_MORE_FILES
=> 18;
114 use constant INVALID_HANDLE_VALUE
=> -1;
116 typedef Win32
::API
::Struct FILETIME
=> qw{
118 DWORD dwHighDateTime
;
121 use constant FILE_ATTRIBUTE_READONLY
=> 0x00000001;
122 use constant FILE_ATTRIBUTE_HIDDEN
=> 0x00000002;
123 use constant FILE_ATTRIBUTE_SYSTEM
=> 0x00000004;
124 use constant FILE_ATTRIBUTE_DIRECTORY
=> 0x00000010;
125 use constant FILE_ATTRIBUTE_ARCHIVE
=> 0x00000020;
126 use constant FILE_ATTRIBUTE_NORMAL
=> 0x00000080;
127 use constant FILE_ATTRIBUTE_TEMPORARY
=> 0x00000100;
128 use constant FILE_ATTRIBUTE_COMPRESSED
=> 0x00000800;
129 use constant MAX_PATH
=> 260;
131 typedef Win32
::API
::Struct WIN32_FIND_DATAW
=> qw{
132 DWORD dwFileAttributes
;
133 FILETIME ftCreationTime
;
134 FILETIME ftLastAccessTime
;
135 FILETIME ftLastWriteTime
;
140 WCHAR cFileName
[520];
141 WCHAR cAlternateFileName
[28];
142 }; # 4 + 8 x 3 + 4 x 4 + 520 + 28 = 592 bytes
144 my $FindFirstFileW = Win32
::API
->new('kernel32.dll', 'FindFirstFileW',
145 'PS', 'N') or die "FindFirstFile: $^E";
146 my $FindNextFileW = Win32
::API
->new('kernel32.dll', 'FindNextFileW',
147 'NS', 'I') or die "FindNextFile $^E";
148 my $FindClose = Win32
::API
->new('kernel32.dll', 'FindClose',
149 'N', 'I') or die "FileClose $^E";
151 typedef Win32
::API
::Struct SHFILEOPSTRUCT
=> qw{
157 BOOL fAnyOperationsAborted
;
158 LPVOID hNameMappings
;
159 LPCTSTR lpszProgressTitle
;
162 my $SHFileOperationW = Win32
::API
->new('shell32.dll', 'SHFileOperationW',
163 'S', 'I') or die "SHFileOperation: $^E";
165 my $CreateDirectoryW = Win32
::API
->new('kernel32.dll',
166 'BOOL CreateDirectoryW(LPCWSTR lpPathName, LPVOID lpSecurityAttributes);') or die "CreateDirectoryW: $^E";
168 my $RemoveDirectoryW = Win32
::API
->new('kernel32.dll',
169 'BOOL RemoveDirectoryW(LPCWSTR lpPathName);') or die "RemoveDirectoryW: $^E";
173 return encode
("UTF-16LE", decode_utf8
("$str\0"));
180 return $count if $WriteConsoleW->Call($stdout, $str, (bytes
::length($str)-1)/2, $count, []);
188 unless (myexists
($path)) {
190 unless ($CreateDirectoryW->Call($enc->("$p"), undef)) {
192 $parent =~ s/\/[^\/]*$//;
193 if ($parent eq $path) {
196 unless (mymkpath
($parent)) {
199 # Parent made, try again making the child
200 unless ($CreateDirectoryW->Call($enc->("$p"), undef)) {
211 return GetFileAttributesW
($enc->("$p")) != fileConstant
("INVALID_FILE_ATTRIBUTES");
216 return 0 unless myexists
($p);
218 my $handle = CreateFileW
($enc->("$p"), 0, fileConstant
("FILE_SHARE_READ") |
219 fileConstant
("FILE_SHARE_WRITE"), [],
220 fileConstant
("OPEN_EXISTING"), 0, []);
221 my $size = getFileSize
($handle);
222 CloseHandle
($handle);
230 $file = $enc->("$file");
232 die "Not implemented";
234 my $handle = CreateFileW
($file, $mode eq '>>' ? FILE_APPEND_DATA
() : GENERIC_WRITE
(), FILE_SHARE_READ
(), [], OPEN_ALWAYS
(), 0, []);
235 setFilePointer
($handle, 0, FILE_END
());
243 return WriteFile
($handle, $data, 0, $outbytes, []) ?
$outbytes : undef;
251 DeleteFileW
($enc->(shift));
256 *myprint
= sub { print shift; };
257 *mymkpath
= sub { mkpath
(shift); };
258 *myexists
= sub { -e
shift; };
259 *mysize
= sub { -s
shift; };
260 *myopen
= sub { open ($_, shift, shift); };
261 *mysyswrite
= sub { syswrite(shift, shift); };
262 *myclose
= sub { close(shift); };
263 *myunlink
= sub { unlink(shift); };
266 POE
::Session
->create(
268 # Makes initial request for the Digest authentication
270 my ($kernel, $heap) = @_[KERNEL
, HEAP
];
271 my $req = HTTP
::Request
->new(GET
=> 'http://saber.kawaii-shoujo.net/~DeathWolf/Recent/Latest-Files.html');
272 $heap->{progress_message
} = "Requesting List...";
273 myprint
($heap->{progress_message
} . " ");
274 $kernel->post('ua', 'request', 'authenticate', $req, 0);
277 $_[KERNEL
]->post('ua', 'shutdown');
279 # Authenticates and theen actually fetch the list
280 authenticate
=> sub {
281 my $res = $_[ARG1
]->[0];
282 return if defined $_[ARG1
]->[1]; # Wait until the error page finishes downloading because of streaming
283 my $req = HTTP
::Request
->new(GET
=> 'http://saber.kawaii-shoujo.net/~DeathWolf/Recent/Latest-Files.html');
284 die $res->status_line unless ($res->status_line =~ /^(401|2)/);
285 unless ($res->is_success) {
286 my $header = make_digest
($res->header('WWW-Authenticate'));
287 $req->header('Authorization', $header);
289 $_[KERNEL
]->post('ua', 'request', 'stream_latest_files', $req, $reqid++, 'progress');
292 my $msg = $_[HEAP
]->{progress_message
};
293 my ($done, $left) = @
{$_[ARG1
]}[0,1];
294 myprint
(sprintf("%3.2f%%\r$msg ", $done * 100 / $left));
295 myprint
("\n") if $done == $left;
297 # Puts the HTML in a temporary var
298 stream_latest_files
=> sub {
299 my ($heap, $res, $data) = ($_[HEAP
], @
{$_[ARG1
]});
301 die $res->status_line unless ($res->is_success);
304 $heap->{latest_files_data
} = "" unless exists $heap->{latest_files_data
};
305 $heap->{latest_files_data
} .= $data;
307 $_[KERNEL
]->call($_[SESSION
], 'parse_latest_files');
308 $_[KERNEL
]->yield('load_completed_files');
309 $_[KERNEL
]->yield('filter_files');
312 # Parses the HTML, puts hashrefs in $_[HEAP]->{latest_files}
313 parse_latest_files
=> sub {
316 my $content = $heap->{latest_files_data
};
317 delete $heap->{latest_files_data
};
321 # TODO: use a XML parser, saber now uses XHTML 1.0
322 my @lines = split m/\n/, $content;
327 if (m!class="date".*?>(.+)</td>!) {
329 } elsif (m!class="category".*?>(.+)</td>!) {
330 $info->{category
} = $1;
331 } elsif (m!class="size".*?>(.+)</td>!) {
333 } elsif (m!<td><a href="(.+)">(.+)</a></td>!) {
335 $info->{title
} = entity_clean
($2);
337 } elsif (m!^\s*</tr>! and $info->{url
}) {
343 $heap->{latest_files
} = \
@files;
345 # Loads the list of completed files
346 load_completed_files
=> sub {
348 $heap->{completed_files
} = [];
349 if (myexists
("completed_files.txt")) {
350 open my $in, "<", "completed_files.txt";
353 push @
{$heap->{completed_files
}}, decode_utf8
($_);
358 mark_as_completed
=> sub {
359 open my $completed_files, ">>", "completed_files.txt";
360 print $completed_files encode_utf8
($_[ARG0
] . "\n");
361 close $completed_files;
363 # Filters the file list
364 filter_files
=> sub {
365 # TODO: allow filtering by batch contents (subfolders)
366 # TODO: allow filtering by min / max filesize
368 $heap->{allowed_files
} = [];
370 my $filecount = scalar(@
{$heap->{latest_files
}});
372 foreach my $info (@
{$heap->{latest_files
}}) {
373 myprint
("\rFiltering " . $i++ . "/$filecount... ");
375 foreach (@
{$heap->{completed_files
}}) {
376 if ($info->{title
} eq $_) {
383 foreach my $group (keys %file_allow_filters) {
384 foreach my $filter (@
{$file_allow_filters{$group}}) {
385 if (entity_clean
($info->{$group}) =~ /$filter/) {
393 foreach my $group (keys %file_deny_filters) {
394 foreach my $filter (@
{$file_deny_filters{$group}}) {
395 if (entity_clean
($info->{$group}) =~ /$filter/) {
400 last unless $allowed;
403 push @
{$heap->{allowed_files
}}, $info if $allowed;
405 delete $heap->{latest_files
};
406 $_[KERNEL
]->yield('sort_allowed_files');
407 $_[KERNEL
]->yield('print_allowed_files');
408 $_[KERNEL
]->yield('download_entries');
410 sort_allowed_files
=> sub {
411 # TODO: actually sort them
412 # NOTE: the list is used in reverse order
414 print_allowed_files
=> sub {
416 my $a = @
{$heap->{allowed_files
}} > 0 ?
":" : ".";
417 myprint
(@
{$heap->{allowed_files
}} . " entries accepted$a\n");
418 foreach (@
{$heap->{allowed_files
}}) {
419 myprint
(" \"" . $_->{title
} . ($_->{url
} =~ m!/$! ?
"/" : "") . "\" (" . $_->{size
} . ")\n");
422 # This state is called after each file finishes downloading
423 download_entries
=> sub {
424 my ($kernel, $session, $heap) = @_[KERNEL
, SESSION
, HEAP
];
425 my $info = pop @
{$heap->{allowed_files
}};
427 if ($info->{url
} =~ /\/$/) {
428 $kernel->call($session, 'start_download_recursive', $info);
430 $kernel->call($session, 'download_file', $info);
434 # Downloads folder listings
435 start_download_recursive
=> sub {
436 # TODO: write the folder name to completed_files.txt after getting all files
437 # and skip if the folder name is found
440 my $req = HTTP
::Request
->new(GET
=> $info->{url
});
441 $_[HEAP
]->{info
} = $info;
442 $_[HEAP
]->{progress_message
} = "Listing \"". $info->{title
} ."/\":";
443 $_[KERNEL
]->call('ua', 'request', 'stream_subfolder', $req, $reqid++, 'progress');
444 while (defined $_[HEAP
]->{info
}) { $_[KERNEL
]->run_one_timeslice(); }
445 $_[KERNEL
]->yield('download_entries');
447 stream_subfolder
=> sub {
448 my ($heap, $res, $data) = ($_[HEAP
], @
{$_[ARG1
]});
449 my $info = $heap->{info
};
451 die $res->status_line unless ($res->is_success);
453 # HACK: I dunno why I use md5_hex, but it looked like a good idea at the time
455 $heap->{"folder-" . md5_hex
($info->{title
})} = "" unless exists $heap->{"folder-" . md5_hex
($info->{title
})};
456 $heap->{"folder-" . md5_hex
($info->{title
})} .= $data;
458 $_[KERNEL
]->yield('download_recursive');
461 # Parses folder listings, pushes subitems to allowed_files, folders last
462 # This makes subfolders be checked before files
463 download_recursive
=> sub {
464 my ($heap) = $_[HEAP
];
466 my $info = $heap->{info
};
467 my @lines = split m/\n/, $heap->{"folder-" . md5_hex
($info->{title
})};
471 if (m!<td class="n"><a href="(.*?)">.*<td class="t">Directory</td>!) {
474 } elsif (m!<td class="n"><a href=".*?">(.*?)</a></td>.*<td class="s">(.*?)</td>!) {
475 push @files, [$1, $2];
479 while ($_ = pop @files) {
480 my $finfo = {%{$info}};
481 $finfo->{title
} = $info->{title
};
482 $finfo->{title
} .= '/' unless $finfo->{title
} =~ m
!/$!;
483 $finfo->{title
} .= entity_clean
($_->[0]);
484 $finfo->{url
} = $info->{url
} . $_->[0],
485 $finfo->{size
} = $_->[1];
488 foreach (@
{$heap->{completed_files
}}) {
489 if ($finfo->{title
} eq $_) {
494 push @
{$heap->{allowed_files
}}, $finfo unless $skip;
496 # There is no trailing slash on the title
497 # There IS a trailing slash on folder URLs
499 my $subinfo = {%{$info}}; # make a copy
500 $subinfo->{title
} = $info->{title
};
501 $subinfo->{title
} .= '/' unless $subinfo->{title
} =~ m
!/$!;
502 $subinfo->{title
} .= entity_clean
($_);
503 $subinfo->{title
} .= '/' unless $subinfo->{title
} =~ m
!/$!;
504 $subinfo->{url
} = $info->{url
} . $_;
507 foreach (@
{$heap->{completed_files
}}) {
508 if ($subinfo->{title
} eq $_) {
513 push @
{$heap->{allowed_files
}}, $subinfo unless $skip;
516 delete $heap->{"folder-" . md5_hex
($info->{title
})};
517 $heap->{info
} = undef;
518 delete $heap->{info
};
520 # Actually downloads files
521 download_file
=> sub {
522 my ($kernel, $heap, $info) = @_[KERNEL
, HEAP
, ARG0
];
524 my $req = HTTP
::Request
->new(GET
=> $info->{url
});
525 $req->header (Accept_Ranges
=> "bytes");
526 $heap->{have_size
} = mysize
($info->{title
});
527 if ($heap->{have_size
}) {
528 $req->header (Range
=> "bytes=". $heap->{have_size
} ."-");
530 $heap->{have_size
} = 0;
532 my $title = $info->{title
};
533 my $len = 123; # Length of the string on-screen (imprecise, don't know how to actually make it correct)
534 # TODO: make $len customizable and/or somehow detect from the terminal the optimal length
536 # Shorten the printed title to $len characters
537 if (length($title) > $len) {
538 my @paths = split('/', $title);
541 # Keep the main folder name intact if it isn't too long
542 if (length($paths[0]) < $len/2) {
543 $title .= shift(@paths) . "/";
544 $len -= length($title);
546 my $pathcount = @paths;
547 my $prelen = int($len/$pathcount/2) - 2;
548 my $postlen = $prelen + 1;
549 while ($_=(shift @paths)) {
550 if (length($_) <= $len/$pathcount) {
553 $title .= substr($_, 0, $prelen) . "..." . substr($_, -$postlen) . "/";
557 # Remove any trailing / (will have if the title shortening code runs)
559 $heap->{progress_message
} = "\"" . $title ."\" (". $info->{size
} ."):";
560 myprint
($heap->{progress_message
});
562 $heap->{filename
} = $info->{title
};
564 $heap->{last_time
} = time;
565 $heap->{start_time
} = time;
568 $_[KERNEL
]->delay('calculate_speed' => 2);
569 $_[KERNEL
]->call('ua', 'request', 'stream_file', $req, $reqid++, 'file_progress');
571 # Does the actual file downloading
573 my ($kernel, $heap, $req, $res, $data) =
574 (@_[KERNEL
, HEAP
, ARG0
], @
{$_[ARG1
]});
576 die $res->status_line unless $res->is_success or $res->status_line =~ /^416/;
578 if (defined($data)) {
579 unless ($heap->{outfh
} and $heap->{content_length
}) {
580 $heap->{filename
} =~ m!(.*)/!;
582 $heap->{outfh
} = myopen
('>>',$heap->{filename
});
583 $heap->{content_length
} = $res->header('Content-Length');
584 # This is probably wrong
585 if ($heap->{content_length
} == $heap->{have_size
}) {
586 myclose
($heap->{outfh
});
587 $kernel->call($_[SESSION
], 'mark_as_completed', $heap->{filename
});
588 $kernel->post('ua', 'cancel', $req);
589 $kernel->yield('download_entries');
592 unless ($heap->{outfh
} && mysyswrite
($heap->{outfh
}, $data)) {
596 unless ($res->is_success or $res->status_line =~ /^416/) { # Requested Range not satisfiable
597 die $res->status_line;
599 $kernel->call($_[SESSION
], 'mark_as_completed', $heap->{filename
});
600 myclose
($heap->{outfh
});
601 delete $heap->{outfh
};
602 $kernel->yield('download_entries');
605 calculate_speed
=> sub {
609 if ($now == $heap->{last_time
}) {
610 $_[KERNEL
]->delay('calculate_speed' => 0.5);
613 my $spd = $heap->{oct} / 1024 / ($now - $heap->{last_time
});
614 unless (defined $heap->{speeds
}) {
615 $heap->{speeds
} = [];
616 $heap->{speeds_i
} = 0;
617 for(my $i = 0; $i < 8; $i++) { $heap->{speeds
}->[$i] = $spd; }
619 $heap->{speeds
}->[$heap->{speeds_i
}++] = $spd;
620 $heap->{speeds_i
} %= @
{$heap->{speeds
}};
624 foreach (@
{$heap->{speeds
}}) { $heap->{spd
} += $_; }
625 $heap->{spd
} /= @
{$heap->{speeds
}};
627 $heap->{last_time
} = time;
628 $_[KERNEL
]->delay('calculate_speed' => 0.5);
630 calculate_avg_speed
=> sub {
634 $heap->{spd
} = $heap->{len
} / 1024 / ($now - $heap->{start_time
});
636 # Prints the progress report
637 file_progress
=> sub {
638 my ($heap, $pos, $len, $oct) = ($_[HEAP
], @
{$_[ARG1
]});
641 $_[KERNEL
]->call($_[SESSION
], 'calculate_avg_speed');
642 my $line = sprintf("100.00%% (avg. %4.2f kB/s)", $heap->{spd
});
643 myprint
("\r" . $heap->{progress_message
} . " $line" . (" " x
($heap->{prevlen
} - length($line))) . "\n");
644 delete $heap->{have_size
};
645 delete $heap->{prev_time
};
646 delete $heap->{prevlen
};
648 delete $heap->{speeds
};
649 $heap->{speeds_i
} = 0;
650 $_[KERNEL
]->delay('calculate_speed');
652 my $have_size = exists $heap->{have_size
} ?
$heap->{have_size
} : 0;
654 my $spd = $heap->{spd
} ?
$heap->{spd
} : undef;
656 $heap->{oct} += length($oct);
657 $heap->{len
} += $heap->{oct};
659 # $have_size may be a BigInt, use fixed point math / formatters
660 my $pct = ($pos + $have_size)*10000/($len + $have_size);
664 $line = sprintf("%2d.%02d%% (%4.2f kB/s)", $pct / 100, $pct % 100, $spd);
666 $line = sprintf("%2d.%02d%%", $pct / 100, $pct % 100);
668 $heap->{prevlen
} = length($line) unless defined $heap->{prevlen
} and $heap->{prevlen
} > length($line);
669 myprint
("$line" . (" " x
($heap->{prevlen
} - length($line))) . "\r" . $heap->{progress_message
} . " ");
670 $heap->{prevlen
} = length($line);