#!/usr/bin/perl use strict; use IO::Socket; my $id = shift; my $book = shift; my $maxpage; sub conn { my $host = shift; my $port = 80; my $proto = 'tcp'; my $sock = IO::Socket::INET->new( PeerAddr => $host, PeerPort => $port, Proto => $proto); return $sock; } sub clos { my $sock = shift; $sock->close(); } sub getpage { my $path = shift; my $sock = conn( 'www.wretch.cc' ); $sock->send("GET $path HTTP/1.0\n"); $sock->send("User-Agent: X\n"); $sock->send("\n"); my @lines = $sock->getlines(); clos($sock); return @lines; } sub picpage { my $path = shift; my @lines = getpage( $path ); foreach( @lines ) { # 大圖的特徵 if( //send("GET $path HTTP/1.0\n"); $sock->send("User-Agent: X\n"); $sock->send("Referer: $refe\n"); $sock->send("\n"); print("Fetching $name ...\n"); # 跳過 header 的部分 while( my $line = $sock->getline() ) { if( $line =~ /^ $/ ) { last; } } # 寫入檔案 open(OUT, ">$name"); binmode(OUT); my $buf; while( $sock->read( $buf, 8192 )) { print OUT $buf; } clos($sock); close(OUT); } if( not -e $id ) { mkdir $id; print "mkdir $id\n"; } chdir $id; if( not -e $book ) { mkdir $book; print "mkdir $book\n"; } chdir $book; # 取得第一頁的內容 my @lines = getpage( "/album/album.php?id=$id&book=$book"); # $maxpage 表共有幾頁 $maxpage = 1; foreach(@lines) { # 頁數的特徵 my @list = /album.php\?id=$id&book=$book&page=([0-9]+)/g; foreach( @list ) { if($_ > $maxpage) { $maxpage = $_; } } } # 處理每一頁 foreach my $page (1..$maxpage) { my $path = "/album/album.php?id=$id&book=$book&page=$page"; my @lines = getpage( $path ); foreach( @lines ) { # 小圖的特徵 if( /href="\.\/show.php\?.*&f=([0-9]+)&p=([0-9]+)"/ ) { my $picpath = "/album/show.php?i=$id&b=$book&f=$1&p=$2"; picpage( $picpath ); } } } chdir "../..";