| 1 | package Dyatel; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Dyatel - Internal | ||
| 9 | |||
| 10 | =head1 VERSION | ||
| 11 | |||
| 12 | Version 0.01 | ||
| 13 | |||
| 14 | =head1 AUTHOR | ||
| 15 | |||
| 16 | andrey@kostenko.name | ||
| 17 | |||
| 18 | =head1 LICENSE | ||
| 19 | |||
| 20 | perl | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | our $VERSION = '0.01'; | ||
| 25 | use 5.010; | ||
| 26 | use mro 'c3'; | ||
| 27 | |||
| 28 | use Data::Dumper; | ||
| 29 | use Net::Twitter::Stream; | ||
| 30 | use WWW::Mechanize; | ||
| 31 | use YAML qw(LoadFile); | ||
| 32 | use base 'Class::Accessor::Fast'; | ||
| 33 | |||
| 34 | __PACKAGE__->mk_accessors( qw(twitter vkontakte mech) ); | ||
| 35 | |||
| 36 | =head2 new | ||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | sub new { | ||
| 43 | my $class = shift; | ||
| 44 | my @files = grep { -e $_ } map glob, qw(~/.dyatel /usr/local/etc/dyatelrc /etc/dyatelrc); | ||
| 45 | die "No config found\n" unless @files; | ||
| 46 | my $self = $class->next::method(LoadFile($files[0])); | ||
| 47 | $self->mech( WWW::Mechanize->new ); | ||
| 48 | $self->mech->cookie_jar({}); | ||
| 49 | return $self; | ||
| 50 | } | ||
| 51 | |||
| 52 | =head2 auth | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | sub auth { | ||
| 57 | my $self = shift; | ||
| 58 | my $mech = $self->{mech}; | ||
| 59 | $mech->get('http://vkontakte.ru/'); | ||
| 60 | sleep 1; | ||
| 61 | $mech->submit_form( form_number => 1, fields => $self->vkontakte ); | ||
| 62 | $mech->submit_form( form_number => 1 ); | ||
| 63 | ($mech->content =~ /<input type='hidden' id='activityhash' value='(\w+)'>/ ) or die "Not authorized!\n"; | ||
| 64 | Net::Twitter::Stream::Follow->new ( @{$self->twitter}{qw(login password)}, | ||
| 65 | '9310862', | ||
| 66 | sub { $self->got_tweet_callback(@_) } ); | ||
| 67 | } | ||
| 68 | |||
| 69 | =head2 run | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | sub run { | ||
| 74 | Danga::Socket->EventLoop; | ||
| 75 | } | ||
| 76 | |||
| 77 | =head2 got_tweet_callback | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub got_tweet_callback { | ||
| 82 | my $self = shift; | ||
| 83 | my $tweet = shift; | ||
| 84 | return if $tweet->{text} =~ /^\@/; | ||
| 85 | $self->mech->get('http://vkontakte.ru/'); | ||
| 86 | sleep 1; | ||
| 87 | ($self->{activity_hash}) = ($self->mech->content =~ /<input type='hidden' id='activityhash' value='(\w+)'>/ ); | ||
| 88 | sleep 1; | ||
| 89 | $self->mech->post( 'http://vkontakte.ru/profile.php', { activityhash => $self->{activity_hash}, setactivity => $tweet->{text} } ); | ||
| 90 | sleep 1; | ||
| 91 | } | ||
| 92 | |||
| 93 | 1; # End of Dyatel |