# author: envi
# naver webtoon downloader(?)
#!/usr/bin/perl
use warnings;
use strict;
use LWP::Simple;
use threads ('yield',
'stack_size' => 64*4096,
'exit' => 'threads_only',
'stringify');
my $url = "http://comicmall.naver.com/webtoon.nhn?m=submain&serviceCode=";
my %code = ('1_mon' => 'S0000050',
'2_tue' => 'S0000051',
'3_wed' => 'S0000052',
'4_thu' => 'S0000053',
'5_fri' => 'S0000054');
my %contentIdHash;
foreach (keys %code)
{
mkdir "./$_";
chdir "./$_";
downloadAll( getComicList($code{$_}) );
chdir "..";
}
#---------------------------------------------------------------------------
sub getComicList
{
my $curID = shift;
my $content = get("$url$curID");
my %contentIdHash;
%contentIdHash = reverse ($content =~ m{<a href="/webtoon.nhn\?m=list&contentId=(\d+)" class="gr01"><b>(\D+)</b></a>}g);
%contentIdHash;
}
sub downloadAll
{
my %contentIdHash = @_;
my $thread;
foreach (keys(%contentIdHash))
{
$thread = threads->create('download', ($contentIdHash{$_}, $_));
}
foreach (threads->list())
{
$_->join();
}
}
sub download
{
my ($curComicNum, $comicName) = @_;
my $episode = 1;
while (1)
{
print STDOUT "$comicName->Episode: $episode\n" unless (-e "$comicName/$episode");
my $page = 1;
my $res = get("http://comicmall.naver.com/webtoon.nhn?m=detail&contentId=$curComicNum&no=$episode&page=1");
my @jpgList = $res =~ m{http://imgcomic.naver.com/webtoon/$curComicNum/$episode/(\d*.\w\w\w)}g;
last if @jpgList == 0;
mkdir "$comicName";
mkdir "$comicName/$episode";
foreach (@jpgList)
{
unless (-e "$comicName/$episode/$_")
{
my $jpgRes = get("http://imgcomic.naver.com/webtoon/$curComicNum/$episode/$_");
last if $jpgRes eq "";
open my $fh, '>', "./$comicName/$episode/$_";
binmode $fh;
print $fh $jpgRes;
close $fh;
}
$page++;
}
$episode++;
}
}