All pastes #558321 Raw Edit

Someone

public text v1 · immutable
·published 2007-06-11 10:06 UTC
rendered paste body
#!/usr/bin/perl

use strict;
use LWP;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;

my $site='http://v2.sagubooru.com';
$|++;

my($query,$limit,$pos,$rating,$path,$filename)=(shift,'16777216','1','sqmu','./','${num}');

usage() unless $query;
parse();
$path=~s/[\\\/]$//;

my $ua = LWP::UserAgent->new;
$ua->agent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'); #blatant lie
$ua->timeout(12);

my @posts;
my $currpos=$pos;
while($currpos<$pos+$limit){
	fill() unless @posts;
	last unless @posts;
	my $post=shift @posts;
	my $fn=get_filename($post);
	print "$fn existed\n" and $currpos++ and next if -e "$path/$fn.$post->{fileextension}";
	
	while(1){
		print "Getting #$post->{num}.. ";
		
		my $req=(GET "$site/data/3/$post->{md5}/ext=$post->{fileextension}");
		my $res=$ua->request($req);
		if($res->is_success){
			print "done";
			write_file("$path/$fn.$post->{fileextension}",\$res->content);
			print ", saved as $fn.$post->{fileextension}\n";
			$currpos++;
			last;
		}
		else{
			print "error:".$res->status_line."\n";
		}
	}
}

sub get_filename{
	my($post)=@_;
	
	my $str=$filename;
	$str=~s/\${(.*?)}/$post->{$1}/gx;
	
	return $str;
}

sub write_file{
	my($filename,$content)=@_;
	my $path="";
	
	while($filename=~/(.*?)[\/\\]/g){
		mkdir "$path$1";
		$path.="$1/";
	}
	
	open FILE,">$filename" or die "$! - $filename";
	binmode FILE;
	print FILE $$content;
	close FILE;
}

sub fill{
	while(1){
		print "Getting post list, offset=$currpos.. ";
		
		my $req = (GET "$site/9/?position=$currpos&limit=50&request=GetPosts&postrating=$rating&search=$query");
		my $res = $ua->request($req);
		if ($res->is_success){
			$_=$res->content;
			while(/<item PostID="(\d+)">(.*?)<\/item>/g){
				my($num,$text)=($1,$2);
				my %post;
				$post{num}=$num;
				my($date,$userid,$md5,$contenttype,$w,$h,$ip,$ext)=map{
					$text=~/<$_>(.*)<\/$_>/;$post{lc $_}=$1;$1;
				}("UploadDate","UserID","MD5","ContentType","Width","Height","UploadIP","FileExtension");
				push @posts,\%post;
			}
			print "done!\n";
			last;
		}
		else{
			print "error:".$res->status_line."\n";
		}
	}
}

sub parse{
	while($_=shift @ARGV){ 
		if(/^--(limit|pos|rating|path|filename)=(.*)$/)
			{eval(qq{\$$1='$2'});}
		else
			{usage();}
	}
}

sub usage{
	print <<HERE;
Usage: $0 QUERY [OPTION]...
Downloads images from sagubooru into current directory.

      --filename=TEMPLATE    Use TEMPLATE to to generate file names (default
                             '\${num}'; ex: 'post\${num}', 'sagu-\${md5}')
      --limit=COUNT          Download up to COUNT images (default 16777216)
      --pos=POSITION         Start downloading from image with number POSITION
      --rating=RATING        Combination of 's' 'q' 'm' 'u' (ex: 'smq')
      --path=PATH            Download to PATH instead of current directory
HERE
	
	exit;
}