[PERL] 4chan image downloader
Sup faggots,
da mir c# auf den Sack geht und ich kein bock habe json in cpp zu parsen oder eine socket library zu schreiben habe ich das "Projekt" mal in perl umgesetzt.
Ihr braucht:
WWW::Mechanize
JSON
Dazu einfach:
# cpan
# install JSON
# install WWW::Mechanize
Gegebenenfalls noch JSON::XS wenn es bei JSON nicht schon dabei ist.
Aufruf:
./download.pl https://boards.4chan.org/b/res/424778925
Es können mehrere URLs angegeben werden, einfach mit einem Leerzeichen trennen. Dateien landen default in /root/pic (LEGT DEN ORDNER AN BEVOR IHR OHNE PLAN WAS MACHT), könnt ihr aber im Script ändern (Variable $folder).
EDIT: Geht jetzt mit jedem Board.
Source:
PHP-Code:
#!/usr/bin/perl
use strict;
use WWW::Mechanize;
use JSON -support_by_pp;
my $url;
my $board;
foreach my $arg (@ARGV)
{
parseURL($arg);
downloadJSON($url);
}
sub downloadJSON
{
my $download = WWW::Mechanize->new(autocheck => 0);
$download->get($url);
if ($download->success())
{
my $source = $download->content();
my $json = new JSON;
my $jsonText = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($source);
foreach my $posts(@{$jsonText->{posts}})
{
my $pic = $posts->{tim} . $posts->{ext};
if ($pic != "")
{
downloadPicture( $posts->{tim} . $posts->{ext} );
}
}
} else
{
print "looks like the thread 404'd\n";
}
}
sub downloadPicture
{
my $folder = "/root/pic/";
my $download = WWW::Mechanize->new(autocheck => 0);
my $file = $folder . $_[0];
if ( -e $file)
{
print "file " . $file . " already exists\n";
} else
{
$download->get( "http://images.4chan.org/$board/src/" . $_[0] );
if ($download->success())
{
open (my $picture, ">$file") || die $!;
print $picture $download->content;
print "downloading " . $file . "\n";
close ($picture);
} else
{
print "failed";
}
}
}
sub parseURL
{
my $oldUrl = $_[0];
$url = $oldUrl;
$oldUrl =~ s/http(s):\/\/boards.4chan.org\///;
$oldUrl =~ s/\/res\/[0-9]{1,15}//;
$board = $oldUrl;
$url =~ s/http(s):\/\/boards.4chan.org\/$board\/res\//http:\/\/api.4chan.org\/$board\/res\//;
$url = $url . ".json";
}
hf & gl