The Twilight Report - tweet [entries|archive|friends|userinfo]
Грэм

[ website | White Dactyl Photography ]
[ userinfo | deadjournal userinfo ]
[ archive | journal archive ]

tweet [Sep. 24th, 2008|04:16 pm]
Previous Entry Add to Memories Tell A Friend Next Entry
[Tags|, , , , , , , , , ]

  • 23 September 2008 04:02pm: Something for everyone, an opera tonight! First trip to the Sydney Opera House to see them sing and stuff.
  • 22 September 2008 02:22pm: I am going to be in New York/New Jersey 4-18 October.
  • 22 September 2008 08:29am: City jackhammers are my wake up call at The Point. Is it Monday again already?
  • 21 September 2008 10:19pm: On the train ride back from Canberra the kangaroos were scattering in fear as we coasted through landscape. Back in Sydney.

twitter.com/plicease

linkReply

Comments:
[User Picture]From: [info]drkryl
2008-09-24 03:45 am (UTC)

(Link)

Hey, do you use LoudTwitter or something else? I've been trying to figure out the best way to post everywhere with a single post, but nothing plays well together. I was actually looking at InsaneJournal - they seem to have more features than DJ.
[User Picture]From: [info]plicease
2008-09-24 03:49 pm (UTC)

(Link)

I wrote my own perl script to do it so it integrates in with my website. It's moderately simple (the most complicated thing is dealing with dates), where it parses the RSS using XML::Simple and creates a journal entry on my website, which then gets sync'ed with DJ and LJ.

Right now I run it manually, but I'm considering having it run from a cron job on nullray nightly.
[User Picture]From: [info]drkryl
2008-09-26 11:12 am (UTC)

(Link)

Cool. I'm trying to educate myself in things like that. I guess I should learn Perl first :) Could I see your script or maybe get a quick tutorial at some point?
[User Picture]From: [info]plicease
2008-09-26 02:29 pm (UTC)

(Link)

Sure. The script is in /web/perl/NX/scripts/twittersync.pl

It doesn't have any comments though, so I am not sure how useful it would be. It's also setup to insert the results into the Postgres database on nullray and then calls other scripts to actually post to nullray.

Basically you use LWP to grab the RSS feed (LWP is the standard way to grab stuff that can be addressed with a URL):

use LWP::UserAgent;
my $ua = new LWP::UserAgent;
$ua->evn_proxy;
my $url = "http://feedurl...whatever";
my $response = $ua->get($url);
die $response->status_line, " on $url" unless $response->us_success;
my $raw_rss = $response->decode_content;

Now you have the raw RSS (XML) and you need to parse it. There are lots of ways to do this, but the simplest (as long as you don't care about XSD verification or anything like that) is XML::Simple

use XML::Simple qw( XMLin );
my $feed = XMLin($raw_rss);

This creates a "hash of hashes" that represents the hierarchical nature of the XML. If you want to see what the results look like in Perl terms, use Data::Dumper (YAML is also a handy way to look at this kind of data, but it is cross-language and not Perl specific).

use Data::Dumper qw( Dumper );
print Dumper($feed);

But anyhow, to iterate over each item in the RSS feed, this is what you do:

my @items = ref $items eq 'ARRAY' ? @$items : ($items) ;
foreach my $item (@items)
{
my $link = $item->{link};
my $description = $item->{description};
my $pub_date = $item->{pubDate};
# ...
# do something with these values
}

You need the ?: operator in the assignment of @items, because if there is only one item it will be stored as a scalar instead of a list (this is a limitation of XML::Simple, it can't tell the difference between something that should be a single XML element and something that is a list of just one XML element).

The script that posts to lj is /web/perl/NX/scripts/ljsync.pl and also devoid of many comments. It's also probably not directly useful since it grabs items from the database. Basically I wrote my own LJ client code NX::LiveJournal (lives in /web/perl/NX/LiveJournal), because I wasn't happy with the ones that are found on CPAN. It's actually quite good (I was at one point thinking about submitting it to CPAN) but the interface is not documented at all (of course! oh dear).

use NX::LiveJournal::Client;
my $lj = new NX::LiveJournal::Client(
username => 'blah',
password => 'secret',
server => 'www.livejournal.com',
mode => 'cookie', # should be 'challenge' for deadjournal
);
die "error connecting to www.livejournal.com $NX::LiveJournal::Client::error" unless defined $lj;

Determining the mode is a process of trial and error. 'cookie' is better (because it doesn't mean 2 HTTP request for every 1 real request), but the DJ server is older and only works with 'challenge'.

$event = $lj->create;
$event->eventtime('2008-09-26 04:30:00');
$event->subject('subject');
$event->event('actual message text');
$event->settags(qw( funny story test ));
#$event->setprop(opt_preformatted => 1); # if you don't want LJ to add
tags and stuff
$event->update;

the NX::LiveJournal stuff uses mostly the terminology used in the LJ XML-RPC API, and some of it (to me at least) seems a bit odd. It's pretty complete, including all the stuff that is in the LJ API, even stuff that I don't really use. It isn't well documented, but if you use it in conjunction with the LJ API documentation it might make sense.

The other option is using LJ::Simple (which is installed on nullray). I think that it has serious problems though. Net::LiveJournal is a joke so don't even go there.

Incidentally, documentation for everything that is installed in the nullray perl can be found on this page:

www.wdlabs.com/doc

click on "Perl 5.10.0 (/web/bin/perl)" (it requires your nullray username and password).

If you are interested in working on any of this I'd be happy to give you access to the subversion repository. :)
[User Picture]From: [info]plicease
2008-09-26 03:45 pm (UTC)

(Link)

"It's also setup to insert the results into the Postgres database on nullray and then calls other scripts to actually post to nullray."

That should have read as

"It's also setup to insert the results into the Postgres database on nullray and then calls other scripts to actually post to LJ."
[User Picture]From: [info]plicease
2008-09-26 03:50 pm (UTC)

(Link)

another correction:

#$event->setprop(opt_preformatted => 1); # if you don't want LJ to add <br/> tags and stuff

setting opt_preformatted basically means that you are writing raw HTML and you don't want LJ inserting extra line breaks or anything.
[User Picture]From: [info]drkryl
2008-09-26 06:43 pm (UTC)

(Link)

wow thanks :) I wonder if I just got more than I bargained for, I'll try to understand what's what :) Should we delete this, to prevent knowledge leak? :)
[User Picture]From: [info]plicease
2008-09-27 01:08 am (UTC)

(Link)

meh. None of this is secret. Most of it is just standard Perl that can be found on CPAN.