File Coverage

blib/lib/Net/Twitter/Diff.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Net::Twitter::Diff;
2              
3 1     1   29687 use warnings;
  1         2  
  1         34  
4 1     1   6 use strict;
  1         2  
  1         34  
5 1     1   539 use Moose;
  0            
  0            
6             extends 'Net::Twitter::Core';
7             with 'Net::Twitter::Role::API::REST';
8              
9             use Array::Diff;
10              
11             our $VERSION = '0.12';
12              
13             sub xfollowing {
14             my $self = shift;
15             my $id = shift;
16              
17             my $cursor = -1;
18             my @data = ();
19             while(1){
20             my $args = { cursor => $cursor } ;
21             $args->{id} = $id if $id;
22             my $res = $self->following( $args );
23             push @data , @{ $res->{users} };
24              
25             $cursor = $res->{next_cursor};
26             last if $cursor == 0;
27             }
28              
29             return \@data;
30             }
31              
32             sub xfollowers {
33             my $self = shift;
34              
35             my $cursor = -1;
36             my @data = ();
37             while(1){
38             my $res = $self->followers({ cursor => $cursor });
39             push @data , @{ $res->{users} };
40              
41             $cursor = $res->{next_cursor};
42             last if $cursor == 0;
43             }
44              
45             return \@data;
46             }
47              
48             sub diff {
49             my $self = shift;
50             my $args = shift;
51              
52             my $res = {};
53             my @following = map { $_->{screen_name} } @{$self->xfollowing};
54             my @followers = map { $_->{screen_name} } @{$self->xfollowers};
55              
56             my $diff = Array::Diff->diff( [ sort @followers ] , [ sort @following ] );
57              
58             $res->{not_following} = $diff->deleted;
59             $res->{not_followed} = $diff->added;
60             my @communicated = ();
61             my $not_followed_ref = {};
62             for my $user ( @{ $res->{not_followed} } ) {
63             $not_followed_ref->{ $user } = 1;
64             }
65              
66             for my $screen_name ( @following ) {
67             if ( !defined $not_followed_ref->{ $screen_name } ) {
68             push @communicated , $screen_name;
69             }
70             }
71              
72             $res->{communicated} = \@communicated;
73              
74             return $res;
75             }
76              
77              
78             sub comp_following {
79             my $self = shift;
80             my $id = shift;
81              
82             my $res = {};
83             my $me_ref = $self->xfollowing();
84             my $him_ref = $self->xfollowing( $id );
85              
86             my $me = [];
87             my $him = [];
88             my $me_hash = {};
89             for my $item ( @{ $me_ref } ) {
90             push @{ $me } , $item->{screen_name};
91             $me_hash->{ $item->{screen_name} } = 1;
92             }
93              
94             for my $item ( @{ $him_ref } ) {
95             push @{ $him } , $item->{screen_name};
96             }
97              
98             my $diff = Array::Diff->diff( [sort @$me ], [ sort @$him ] );
99              
100             $res->{only_me} = $diff->deleted;
101             $res->{not_me} = $diff->added;
102             my @communicated = ();
103              
104             for my $screen_name ( @{ $him } ) {
105             if ( defined $me_hash->{ $screen_name } ) {
106             push @communicated , $screen_name;
107             }
108             }
109              
110             $res->{share} = \@communicated;
111              
112             return $res;
113             }
114              
115             1;
116              
117             =head1 NAME
118              
119             Net::Twitter::Diff - Twitter Diff
120              
121             =head1 SYNOPSIS
122              
123             use Net::Twitter::Diff;
124              
125             my $diff = Net::Twitter::Diff->new( username => '******' , password => '******');
126              
127             my $res = $diff->diff();
128              
129             # get screen_names who you are not following but they are.
130             print Dumper $res->{not_following};
131              
132             # get screen_names who they are not following but you are.
133             print Dumper $res->{not_followed};
134              
135             # get screen_names who you are following them and also they follow you.
136             print Dumper $res->{communicated};
137              
138              
139             my $res2 = $diff->comp_following( 'somebody_twitter_name' );
140              
141             # only you are following
142             print Dumper $res2->{only_me} ;
143              
144             # you are not following but somebody_twitter_name are following
145             print Dumper $res2->{not_me} ;
146              
147             # both you and somebody_twitter_name are following
148             print Dumper $res2->{share} ;
149              
150              
151             =head1 DESCRIPTION
152              
153             Handy when you want to know relationshops between your followers and followings
154             and when you wnat to compare your following and somebody's.
155              
156             =head1 METHOD
157              
158             =head2 diff
159              
160             run diff
161              
162             response hash
163              
164             =over 4
165              
166             =item B<not_following>
167              
168             get screen_names who you are not following but they are.
169              
170             =item B<not_followed>
171              
172             get screen_names who they are not following but you are.
173              
174             =item B<communicated>
175              
176             get screen_names who you are following them and also they follow you.
177              
178             =back
179              
180             =head2 comp_following( $twitter_id )
181              
182             compaire your following and somebody's
183              
184             response hash
185              
186             =over 4
187              
188             =item B<only_me>
189              
190             only you are following
191              
192             =item B<not_me>
193              
194             you are not following but somebody is following
195              
196             =item B<share>
197              
198             both you and somebody are following.
199              
200             =back
201              
202             =head2 xfollowing
203              
204             can get more that 100 followings.
205              
206             =head2 xfollowers
207              
208             can get more that 100 followers.
209              
210             =head1 SEE ALSO
211              
212             L<Net::Twitter>
213              
214             =head1 AUTHOR
215              
216             Tomohiro Teranishi <tomohiro.teranishi@gmail.com>
217              
218             =cut