#!/usr/bin/perl -w ############################################################################## # Original source by Choric http://www.livejournal.com/users/choric/ # Original source available at http://printf.net/lj-music.pl.txt # Modifications to the original by Corvar # http://www.theonering.net/staff/corvar: # 1) no echo of password # 2) resolve www.livejournal.com into IP # 3) save music data and date of last item in ~/.lj-music.username # 4) only grab new items. # Modified source at http://www.theonering.net/staff/corvar/lj-music.pl.txt ############################################################################## use strict; use IO::Socket; use Data::Dumper; my $ljaddr = join(".",unpack("C4",(gethostbyname("www.livejournal.com"))[4])); my $savefile = $ENV{'HOME'}."/.lj-music."; my $ljsock = IO::Socket::INET->new( PeerAddr => "$ljaddr", PeerPort => "80", Proto => "tcp", Type => SOCK_STREAM) or die "Couldn't connect to the livejournal.com web server : $@\n"; # Declare some vars. my (@items, @music); my ($user, $pass, $lastsync, $key, $value, $total); my $counter = 1; my %artistes; print "Enter your livejournal username: "; $user = ; chomp $user; system "stty -echo"; print "Enter your livejournal password: "; $pass = ; chomp $pass; system "stty echo"; print "\n"; if( -e $savefile.$user) { my($data); open(REST,$savefile.$user); while() { $data = $data."$_"; } close(REST); eval($data); } # Set up before printing to ljsock. my $string = "mode=syncitems&user=$user&password=$pass"; if($lastsync) { $string = $string."&lastsync=$lastsync"; } my $line1 = "POST /cgi-bin/log.cgi HTTP/1.0\r\n"; my $line2 = "Host: www.livejournal.com\r\n"; my $line3 = "Content-type: application/x-www-form-urlencoded\r\n"; my $line4 = "Content-length: " . length($string) . "\r\n"; my $line5 = "\r\n"; # Retrieve all journal entries, using the syncitems mode. print $ljsock $line1 . $line2 . $line3 . $line4 . $line5 . $string. "\r\n\r\n"; my $reply = <$ljsock>; while(<$ljsock>) { if(/^L-/) { s/L-//; push @items, $_; } if ($_ =~ /^sync_count$/) { $total = <$ljsock>; chomp $total; } } @items = sort{$a <=> $b} @items; print "\n\nTotal number of journal entries to download: $total.\n\n"; my($x); foreach my $item (@items) { chomp($item); $ljsock = IO::Socket::INET->new( PeerAddr => "$ljaddr", PeerPort => "80", Proto => "tcp", Type => SOCK_STREAM) or die "Couldn't connect to the livejournal.com web server : $@\n"; my $string = "mode=getevents&user=$user&password=$pass&selecttype=one&itemid=$item"; my $line1 = "POST /cgi-bin/log.cgi HTTP/1.0\r\n"; my $line2 = "Host: www.livejournal.com\r\n"; my $line3 = "Content-type: application/x-www-form-urlencoded\r\n"; my $line4 = "Content-length: " . length($string) . "\r\n"; my $line5 = "\r\n"; # Retrieve all journal entries, using the getevents mode. print "Retrieving itemid: $item. ($counter of $total)\n"; $counter++; print $ljsock $line1 . $line2 . $line3 . $line4 . $line5 . $string. "\r\n\ r\n"; my $reply = <$ljsock>; while(<$ljsock>) { if (/^current_music$/) { <$ljsock>; my $found = <$ljsock>; push @music, $found; } if(/^events_1_eventtime$/) { $lastsync = <$ljsock>; chop($lastsync); } } } foreach my $song (@music) { $song =~ s/\.//g; if ($song =~ /(.*) - (.*)/) { push @{$artistes{$1}}, $2 }; } print "\nResults:\n--------\n\n"; my $datadump = Data::Dumper->Dump([\%artistes,$lastsync],[qw(*artistes lastsync)]); open(SAVE,">".$savefile.$user); print SAVE "$datadump"; close(SAVE); print "$_: ",scalar @{$artistes{$_}},"\n" foreach sort { scalar @{$artistes{$b}}<=>scalar @{$artistes{$a}} || ($a cmp $b) } keys %artistes;