File Coverage

blib/lib/WWW/Reddit.pm
Criterion Covered Total %
statement 24 75 32.0
branch 0 4 0.0
condition 0 15 0.0
subroutine 8 17 47.0
pod 0 8 0.0
total 32 119 26.8


line stmt bran cond sub pod time code
1             package WWW::Reddit;
2              
3             our $VERSION = '0.10';
4              
5 1     1   29454 use 5.012004;
  1         4  
  1         36  
6 1     1   1419 use Data::Dumper;
  1         15184  
  1         86  
7              
8 1     1   1204 use common::sense;
  1         22  
  1         9  
9              
10 1     1   1272 use LWP::Simple;
  1         671717  
  1         10  
11 1     1   1798 use JSON;
  1         14846  
  1         6  
12              
13 1     1   1139 use HTTP::Cookies;
  1         8350  
  1         39  
14 1     1   9 use LWP::UserAgent;
  1         2  
  1         264  
15              
16             my $base_url = 'http://www.reddit.com/';
17             my $api_url = $base_url . 'api/';
18             my $login_url = $api_url . 'login';
19             my $submit_url = $api_url . 'submit';
20             my $comment_url = $api_url . 'comment';
21              
22             my $api_type = 'json';
23              
24             sub new {
25 0     0 0   my $obj_class = shift;
26 0   0       my $class = ref $obj_class || $obj_class;
27            
28 0           my ($user, $passwd, $subreddit) = @_;
29              
30 0           my $self = {
31             base_url => $base_url,
32             api_url => $api_url,
33            
34             login_url => $login_url,
35             submit_url => $submit_url,
36              
37             api_type => $api_type,
38              
39             user => $user,
40             passwd => $passwd,
41              
42             subreddit => $subreddit,
43              
44             ua => new LWP::UserAgent,
45             cookie_jar => HTTP::Cookies->new,
46              
47             modhash => '',
48             };
49              
50 0           bless $self, $class;
51            
52 0           $self->create_methods;
53 0           return $self;
54             }
55              
56             #>-----------------------------------------------<#
57             # Helper Methods
58             #>-----------------------------------------------<#
59              
60             # create accessor/mutator methods for defined parameters
61             sub create_methods {
62 0     0 0   my $self = shift;
63 0           for my $datum (keys %{$self}) {
  0            
64 1     1   7 no strict "refs";
  1         2  
  1         861  
65             *$datum = sub {
66 0     0     my $self = shift;
67 0 0         $self->{$datum} = shift if @_;
68 0           return $self->{$datum};
69 0           };
70             }
71             }
72              
73             # Set cookie
74             sub set_cookie {
75 0     0 0   my $self = shift;
76 0           my $response = shift;
77              
78 0           $self->cookie_jar->extract_cookies ($response);
79 0           $self->ua->cookie_jar ($self->cookie_jar);
80 0           $self->parse_modhash ($response);
81             }
82              
83             # Set modhash
84             sub parse_modhash {
85 0     0 0   my $self = shift;
86 0           my $response = shift;
87              
88 0           my $decoded = from_json ($response->content);
89 0           $self->modhash ($decoded->{json}{data}{modhash});
90             }
91              
92             # takes link, returns post ID
93             sub parse_link {
94 0     0 0   my $self = shift;
95 0           my $link = shift;
96              
97 0           my ($id) = $link =~ /comments\/(\w+)\//i;
98 0           return $id;
99             }
100              
101             #>---------------------------------------------------<#
102             # Main Methods
103             #>---------------------------------------------------<#
104              
105             # Login to reddit
106             sub login {
107 0     0 0   my $self = shift;
108            
109 0 0         if (@_) {
110 0           $self->user ($_[0]);
111 0           $self->passwd ($_[1]);
112             }
113 0           my $response = $self->ua->post($self->login_url,
114             {
115             api_type => $self->api_type,
116             user => $self->user,
117             passwd => $self->passwd,
118             }
119             );
120              
121 0           $self->set_cookie( $response);
122             # print Dumper $response;
123             }
124              
125             # Submit link to reddit
126             sub submit_link {
127 0     0 0   my $self = shift;
128 0           my ($title, $url, $subreddit) = @_;
129              
130 0           my $kind = 'link';
131              
132 0   0       my $newpost = $self->ua->post($self->submit_url,
      0        
133             {
134             uh => $self->modhash,
135             kind => $kind,
136             sr => $subreddit || $self->subreddit,
137             title => $title,
138             r => $subreddit || $self->subreddit,
139             url => $url,
140             }
141             );
142              
143 0           my $json_content = $newpost->content;
144 0           my $decoded = from_json $json_content;
145              
146             #returns link to new post if successful
147 0           my $link = $decoded->{jquery}[18][3][0];
148 0           my $id = $self->parse_link($link);
149              
150 0           return $id, $link;
151             }
152              
153             sub submit_story {
154 0     0 0   my $self = shift;
155 0           my ($title, $text, $subreddit) = @_;
156              
157 0           my $kind = 'self';
158              
159 0   0       my $newpost = $self->ua->post($self->submit_url,
      0        
160             {
161             uh => $self->modhash,
162             kind => $kind,
163             sr => $subreddit || $self->subreddit,
164             r => $subreddit || $self->subreddit,
165             title => $title,
166             text => $text,
167             },
168             );
169              
170 0           my $json_content = $newpost->content;
171 0           my $decoded = from_json $json_content;
172              
173             #returns id and link to new post if successful
174 0           my $link = $decoded->{jquery}[12][3][0];
175 0           my $id = $self->parse_link($link);
176              
177 0           return $id, $link;
178             }
179              
180             1;
181             __END__