# -*- cperl -*-

use LWP;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;
use HTTP::Headers;
use MIME::QuotedPrint;
use MIME::Base64;
use Encode;

my $hdr = HTTP::Headers->new;
#$hdr->header(Cookie => 'phpbb3_6cazq_u=garbage; phpbb3_6cazq_k=garbage; phpbb3_6cazq_sid=garbage);

my $useragent = LWP::UserAgent->new
	(
	 agent => 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0',
#	 default_headers => $hdr,
	 keep_alive => 1
	);

use Data::Dumper;

# oops
die unless (length $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME});

for (my $idx = 1; $idx <= 9300; $idx++) {
	print "$idx\n";
	my $response = $useragent->get('https://forums.famitracker.com/download/file.php?id='.$idx);

	if ($response->code() == 200) {
		open Z,'>',"$idx--last-modified" or warn "LM $idx $? $!";
		print Z $response->header("Last_Modified");
		close Z;
		# ->filename is useless to me
		print $response->header("Content_Disposition"),"\n";
		open Z,'>',"$idx--".$response->header("Content_Disposition") or warn "$idx $? $!";
		print Z $response->content;
		close Z;
	} else {
		open Z,'>',"$idx--error.dumper" or warn "Err $idx $? $!";
		print "HTTP ",$response->code(),"\n";
		print Z Data::Dumper->Dump([$response]);
		close Z;
	}

	sleep 1+3*rand;
}
