-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeed2diasp.pl
More file actions
executable file
·193 lines (148 loc) · 4.7 KB
/
feed2diasp.pl
File metadata and controls
executable file
·193 lines (148 loc) · 4.7 KB
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
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use autodie;
use 5.10.0;
use Data::Dumper;
use JSON;
use XML::Feed;
use WWW::Mechanize;
use HTML::Strip;
use Readonly;
Readonly my $CONFIGPATH => 'feed2diasp.json';
Readonly my $FEEDSSEEN => 'feed2diasp-seen';
my $config = get_config();
my $mech = do_diaspora_login($config);
# status_update($mech, "I is diasporizing an ting. What a gwan?"); # DEBUG
open my $in, '<:utf8', $FEEDSSEEN;
my @seen = <$in>;
close $in;
my %seen = map { chomp; $_ => 1 } @seen;
my $max_items = $config->{max_items};
my $new_items = 0;
for my $feed (@{$config->{feeds}}){
my @feed_items = fetch_feed($feed);
open my $out, ">>:utf8", $FEEDSSEEN;
for(@feed_items) {
next if ($seen{$feed . $_->id});
my $msg = '';
$msg .= format_msg($_->summary->body) if ($_->summary && $_->summary->body);
$msg .= format_msg($_->content->body) if ($_->content && $_->content->body);
$msg .= "\nRead more at: "
. $_->link if ($_->link);
# say "Status update: " . $_->link . "\n" . $msg; # DEBUG
$mech = status_update($config, $mech, $msg);
$new_items++;
print $out $feed . $_->id . "\n";
last unless $new_items < $max_items;
}
close $out;
}
sub get_config {
open my $in, '<:utf8', $CONFIGPATH;
my @lines = <$in>;
close $in;
my $config = decode_json "@lines";
#die Dumper $config; #DEBUG
return $config;
}
sub do_diaspora_login {
my $config = shift;
my $login_url = "https://" . $config->{pod} . "/users/sign_in";
my $mech = WWW::Mechanize->new();
$mech->get($login_url);
my $form = $mech->form_id('new_user');
my $auth_token = $form->value('authenticity_token'); # not quite sure how this works.
my $res = $mech->submit_form(
form_id => 'new_user',
fields => {
'user[password]' => $config->{password},
'user[username]' => $config->{username},
'authenticity_token' => $auth_token,
'utf8' => '✓',
'user[remember_me]' => 0,
'commit' => 'Sign in',
},
);
die "Fuck it, can't login. Maybe you need to change the login details in $CONFIGPATH?" unless $res->is_success;
return $mech;
}
sub status_update {
my ($config,$mech,$msg) = (shift,shift,shift);
my $form = $mech->form_id('new_status_message');
my $auth_token = $form->value('authenticity_token');
my $res = $mech->submit_form(
form_id => 'new_status_message',
fields => {
'status_message[fake_text]' => $msg,
'status_message[text]' => $msg,
'utf8' => '✓',
'commit' => 'Share',
'aspect_ids[]' => 'all_aspects',
}
);
warn "Fuck it, can't post $msg" unless $res->is_success;
$mech->get("https://" . $config->{pod} . "/stream");
return $mech;
}
sub fetch_feed {
my $uri = shift;
my $feed = XML::Feed->parse(URI->new($uri));
return $feed->entries;
}
sub format_msg {
my $msg = shift;
my $hs = HTML::Strip->new();
return $hs->parse( $msg );
}
__END__
=head1 NAME
feed2diasp.pl : Get a feed and squish it into diaspora.
=head2 VERSION
0.1
=head1 SYNOPSIS
Edit feed2diasp.json to set up your account details then just:
$ feed2diasp.pl
You probably want to run this as a cronjob
=head1 DESCRIPTION
Install by untarring with
$ tar xvzf feed2diasp.tar.gz
Then
$ cd feed2diasp
You'll need to make sure you have Perl 5.10.0 and the CPAN dependencies.
$ sudo cpan XML::Feed Readonly HTML::Strip Data::Dumper WWW::Mechanize
Then copy the example config file and edit it to something more to your taste.
$ cp feed2diasp.json.example feed2diasp.json
You should make feed2diasp.pl executable wiith
$ chmod 750 feed2diasp.pl
Finally you can start updating your feed with
$ ./feed2diasp.pl
You may want to put that in a cron job.
=head2 OPTIONS
All options are set in feed2yaml.json . I used JSON rather than YAML to reduce dependencies
=head1 REQUIREMENTS
Perl 5.10.0
Data::Dumper
JSON
XML::Feed
WWW::Mechanize
HTML::Strip
Readonly
=head1 COPYRIGHT AND LICENCE
Copyright (C)2011 Charlie Harvey
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version
2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public
License along with this program; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Also available on line: http://www.gnu.org/copyleft/gpl.html
=cut