Categories: Top ::
About
Codejunkie
Monologues of a mobile retro coder.
skeezix[at]codejedi.com
www.codejedi.com
Subscribe
Subscribe to a syndicated RSS feed. I've
also made a Livejournal version and Ben whipped up an auto-RSS Livejournal
Blogs
DadHacker; epic rants.
ASCII@textfiles
Michael Mace
JoelOnSoftware
Bruce Schneier
Wil Wheaton
I, Cringely
WritingOnYourPalm
Dan Gillmor
GrandTextAuto
Freedom to Tinker
Mark's SysInternals Blog
A List Apart
Tam's Palm
Bytecellar retro goodness
Lost Garden
Bill Ing
Ben Combee
PocketGoddess
PocketFactory
Random Links
PalmInfoCenter
Zodiac Gamer
GP32x
Little Green Desktop
Atari Age
Penny Arcade
Hack-a-Day
Retro Remakes
SHMUPS!
Podcasts
1SRC
RetroGamingRadio
Recent Entries
| January 2009 | ||||||
|---|---|---|---|---|---|---|
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
| 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 |
Archives
A number of my friends have their own blogs on Livejournal and once in awhile someone requests I move over there instead of running my own blog. Of course I prefer to run it myself because I'm that sort of guy, but also so I have full control over the content (for future importing, exporting, republishing, etc). LJ lets people view a blog, but also allows them to integrate the display of friends blogs, so as to see an aggregated view of desirable content all in one place. I offer an RSS syndication option which is good for most people, but I've found out LJ doesn't let people tie into an RSS feed.... unless the feed owner-to-be (me in this case) pays $$$ to the LJ folks. Well, naturally, I have no interest in paying up for such a thing - its free to make an account, and free to post to an account, and free to blog any which way... so why not automate the whole process and not pay LJ to poach my content?
LJ is based on their open source software and offers numerous APIs for posting and otherwise manipulating blogs. For instance, you can log into the site and post a blog entry yourself, as several hundred thousand people do each day. You can pay to open an email gateway so you can make entries via email (such as from a PDA or phone), as well. Further, for coders and other applications, you can use one of a few APIs.. from a simple one, to a simple XML-RPM one, to heavier duty approaches. Well, I figured I'd slap together something quick.. real quick.. about an hours work. Not the most flexible or sexily written piece of script, but probably reusable by others, and she seems to work very nicely (I imported nearly one hundred blog entries over to LJ in just a few seconds!)
Really, the script just takes a file and posts it to the livejournal. In my blog (a Blosxom blog) each entry is just a file, where the filename includes the date up front, and the file content is free form after the first line - the first line is the entry title. So the script can be used for posting arbitrary textfiles to LJ, or in my case.. importing from one blog to LJ.
Theres some kludging going on, and its not written really prettily - no use of keen subroutines to translate text to encoded text nicely or the like, but with such a simple job I just wanted it to work, and be done in a few minutes.. so don't blame me if it eats your hard drive :) Use at your own risk, though I expect it to work..
./livejournal-push.pl LJ-USERNAME LJ-PASSWORD ./path/filename.txt
It prepends a blog path; you can toss that out if you like. is_testing turns on a stdout display, and is_live turns on actually posting to LJ. is_backdate turns on the LJ option to let you post an older entry than the newest entry (otherwise it rejects it.)
If you don't specify a file, it'll read from stdin and use the current date and time as posting date and time.
It spits out OK or FAILED+reason.
I enabled is_backdate and then did a 'find . -name "*txt" -exec ./livejournal-push.pl USERNAME PASSWORD {} \;' to post all the entries.. just like that.
The script is here:
#!/usr/bin/perl
# Experimenting with XML-RPC and LJ's Flat protocol
# This experiment posts a textfile to an LJ
#
# This script is lamely done, but it works!
#
# Of interest:
# HTML Escapes: http://www.html-reference.com/Escape.htm
# LJ Server docs: http://www.livejournal.com/doc/server/
#
use IPC::Open2;
$is_testing = 0;
$is_live = 1;
#$is_backdate = "&prop_opt_backdated=1";
$is_backdate = "";
$username = shift;
$password = shift;
$filename = shift; # optional argument
if ( ! $password ) {
print "$0: Requires username and password on commandline.\n";
exit ( 0 );
}
# strip filename to filename (ignore path)
#
$filename =~ /(.*)\/(.*)/;
$basename = $2;
$filename =~ /(.*)blog\/(.*)/;
$blogpath = "http://www.codejedi.com/cgi-bin/blog.cgi/$2";
$blogpath =~ s/\.txt/\.blog/;
# first, build up content string
#
$subject = '';
$content = '';
$message = '';
if ( $filename ) {
# prepend path to blog
$content = "Full article is here: $blogpath\n\n";
# pick up from file
open ( FILE, "$filename" ) ||
die ( "Couldn't open $filename: $!\n" );
while ( $inline = ) {
if ( $subject eq '' ) {
$subject .= $inline;
} else {
$content .= $inline;
}
} # while
close ( FILE );
} else {
# poach content from stdin
while ( $inline = ) {
if ( $subject eq '' ) {
$subject .= $inline;
} else {
$content .= $inline;
}
} # while
}
# strip trailing newlines
#
$subject =~ s/\n$//;
$content =~ s/\n$//;
# determine dates
#
if ( $filename ) {
# poach date from filename, blosxom style
# ex: 20050429new_blog.txt
$sec = $min = $hour = 0;
$year = substr ( $basename, 0, 4 );
$mon = substr ( $basename, 4, 2 );
$mday = substr ( $basename, 6, 2 );
} else {
# assume current date
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$mon += 1;
}
# translate special characters
#
# spaces
$subject =~ s/ /+/g;
$content =~ s/ /+/g;
# amp
$subject =~ s/&/%26/g;
$content =~ s/&/%26/g;
$subject =~ s/\%3c/g;
$content =~ s/\%3c/g;
$subject =~ s/\>/%3e/g;
$content =~ s/\>/%3e/g;
$subject =~ s/\?/%3f/g;
$content =~ s/\?/%3f/g;
$subject =~ s/\./%2e/g;
$content =~ s/\./%2e/g;
$subject =~ s///g;
$content =~ s///g;
$subject =~ s/'/%27/g;
$content =~ s/'/%27/g;
#$subject =~ s/\n/%0d/g;
#$content =~ s/\n/%0d/g;
$subject =~ s/\"/%22/g;
$content =~ s/\"/%22/g;
$subject =~ s/\!/%21/g;
$content =~ s/\!/%21/g;
$subject =~ s/\:/%3a/g;
$content =~ s/\:/%3a/g;
$subject =~ s/\//%2f/g;
$content =~ s/\//%2f/g;
$subject =~ s/=/%3d/g;
$content =~ s/=/%3d/g;
# assemble message
#
$message = "mode=postevent&user=$username&password=$password&event=$content&subject=$subject$is_backdate&year=2005&mon=$mon&day=$mday&hour=$hour&min=$min\n";
$message_length = length ( $message ) - 1;
if ( $is_testing ) {
print "Subject: -------------------------------------------------------\n";
print $subject;
print "Content: -------------------------------------------------------\n";
print $content;
print "Message: -------------------------------------------------------\n";
print $message;
}
# second-to-last, build header
#
$header = <<EOF
POST /interface/flat HTTP/1.0
Host: www.livejournal.com
Content-type: application/x-www-form-urlencoded
Content-length: $message_length
EOF
;
# last, spit header and content into LJ's webserver
#
if ( $is_testing ) {
print "API: -----------------------------------------------------------\n";
print $header;
print $message;
print "----------------------------------------------------------------\n";
}
# post it!
#
if ( $is_live ) {
# telnet to LJ
$pid = open2 ( $rdrfh, $wtrfh, '/usr/bin/telnet',
'www.livejournal.com', '80' );
# post entry
print $wtrfh $header;
print $wtrfh $message;
# await response
while ( $inline = <$rdrfh> ) {
if ( $inline =~ /success/ ) {
$inline = <$rdrfh>;
if ( $inline =~ /FAIL/ ) {
close ( $rdrfh );
close ( $wtrfh );
print "FAILED\n";
exit ( -1 );
} else {
close ( $rdrfh );
close ( $wtrfh );
print "OK\n";
exit ( 0 );
}
} elsif ( $inline =~ /errmsg/ ) {
$inline = <$rdrfh>;
print "ERROR: $inline\n";
}
}
# report success/failure
}
[ Category: / technology / blogging ] [link] [Comments]>