-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlusFeed.pm
230 lines (194 loc) · 5.72 KB
/
PlusFeed.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package PlusFeed;
use strict;
use warnings;
use Carp;
use Google::Plus;
require 5.10.1;
use XML::RSS;
use utf8;
our $VERSION = '0.05';
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
$self->_initialize(@_);
return $self;
}
sub _initialize {
my $self = shift;
my $hash = {@_};
croak "Please provide Google+ API Key" if (not $hash->{key});
$self->{_key} = $hash->{key};
my $plus = Google::Plus->new(key => $hash->{key});
$self->{_plus_object} = $plus;
croak "Please provide Google+ User Id" if (not $hash->{user});
$self->{_user} = $hash->{user};
my $rss = new XML::RSS(version => '2.0');
$self->{_rss_object} = $rss;
# page only consists 20 items for now
# Google::Plus not supporting maxItems yet
$self->{_pages} = $hash->{pages};
$self->{_pages} //= 0; # default is 0 means all pages
# caching
# cache - must be already initialized CHI object
$self->{_cache_object} = $hash->{cache};
$self->{_cache_ttl} = $hash->{cache_ttl};
$self->{_cache_ttl} //= 300; # default seconds
return 1;
}
# make body of RSS entry using G+ activity item
# many raw html magic :)
sub _make_body {
my $self = shift;
my $item = shift;
no warnings;
my $object = $item->{object};
my $body = $item->{annotation};
if ($body) { $body = $body . "<br />"; }
if ($object->{attachments}) {
for my $att (@{$object->{attachments}}) {
if ($att->{objectType} eq 'photo') {
$body =
$body
. '<a href="'
. $att->{fullImage}->{url}
. '"><img src="';
$body = $body . $att->{image}->{url} . '"></a>';
}
elsif ($att->{objectType} eq 'video') {
$body = $body . '<a href="' . $att->{url} . '"><img src="';
$body = $body . $att->{image}->{url} . '"></a>';
}
else {
$body =
$body
. ' <a href="'
. $att->{url} . '">'
. $att->{displayName} . '</a>';
}
}
}
if ($body) { $body = $body . "<br />"; }
$body = $body . $object->{content};
$body = $body . ' <a href="' . $object->{url} . '">...</a>';
return $body;
}
### put string in cache
sub _cache_put {
my $self = shift;
my $string = shift;
my $namespace = shift;
my $key = shift;
my $cache = $self->{_cache_object};
return if not defined $cache;
# put string in cache
$cache->set($namespace . $key, $string, $self->{_cache_ttl});
}
### get tring from cache
sub _cache_get {
my $self = shift;
my $namespace = shift;
my $key = shift;
my $cache = $self->{_cache_object};
return if not defined $cache;
return $cache->get($namespace . $key);
}
sub get_rss_stream {
my $self = shift;
my $hash = {@_};
my $plus = $self->{_plus_object};
my $rss = $self->{_rss_object};
my $pages = $self->{_pages};
my $user = $self->{_user};
my $rss_stream = $self->_cache_get('RSS', $user);
return $rss_stream if defined $rss_stream;
my $person = $plus->person($user);
$self->{_person_object} = $person;
my $person_name = $person->{displayName};
$rss->channel(
title => "Google+ for $person_name",
link => "https://plus.google.com/$user/posts",
description => "Google+ RSS feed for $person_name"
);
# get this person's activities
my $activities = $plus->activities($user, 'public');
$self->{_activities_object} = $activities;
while ($activities->{nextPageToken}) {
my $next = $activities->{nextPageToken};
for my $item (@{$activities->{items}}) {
my $body = $self->_make_body($item);
$rss->add_item(
title => $item->{title},
description => $body,
guid => $item->{url},
url => $item->{url},
author => $item->{actor}->{displayName},
date => $item->{published},
);
}
$activities = $plus->activities($user, 'public', $next)
if defined $next && $self->{_pages} >= 0;
--$self->{_pages} if $self->{_pages} > 0;
}
# put string in cache
$self->_cache_put('RSS', $user, $rss->as_string);
# renew objects
$self->{_plus_object} = $plus;
$self->{_rss_object} = $rss;
return $rss->as_string;
}
### accessors below
### was added for clarity :)
sub get_api_key {
my $self = shift;
return $self->{_key};
}
sub set_api_key {
my $self = shift;
my $key = shift;
$self->{_key} = $key;
$self->_initialize(
key => $self->{_key},
user => $self->{_user},
pages => $self->{_pages},
cache => $self->{_cache},
cache_ttl => $self->{_cache_ttl}
);
}
sub get_user_id {
my $self = shift;
return $self->{_user};
}
sub set_user_id {
my $self = shift;
my $user = shift;
$self->{_user} = $user;
$self->_initialize(
key => $self->{_key},
user => $self->{_user},
pages => $self->{_pages},
cache => $self->{_cache},
cache_ttl => $self->{_cache_ttl}
);
}
sub get_plus_object {
my $self = shift;
return $self->{_plus_object};
}
sub get_person_object {
my $self = shift;
return $self->{_person_object};
}
sub get_activities_object {
my $self = shift;
return $self->{_activities_object};
}
sub get_rss_object {
my $self = shift;
return $self->{_rss_object};
}
sub get_cache_object {
my $self = shift;
return $self->{_cache_object};
}
1;