our
$VERSION
=
'0.3.02'
;
use
5.010001;
has
'base_url'
=> (
is
=>
'ro'
,
isa
=>
'Str'
,
);
has
'api_url'
=> (
is
=>
'ro'
,
isa
=>
'Str'
,
lazy
=> 1,
default
=>
sub
{
$_
[0]->base_url .
'api/'
},
);
has
'login_api'
=> (
is
=>
'ro'
,
isa
=>
'Str'
,
lazy
=> 1,
default
=>
sub
{
$_
[0]->api_url .
'login'
},
);
has
'submit_api'
=> (
is
=>
'ro'
,
isa
=>
'Str'
,
lazy
=> 1,
default
=>
sub
{
$_
[0]->api_url .
'submit'
},
);
has
'comment_api'
=> (
is
=>
'ro'
,
isa
=>
'Str'
,
lazy
=> 1,
default
=>
sub
{
$_
[0]->api_url .
'comment'
},
);
has
'vote_api'
=> (
is
=>
'ro'
,
isa
=>
'Str'
,
lazy
=> 1,
default
=>
sub
{
$_
[0]->api_url .
'vote'
},
);
has
'api_type'
=> (
is
=>
'ro'
,
isa
=>
'Str'
,
default
=>
'json'
,
);
has
'ua'
=> (
is
=>
'rw'
,
isa
=>
'LWP::UserAgent'
,
default
=>
sub
{ LWP::UserAgent->new },
handles
=> {
post
=>
'post'
,
get
=>
'get'
,
agent_cookie_jar
=>
'cookie_jar'
}
);
has
'cookie_jar'
=> (
is
=>
'rw'
,
isa
=>
'HTTP::Cookies'
,
lazy
=> 1,
default
=>
sub
{ HTTP::Cookies->new },
);
has
[
'user_name'
,
'password'
, ] => (
is
=>
'rw'
,
isa
=>
'Str'
,
required
=> 1,
trigger
=> \
&_login
,
);
has
'subreddit'
=> (
is
=>
'rw'
,
isa
=>
'Str'
,
);
has
'modhash'
=> (
is
=>
'rw'
,
isa
=>
'Str'
,
);
has
'_user_search_name'
=> (
is
=>
'rw'
,
isa
=>
'Str'
,
lazy
=> 1,
default
=>
''
,
);
has
'about_user_api'
=> (
is
=>
'rw'
,
isa
=>
'Str'
,
lazy
=> 1,
default
=>
sub
{
$_
[0]->base_url .
'user/'
.
$_
[0]->_user_search_name .
'/about.json'
},
);
has
'user_info'
=> (
is
=>
'rw'
,
isa
=>
'Reddit::Type::User'
,
lazy
=> 1,
default
=>
sub
{ Reddit::Type::User->new },
);
sub
_login {
my
$self
=
shift
;
my
$response
=
$self
->ua->post(
$self
->login_api,
{
api_type
=>
$self
->api_type,
user
=>
$self
->user_name,
passwd
=>
$self
->password,
}
);
$self
->_set_cookie(
$response
);
}
sub
_set_cookie {
my
$self
=
shift
;
my
$response
=
shift
;
$self
->cookie_jar->extract_cookies (
$response
);
$self
->agent_cookie_jar (
$self
->cookie_jar);
$self
->_parse_modhash (
$response
);
}
sub
_parse_modhash {
my
$self
=
shift
;
my
$response
=
shift
;
my
$decoded
= from_json (
$response
->content);
$self
->modhash (
$decoded
->{json}{data}{modhash});
}
sub
_parse_link {
my
$self
=
shift
;
my
$link
=
shift
;
my
(
$id
) =
$link
=~ /comments\/(\w+)\//i;
return
't3_'
.
$id
;
}
sub
_parse_comment_id {
my
$id
=
shift
;
if
(
length
(
$id
) == 5){
$id
=
"t3_"
.
$id
}
elsif
(
length
(
$id
) == 7){
$id
=
"t1_"
.
$id
}
else
{
die
"Invalid ID length"
}
return
$id
;
}
sub
submit_link {
my
$self
=
shift
;
my
(
$title
,
$url
,
$subreddit
) =
@_
;
my
$kind
=
'link'
;
my
$newpost
=
$self
->ua->post(
$self
->submit_api,
{
uh
=>
$self
->modhash,
kind
=>
$kind
,
sr
=>
$subreddit
||
$self
->subreddit,
title
=>
$title
,
r
=>
$subreddit
||
$self
->subreddit,
url
=>
$url
,
}
);
my
$json_content
=
$newpost
->content;
my
$decoded
= from_json
$json_content
;
my
$link
=
$decoded
->{jquery}[18][3][0];
my
$id
=
$self
->parse_link(
$link
);
return
$id
,
$link
;
}
sub
submit_story {
my
$self
=
shift
;
my
(
$title
,
$text
,
$subreddit
) =
@_
;
my
$kind
=
'self'
;
my
$newpost
=
$self
->post(
$self
->submit_api,
{
uh
=>
$self
->modhash,
kind
=>
$kind
,
sr
=>
$subreddit
||
$self
->subreddit,
r
=>
$subreddit
||
$self
->subreddit,
title
=>
$title
,
text
=>
$text
,
},
);
my
$json_content
=
$newpost
->content;
my
$decoded
= from_json
$json_content
;
my
$link
=
$decoded
->{jquery}[12][3][0];
my
$id
=
$self
->_parse_link(
$link
);
return
$id
,
$link
;
}
sub
comment {
my
$self
=
shift
;
my
(
$thing_id
,
$comment
) =
@_
;
$thing_id
=
$self
->_parse_comment_id(
$thing_id
);
my
$response
=
$self
->post(
$self
->comment_api,
{
thing_id
=>
$thing_id
,
text
=>
$comment
,
uh
=>
$self
->modhash,
},
);
my
$decoded
= from_json
$response
->content;
return
$decoded
->{jquery}[18][3][0][0]->{data}{id};
}
sub
get_user_info {
my
$self
=
shift
;
my
$search_name
=
shift
;
$self
->_user_search_name(
$search_name
);
my
$response
=
$self
->get (
$self
->about_user_api);
my
$decoded
= from_json
$response
->content;
my
$data
=
$decoded
->{data};
while
(
my
(
$key
,
$value
) =
each
%{
$data
}) {
if
(JSON::is_bool
ref
$value
){
$value
=
$value
?
'1'
:
'0'
;
}
$self
->user_info->
$key
(
$value
);
}
return
$self
->user_info;
}
sub
vote {
my
$self
=
shift
;
my
(
$thing_id
,
$direction
) =
@_
;
given
(
$direction
) {
when
( /up/i || 1) {
$direction
= 1;
}
when
( /down/i || -1) {
$direction
= -1;
}
when
( /rescind/i || 0 ) {
$direction
= 0;
}
default
{
warn
"Please enter a valid direction"
;
return
0;
}
}
my
$response
=
$self
->post (
$self
->vote_api,
{
id
=>
$thing_id
,
dir
=>
$direction
,
uh
=>
$self
->modhash
}
);
return
$response
->content;
}
no
Moose;
__PACKAGE__->meta->make_immutable;
1;