source: trollforge/troll/LJ.pm @ 386

Revision 310, 4.0 KB checked in by incog, 3 years ago (diff)
Line 
1#!/usr/bin/perl
2# mao @ irc.hardchats.com
3# simple class for working with livejournal
4
5package LJ;
6use strict;
7use warnings;
8
9use Mouse;
10use LWP;
11use LWP::UserAgent;
12use HTTP::Request;
13
14has 'user' => (is => 'rw', isa => 'Str');
15has 'password' => (is => 'rw', isa => 'Str');
16has 'server' => (is => 'rw', isa => 'Str', default => 'livejournal.com');
17has 'http_proxy' => (
18        is => 'rw',
19        isa => 'Str',
20        predicate => 'use_proxy'
21        );
22
23# LJ::post("Subj", "test");
24sub post {
25        my $self = shift;
26        my $subj = shift;
27        my $body = shift;
28        my @tiem = _getTiem();
29        my $params = {
30            mode       => "postevent",
31            user       => $self->user,
32            password   => $self->password,
33            subject        => $subj,
34            event          => $body,
35            version    => 1,
36            clientversion => "JavaEE-ProBlogger",
37            year => $tiem[0],
38            mon => $tiem[1],
39            day => $tiem[2],
40            hour => $tiem[3],
41            min => $tiem[4]
42        };
43        my $user_a = LWP::UserAgent->new(agent => "JavaEE-ProBlogger 2000");
44        my $req = HTTP::Request->new(POST => "http://".$self->server."/interface/flat");
45        $req->content(_encode($params));
46        my $res = $user_a->request($req);
47        print $res->content;
48        return undef unless $res;
49        return split(/\n/, $res->content);
50}
51
52sub _getTiem {
53        my @curTime = localtime;
54        $curTime[5]+=1900; # year
55        $curTime[4]+=1; # month
56        return ( $curTime[5], $curTime[4], $curTime[3], $curTime[2], $curTime[1] ); 
57}
58
59
60
61# LJ::comment("http://lj.rossia.org/users/yaroslavz/80368.html?replyto=378608", "body text, "subject text");
62# LJ::comment("http://lj.rossia.org/users/yaroslavz/80368.html" ...
63# LJ::comment("http://lj.rossia.org/users/yaroslavz/80368.html?mode=reply" ...
64# LJ::comment("http://ttt.livejournal.com/84144.html", ...);
65sub comment {
66        my $self = shift;
67       
68        my $reply_url = my $comment_url =shift;
69        my $body = shift;
70        my $subj = shift;
71       
72        my $journal; my $post_id; my $parent;
73       
74        # validating the url
75        my $server = $self->server;
76        if($reply_url =~ /http:\/\/$server\/users\/(.*?)\/(\d*).html/) {
77                $journal = $1; $post_id = $2;
78        } elsif ($reply_url =~ /http:\/\/(.*?)\.$server\/(\d*).html/)   {
79                $journal = $1; $post_id = $2;
80        #} elsif ($reply_url =~ /http:\/\/community\.$server\/(.*?)\/(\d*).html/ {
81        #       $journal = $1; $post_id = $2;
82        } else {
83                return "nope";
84        }
85        #return "ya";
86        # if there is no ?mode=reply in url, we should add it
87        $reply_url .= "?mode=reply" unless ($reply_url =~ /\?mode=reply$/);
88
89        my $chal;
90        my $user_a = LWP::UserAgent->new(agent => "JavaEE-ProBlogger 2000");
91        $user_a->proxy(['http']=>$self->http_proxy); # if($self->use_proxy);
92       
93        my $req = HTTP::Request->new(GET => $reply_url);
94        my $res = $user_a->request($req);
95        if($res->content =~ /id='login_chal' value='(.*?)' \/>/) {
96                $chal = $1; 
97        } else {
98                return undef;
99        }
100        if($res->content =~ /type='hidden' name="replyto" value="(\d+)" \/>/)
101        {
102                $parent = $1;
103        } else {
104                $parent = 0;
105        }
106       
107       
108        my $params = {
109            replyto                     => $parent,
110            parenttalkid        => $parent,
111            itemid              => $post_id,
112            journal                     => $journal,
113            usertype            => "user",
114            userpost            => $self->user,
115            password            => $self->password,
116            subject                     => $subj,
117            body                        => $body,
118            chal                        => $chal,
119            subjecticon         => "no",
120        };
121       
122        $req = HTTP::Request->new(POST => "http://".$self->server."/talkpost_do.bml");
123        $req->content(_encode($params));
124        $req->content_type('application/x-www-form-urlencoded');
125       
126        $res = $user_a->request($req);
127        return undef unless $res;
128        return $res->content;
129}
130
131# LOL STOLEN FROM NET::LIVEJOURNAL LOL FITZPATIKK IS GAY
132sub _encode {
133    my $val = shift;
134    $val = "" unless defined $val;
135    if (ref $val) {
136        my $ret = "";
137        while (my ($k, $v) = each %$val) {
138            $ret .= _encode($k) . "=" . _encode($v) . "&";
139        }
140        chop $ret;
141        return $ret;
142    } else {
143        $val =~ s/([^a-zA-Z0-9_\,\-.\/\\\: ])/uc sprintf("%%%02x",ord($1))/eg;
144        $val =~ tr/ /+/;
145        return $val;
146    }
147}
148
1491;
150
Note: See TracBrowser for help on using the repository browser.