/
Superfeedr.pm
280 lines (225 loc) · 6.89 KB
/
Superfeedr.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package AnyEvent::Superfeedr;
use strict;
use warnings;
use 5.008_001;
our $VERSION = '0.02';
use Carp;
use AnyEvent;
use AnyEvent::Superfeedr::Notification;
use AnyEvent::XMPP::Client;
use AnyEvent::XMPP::Ext::Superfeedr;
use AnyEvent::XMPP::Ext::Pubsub;
use XML::Atom::Entry;
use URI::Escape();
our $SERVICE = 'firehoser.superfeedr.com';
use constant SUBSCRIBE_CHUNK_SIZE => 30;
# TODO:
# debug
# tests? worthwhile?
#
# Also, maybe more direct callbacks for sub/unsub
sub new {
my $class = shift;
my %param = @_;
my %filtered;
for ( qw{ jid password debug
on_notification on_connect on_disconnect on_error }) {
$filtered{$_} = delete $param{$_};
}
croak "Unknown option(s): " . join ", ", keys %param if keys %param;
my $superfeedr = bless {
debug => $filtered{debug} || 0,
jid => $filtered{jid},
password => $filtered{password},
}, ref $class || $class;
## can be passed to connect() too
$superfeedr->{on_connect} = $filtered{on_connect}
if $filtered{on_connect};
my $on_error = $filtered{on_error} || sub {
my ($cl, $acc, $err) = @_;
warn "Error: " . $err->string;
};
my $cl = AnyEvent::XMPP::Client->new(
debug => $superfeedr->{debug},
);
my $pass = $superfeedr->{password};
my $jid = $superfeedr->{jid}
or croak "You need to specify your jid";
$cl->add_account($jid, $pass, undef, undef, {
dont_retrieve_roster => 1,
});
$cl->add_extension(my $ps = AnyEvent::XMPP::Ext::Superfeedr->new);
$superfeedr->{xmpp_pubsub} = $ps;
$cl->reg_cb(
error => $on_error,
connected => sub {
$superfeedr->{connected} = 1;
$superfeedr->{on_connect}->($superfeedr)
if $superfeedr->{on_connect};
},
disconnect => sub {
$superfeedr->{connected} = 0;
( $filtered{on_disconnect}
|| sub { warn "Got disconnected from $_[2]:$_[3], $_[4]" }
)->($superfeedr, @_);
},
connect_error => sub {
my ($cl, $account, $reason) = @_;
my $jid = $account->bare_jid;
$on_error->("connection error for $jid: $reason");
},
);
if (my $on_notification = $filtered{on_notification} ) {
$ps->reg_cb(
superfeedr_notification => sub {
my $ps = shift;
my $notification = shift;
$on_notification->($notification);
},
);
}
$superfeedr->{xmpp_client} = $cl;
return $superfeedr;
}
sub connect {
my $superfeedr = shift;
my $on_connect = shift;
my $cl = $superfeedr->{xmpp_client}
or return;
if ($cl->{connected}) {
$superfeedr->{on_error}->("Already connected");
return;
}
$superfeedr->{on_connect} = $on_connect if $on_connect;
$cl->start;
}
sub subscribe {
my $superfeedr = shift;
$superfeedr->pubsub_method('subscribe_nodes', @_);
}
sub unsubscribe {
my $superfeedr = shift;
$superfeedr->pubsub_method('unsubscribe_nodes', @_);
}
sub pubsub_method {
my $superfeedr = shift;
my($method, @feed_uris) = @_;
my $cb = ref $feed_uris[-1] eq 'CODE' ? pop @feed_uris : sub { };
my $pubsub = $superfeedr->xmpp_pubsub;
unless ($pubsub) {
$superfeedr->event(error => "no pubsub extension available");
return;
}
my $con = $superfeedr->xmpp_connection;
unless ($con) {
$superfeedr->event(error => "Wait to be connected");
return;
}
my @chunk = splice @feed_uris, 0, SUBSCRIBE_CHUNK_SIZE;
my $res_cb;
my $chunk_cb = sub {
my ($chunk, $res_cb) = @_;
my @xmpp_uris = map { xmpp_node_uri($_) } @$chunk;
$pubsub->$method($con, \@xmpp_uris, $res_cb);
};
$res_cb = sub {
my $err = shift;
if ($err) {
$superfeedr->event(error => $err);
undef @chunk;
undef @feed_uris;
} else {
$cb->($_) for @chunk;
if (@feed_uris) {
@chunk = splice @feed_uris, 0, SUBSCRIBE_CHUNK_SIZE;
$chunk_cb->(\@chunk, $res_cb);
}
else {
undef $chunk_cb;
undef $res_cb;
}
}
};
$chunk_cb->(\@chunk, $res_cb);
}
sub xmpp_node_uri {
my $enc_feed = URI::Escape::uri_escape_utf8(shift, "\x00-\x1f\x7f-\xff");
# work around what I think is a but in AnyEvent::XMPP
#return "xmpp:$SERVICE?;node=$enc_feed";
return "xmpp:$SERVICE?sub;node=$enc_feed";
}
sub xmpp_pubsub {
my $superfeedr = shift;
return $superfeedr->{xmpp_pubsub};
}
sub xmpp_connection {
my $superfeedr = shift;
my $con = $superfeedr->{xmpp_connection};
return $con if $con;
my $client = $superfeedr->{xmpp_client} or return;
my $jid = $superfeedr->{jid};
my $account = $client->get_account($jid) or return;
$con = $account->connection;
$superfeedr->{xmpp_connection} = $con;
return $con;
}
1;
__END__
=encoding utf-8
=for stopwords
=head1 NAME
AnyEvent::Superfeedr - XMPP interface to Superfeedr service.
=head1 SYNOPSIS
use AnyEvent::Superfeedr;
my $callback = sub {
my AnyEvent::Superfeedr::Notification $notification = shift;
my $feed_uri = $notification->feed_uri;
my $http_status = $notification->http_status;
my $next_fetch = $notification->next_fetch;
printf "status %s for %s. next: %s\n",
$http_status, $feed_uri, $next_fetch;
for my XML::Atom::Entry $entry ($notification->entries) {
printf "Got: %s\n" $entry->title;
}
};
my $superfeedr = AnyEvent::Superfeedr->new(
jid => $jid,
password => $password
on_notification => $callback,
);
$superfeedr->connect;
AnyEvent->condvar->recv;
# Subsribe upon connection
my $superfeedr = AnyEvent::Superfeedr->new(
jid => $jid,
password => $password,
);
$superfeedr->connect(sub { $superfeedr->subscribe($feed_uri) });
# Periodically fetch new URLs from database and subscribe
my $timer = AnyEvent->timer(
after => 5,
interval => 5 * 60,
cb => sub {
my @new_feeds = get_new_feeds_from_database() or return;
$superfeedr->subscribe(@new_feeds);
},
);
$end->recv;
=head1 DESCRIPTION
WARNING: the interface is likely to change in the future.
Allows you to subscribe to feeds and get notified real-time about new
content.
This is a first version of the api, and probably only covers specific
architectural needs.
=head1 EXAMPLES
see in the eg/ directory of the distribution.
=head1 AUTHOR
Yann Kerherve E<lt>yannk@cpan.orgE<gt>
=head1 CONTRIBUTORS
Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 SEE ALSO
L<AnyEvent::XMPP> L<AnyEvent> L<http://superfeedr.com>
=cut