/
birdcage.pl
executable file
·331 lines (254 loc) · 8.4 KB
/
birdcage.pl
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/perl
#
# birdcage.pl
#
$version="0.3.5";
#
# Copyright (C) 2007-2009 Greck Cannon
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the Greck Cannon nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY GRECK CANNON ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL GRECK CANNON BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
###############################################################################
#
# This script is designed to dump a day's worth of Tweets into your
# LiveJournal. The API is a bit weird, and only allows you to reach
# back at most 20 statuses, so... to save a ton of logic, birdcage.pl
# is hardcoded to read all Tweets (unless you make more than 20 in a
# day!) since midnight today. The two caveats are: set the time zone
# below to match whatever time zone your LiveJournal is in, and have
# cron run birdcage.pl at one minute until midnight, *adjusted for
# any difference in time zones between your server and LiveJournal*.
# E.g., if your LJ is in US/Central and your server is in US/Pacific,
# schedule the cron job to run at 21:59.
#
# birdcage.pl needs the following modules, all available either in the
# standard Perl distribution, or via CPAN:
#
# LWP::Simple
# Date::Manip
# LJ::Simple
#
# CHANGELOG
#
#
# TO-DO
# - update all the in-line documentation
# - lots of error checking
#
# 0.3.5
# - change \n to <BR> to preserve original intent
# - added link in subject (we'll see if that breaks any styles)
# - anchored s{</text>}{} to EOL... just in case
#
# 0.3.4
# - handle <text>...</> with embedded \n
# - got rid of an inadvertently-left-in line of debug code
#
# 0.3.3
# - added version to LJ post subject for metrics
#
# 0.3.2
# - added the <!--id--> comment at the beginning of each row
#
# 0.3.1
# - added support for Windows to use birdcage.rc in dirname($0)
#
# 0.3
# - added rudimentary URL detection/linking
# - fixed up entity tags for < and >
# - fixed up the "not respecting since" problem with since_id
# - initial hack at config in ~/.birdcagerc
# - added detection of time zone offset from statuses
# - notice if there are no statuses, and don't post!
#
# 0.2.2
# - Added BSD license notification.
#
# 0.2.1
# - Added max 3 retries when logging in to LJ and again when posting.
#
# 0.2
# - Switched from using QuickPost to using the full PostEntry method.
# - Added support for userpics and tags.
#
# 0.1
# - Initial release.
#
#
# OK, all the config goes in ~/.birdcagerc now!
# all the same options as before, usage hasn't changed
# now go make it, there's no error checking yet :-)
# and don't forget to chmod it 600 since it has your passwords in it!
# note, tz is gone, it's read from the status stream now
#
# twitter_user=<user>
# lj_user=<user>
# lj_pass=<pass>
# # lj_sec can be one of public, friends, private
# lj_sec=friends
# # lj_tags can be a comma-separated list
# lj_tags=twitter
# # userpic use "keywords" value
# lj_userpic=twitterrific
#
use Cwd qw(abs_path);
use File::Basename;
use LWP::Simple;
use Date::Manip;
use LJ::Simple;
&Date_Init("TZ=UTC");
if ( $^O eq "MSWin32" ) {
$configspec = dirname(abs_path($0)) . "/birdcage.rc";
} else {
$configspec = (getpwuid($>))[7] . "/.birdcagerc";
}
if ( open CONFIG, "$configspec" ) {
while ( <CONFIG> ) {
next if /^#/;
chomp;
( $key, $value ) = split '=', $_, 2;
$config{$key} = $value;
}
close CONFIG;
} else { die "$0: I think your config should be in `$configspec', but couldn't open it"; }
$config{'lj_site'} = 'livejournal.com' unless ($config{'lj_site'});
$url = "http://twitter.com/statuses/user_timeline.xml?screen_name=" . $config{'twitter_user'} ."&count=";
$url .= $config{'since_id'} ? "100&since_id=$config{'since_id'}" : "20";
for ( $i = 0; $i < 3; ++$i ) {
if ( $tweets_xml = get("$url") ) {
last;
} else {
sleep 5;
}
}
if ( not defined($tweets_xml) ) {
die "$0: Twitter request failed";
}
@tweets = split '\n', $tweets_xml;
#
# I know, there's a real XML parser I should be using...
# (and this becomes more apparent the bigger of a hack this becomes)
#
while ( $_ = shift @tweets ) {
if ( /<id>(.+)<\/id>/ ) {
$id = $1;
$latest_id = $id if $id > $latest_id;
}
if ( /<created_at>(.+)<\/created_at>/ ) {
$tstamp = $1;
}
if ( /<text>(.*)/ ) {
$text = $1;
if ( not $text =~ /<\/text>/ ) {
do {
$_ = shift @tweets;
$text .= "<BR>$_";
} while ( not $_ =~ /<\/text>/ );
}
$text =~ s{</text>$}{};
$text =~ s{(http://.+?)(\s|$)}{<a href="\1">\1</a>\2}g;
$text =~ s{&lt;}{<}g;
$text =~ s{&gt;}{>}g;
}
if ( /<user>/ ) {
while ( $_ = shift @tweets ) {
last if /<\/user>/;
if ( /<utc_offset>(.+)<\/utc_offset>/ ) {
$utc_offset_seconds = $1;
$utc_offset_hours = $utc_offset_seconds/3600;
$utc_offset_minutes = ($utc_offset_seconds-(3600*$utc_offset_hours))/60;
if ( $utc_offset_hours < 0 ) {
$direction = "-";
$utc_offset_hours *= -1;
} else {
$direction = "+";
}
$utc_offset = sprintf("%s%02d%02d",$direction,$utc_offset_hours,$utc_offset_minutes);
}
}
}
if ( /<\/status>/ ) {
$statuses++;
$tstamp = UnixDate(Date_ConvTZ(ParseDate($tstamp),"UTC",$utc_offset),"%H:%M:%S");
unshift @entry, "<li><a href=\"http://twitter.com/#!/$config{'twitter_user'}/status/$id\">$tstamp</a> $text</li>"; # unless (m/^\@/, $text);
}
}
$entry = '<ul>';
foreach $_ ( @entry ) {
$entry .= "$_\n";
}
$entry .= '</ul>';
print $entry;
if ( not $statuses ) {
exit 0;
}
for ( $i = 0; $i < 3; ++$i ) {
if ( $lj = new LJ::Simple ( { user => $config{'lj_user'}, pass => $config{'lj_pass'}, site => $config{'lj_site'} } ) ) {
last;
} else {
sleep 5;
}
}
if ( not defined($lj) ) {
die "$0: Failed to login to LiveJournal ($LJ::Simple::error)";
}
%lj_event=();
$lj->NewEntry(\%lj_event)
or die "$0: Failed to create new entry ($LJ::Simple::error)";
$lj->SetProtect(\%lj_event,$config{'lj_sec'})
or die "$0: Failed to set protection ($LJ::Simple::error)";
$lj->SetEntry(\%lj_event,$entry)
or die "$0: Failed to set entry ($LJ::Simple::error)";
$lj->SetSubject(\%lj_event,"Twitter summary")
or die "$0: Failed to set subject ($LJ::Simple::error)";
if ( defined $config{'lj_tags'} ) {
@lj_tags = split ',', $config{'lj_tags'};
$lj->Setprop_taglist(\%lj_event,@lj_tags)
or die "$0: Failed to set tags ($LJ::Simple::error)";
}
$lj->Setprop_preformatted(\%lj_event,1)
or die "$0: Failed to set formatting ($LJ::Simple::error)";
if ( defined $config{'lj_userpic'} ) {
$lj->Setprop_picture_keyword(\%lj_event,$config{'lj_userpic'})
or die "$0: Failed to set userpic ($LJ::Simple::error)";
}
for ( $i = 0; $i < 3; ++$i ) {
( $lj_item_id, $lj_anum, $lj_html_id )=$lj->PostEntry(\%lj_event);
if ( defined($lj_item_id) ) {
last;
} else {
sleep 5;
}
}
if ( not defined($lj_item_id) ) {
die "$0: Failed to post entry ($LJ::Simple::error)";
} else {
$config{'since_id'} = $latest_id;
if ( open CONFIG, ">$configspec" ) {
foreach $c ( keys %config ) {
print CONFIG "$c=$config{$c}\n";
}
close CONFIG;
}
}