File Coverage

lib/LJ/GetCookieSession.pm
Criterion Covered Total %
statement 12 49 24.4
branch 0 12 0.0
condition 0 3 0.0
subroutine 4 7 57.1
pod 1 1 100.0
total 17 72 23.6


line stmt bran cond sub pod time code
1             package LJ::GetCookieSession;
2            
3 2     2   34849 use warnings;
  2         4  
  2         200  
4 2     2   11 use strict;
  2         4  
  2         77  
5 2     2   23 use vars qw($VERSION);
  2         4  
  2         100  
6            
7 2     2   2665 use WWW::Mechanize;
  2         491401  
  2         1266  
8             require Digest::MD5;
9            
10            
11             our $VERSION = '0.01';
12            
13             =pod
14            
15             =head1 NAME
16            
17             LJ::GetCookieSession - A perl module to log into livejournal services
18            
19             =head1 VERSION
20            
21             Version 0.01
22            
23             =head1 SYNOPSIS
24            
25             C is an C module which is used to generate value of cookie parameter
26             named 'ljsession', which can be used in future requests to lj services.
27            
28             Request mode sessiongenerate (see L) is used.
29            
30             use LJ::GetSessionCookie;
31            
32             my $ljsession = LJ::GetCookieSession->generate({user=> ..., pass=>...});
33            
34             L
35            
36             =head1 EXAMPLE
37            
38             The following simple shows how to use the module to get all comments from LiveJournal.
39            
40             use WWW::Mechanize;
41             use LJ::GetCookieSession;
42            
43             my $mech = WWW::Mechanize->new(
44             agent => 'support@creograf.ru',
45             cookie_jar => { "ljsession" => "" }
46             );
47            
48             my $ljsession = LJ::GetCookieSession->generate({user=> ..., pass=>...});
49            
50             die "failed to log into lj: ljsession failed\n" unless ( defined $ljsession );
51            
52             $mech->add_header ('X-LJ-Auth' => "cookie");
53             $mech->add_header ('Cookie' => "ljsession=$ljsession");
54            
55             $mech->get("http://livejournal.com/export_comments.bml?get=comment_body");
56            
57             return undef unless ($mech->res->is_success);
58            
59             my $xml_comments = $mech->content();
60            
61             =head1 LICENSE AND COPYRIGHT
62            
63             Copyright 2011 Anastasiya Deeva, Studio Creograf L, L
64            
65             This program is free software; you can redistribute it and/or modify it
66             under the terms of either: the GNU General Public License as published
67             by the Free Software Foundation; or the Artistic License.
68            
69             See http://dev.perl.org/licenses/ for more information.
70            
71             =head1 AVAILABLE METHODS
72            
73             =head2 LJ::GetSessionCookie::generate()
74            
75             C is a routine which generates value of cookie 'ljsession' for LiveJournal.
76            
77             =over 4
78            
79             =item user
80            
81             The username who owns the journal;
82             this option is B.
83            
84             =item pass
85            
86             The password of the C;
87             this option is B.
88            
89             =item server
90            
91             URL of remote site to login.
92            
93             =back
94            
95             =cut
96            
97             sub generate {
98 0     0 1   my $self = shift;
99 0           my $pars = {
100             "server"=>"http://livejournal.com",
101 0           %{$_[0]}
102             };
103 0 0         $pars->{"server"}="http://".$pars->{'server'} unless($pars->{'server'} =~ /^http/);
104            
105 0 0 0       die "user and password are required for login" unless($pars->{'user'} and $pars->{'pass'});
106            
107 0           my $mech = WWW::Mechanize->new( agent => 'support@creograf.ru', );
108            
109 0           my $r =
110             $mech->post( $pars->{"server"} . "/interface/flat", { "mode" => "getchallenge" } );
111 0           my $response = $self->_flatresponse( $mech->content() );
112            
113 0 0         die "challenge not recieved" unless $response->{'challenge'};
114            
115 0           $r = $mech->post(
116             $pars->{"server"} . "/interface/flat",
117             {
118             "mode" => "sessiongenerate",
119             "user" => $pars->{"user"},
120             "auth_method" => "challenge",
121             "auth_challenge" => $response->{'challenge'},
122             "auth_response" =>
123             $self->_calcchallenge( $response->{'challenge'}, $pars->{"pass"} )
124             }
125             );
126            
127 0           $response = $self->_flatresponse( $mech->content() );
128            
129 0 0         die "auth failed".$mech->content() unless $response->{'ljsession'};
130            
131 0 0         return undef unless defined $response->{'ljsession'};
132 0           return $response->{'ljsession'};
133             }
134            
135             # Define reference from new to generate
136             #*new="";
137             #*new=\&generate;
138            
139             # generates challenge response
140             sub _calcchallenge {
141 0     0     my $self = shift;
142 0           my ( $challenge, $password ) = @_;
143            
144 0           my $md5_1=Digest::MD5->new;
145 0           $md5_1->add($password);
146 0           $password=$md5_1->hexdigest;
147            
148 0           my $md5 = Digest::MD5->new;
149 0           $md5->add($challenge);
150 0           $md5->add($password);
151 0           return $md5->hexdigest;
152             }
153            
154             # parses response of http://www.livejournal.com/interface/flat
155             sub _flatresponse {
156 0     0     my $self = shift;
157 0           my $response = shift;
158 0           my $r = {};
159 0           my @ar = split( /\n/, $response );
160            
161 0           my $index = 0;
162 0           foreach my $name (@ar) {
163 0           $name =~ s/\n//g;
164 0 0         if ( length($name) > 0 ) {
165 0           my $value = $ar[ $index + 1 ];
166 0           $value =~ s/\n//g;
167 0           $r->{$name} = $value;
168 0           $ar[ $index + 1 ] = "";
169             }
170 0           $index++;
171             }
172 0           return $r;
173             }
174            
175             =head1 BUGS
176            
177             Please report any bugs or feature requests to C, or through
178             the web interface at L. I will be notified, and then you'll
179             automatically be notified of progress on your bug as I make changes.
180            
181            
182            
183            
184             =head1 SUPPORT
185            
186             You can find documentation for this module with the perldoc command.
187            
188             perldoc LJ::GetSessionCookie
189            
190            
191             You can also look for information at:
192            
193             =over 4
194            
195             =item * RT: CPAN's request tracker
196            
197             L
198            
199             =item * AnnoCPAN: Annotated CPAN documentation
200            
201             L
202            
203             =item * CPAN Ratings
204            
205             L
206            
207             =item * Search CPAN
208            
209             L
210            
211             =back
212            
213             =cut
214             1;
215