our
$VERSION
=
'0.05'
;
has
browser
=> (
is
=>
'rw'
,
default
=>
sub
{
my
(
$self
) =
@_
;
my
$browser
= new Novel::Robot::Browser();
return
$browser
;
},
);
has
site
=> (
is
=>
'rw'
,
default
=>
sub
{
''
},
);
has
parser
=> (
is
=>
'rw'
,
default
=>
sub
{
my
(
$self
) =
@_
;
my
$parser_base
= new Novel::Robot::Parser();
my
$parser
=
$parser_base
->init_parser(
'Base'
);
return
$parser
;
},
);
sub
set_site {
my
(
$self
,
$site
) =
@_
;
$self
->{site} =
$site
if
(
$site
);
unless
(
$self
->{parser_list}{
$self
->{site} }){
my
$parser_base
= new Novel::Robot::Parser();
$self
->{parser_list}{
$self
->{site} }
=
$parser_base
->init_parser(
$self
->{site});
}
$self
->{parser} =
$self
->{parser_list}{
$self
->{site} };
}
sub
set_site_by_url {
my
(
$self
,
$url
) =
@_
;
my
$site
=
$self
->{parser}->detect_site_by_url(
$url
);
$self
->set_site(
$site
)
if
( !
$self
->{site} or
$self
->{site} ne
$site
);
}
sub
get_index_ref {
my
(
$self
,
@args
) =
@_
;
$self
->set_site_by_url(
$args
[0] )
if
(
$args
[0] =~ m
my
(
$index_url
) =
$self
->{parser}->generate_index_url(
@args
);
my
$html_ref
=
$self
->{browser}->get_url_ref(
$index_url
);
$self
->{parser}->alter_index_before_parse(
$html_ref
);
my
$ref
=
$self
->{parser}->parse_index(
$html_ref
);
return
unless
(
defined
$ref
);
$ref
->{index_url} =
$index_url
;
$ref
->{site} =
$self
->{parser}{site};
return
$ref
unless
(
exists
$ref
->{book_info_urls} );
while
(
my
(
$url
,
$info_sub
) =
each
%{
$ref
->{book_info_urls} } ) {
my
$info
=
$self
->{browser}->get_url_ref(
$url
);
next
unless
(
defined
$info
);
$info_sub
->(
$ref
,
$info
);
}
return
$ref
;
}
sub
get_chapter_ref {
my
(
$self
,
@args
) =
@_
;
$self
->set_site_by_url(
$args
[0] )
if
(
$args
[0] =~ m
my
(
$chap_url
,
$chap_id
) =
$self
->{parser}->generate_chapter_url(
@args
);
my
$html_ref
=
$self
->{browser}->get_url_ref(
$chap_url
);
return
unless
(
$html_ref
);
$self
->{parser}->alter_chapter_before_parse(
$html_ref
);
my
$ref
=
$self
->{parser}->parse_chapter(
$html_ref
);
return
unless
(
$ref
);
$ref
->{content} =~ s
$ref
->{content} =~ s
$ref
->{content} =~ s
$ref
->{chapter_url} =
$chap_url
;
$ref
->{chapter_id} =
$chap_id
;
return
$ref
;
}
sub
get_empty_chapter_ref {
my
(
$self
,
$id
) =
@_
;
my
%data
;
$data
{chapter_id} =
$id
;
return
\
%data
;
}
sub
get_writer_ref {
my
(
$self
,
@args
) =
@_
;
$self
->set_site_by_url(
$args
[0] )
if
(
$args
[0] =~ m
my
(
$writer_url
) =
$self
->{parser}->generate_writer_url(
@args
);
my
$html_ref
=
$self
->{browser}->get_url_ref(
$writer_url
);
my
$writer_books
=
$self
->{parser}->parse_writer(
$html_ref
);
return
$writer_books
;
}
sub
get_query_ref {
my
(
$self
,
$type
,
$keyword
) =
@_
;
my
$key
= encode(
$self
->{parser}->charset,
$keyword
);
my
(
$url
,
$post_vars
) =
$self
->{parser}->make_query_url(
$type
,
$key
);
my
$html_ref
=
$self
->{browser}->get_url_ref(
$url
,
$post_vars
);
return
unless
$html_ref
;
my
$result
=
$self
->{parser}->parse_query(
$html_ref
);
my
$result_urls_ref
=
$self
->{parser}->get_query_result_urls(
$html_ref
);
return
$result
unless
(
defined
$result_urls_ref
);
for
my
$url
(
@$result_urls_ref
) {
my
$h
=
$self
->{browser}->get_url_ref(
$url
);
my
$r
=
$self
->{parser}->parse_query(
$h
);
push
@$result
,
@$r
;
}
return
$result
;
}
no
Moo;
1;