--- nfo/perl/scripts/giantdisc/bin/gdclient.pm 2005/03/08 16:00:57 1.1 +++ nfo/perl/scripts/giantdisc/bin/gdclient.pm 2005/03/08 20:53:46 1.2 @@ -8,8 +8,9 @@ use Data::Dumper; our @ISA = qw( Exporter ); -our @EXPORT = qw( gd_connect gd_disconnect gd_command ); +our @EXPORT = qw( gd_connect gd_disconnect gd_command lcddi_parse_response ); +my $DEBUG = 0; my $sock; @@ -30,13 +31,93 @@ sub gd_command { my $cmd = shift; - my @args = @_; - - $sock->send(join("\t", $cmd, @args) . "\n"); + my $args = shift; + my $read_response = shift; + my @args; + if ($args) { @args = @$args; } + + my $command = join("\t", $cmd, @args); + print "command: '", $command, "'\n" if $DEBUG; + if ($sock->send($command . "\n")) { + if ($read_response) { + #print "read-response", "\n" if $DEBUG; + return gd_read_response(); + } + } } - -#my $data; -#$sock->read(1024,$data) until $sock->atmark; + +sub gd_read_response { + my $buffer; + + my $char = ''; + my $doread = 1; + while ($doread) { + my $newchar; + $doread = $sock->read($newchar, 1); + + # terminate socket if buffer is empty and one newline has arrived + last if (!$buffer && $newchar eq "\n"); + + # terminate socket read when two consecutive newlines have arrived + last if (!$doread || ($newchar eq $char and $char eq "\n")); + + $char = $newchar; + $buffer .= $char; + } + + chomp($buffer) if $buffer; + return $buffer; +} + + +=pod example payload: +my $payload = [ + 'Guano Apes', + 'Open Your Eyes', + '', + '', + '', + '', + '0', + '0', + '189', + '0', + '1e0c1f12', + '1', + '', + '0', + '0', + '', + '', + 'mp3 192' + ]; + +=pod field definition: +$columns = [qw( artist title genre1 genre2 year lang type rating length source sourceid tracknb audiofile condition voladjust created id bitrate )]; + +=cut + + +sub lcddi_parse_response { + my $payload = shift; + my @lines = split("\n", $payload); + + # prepare album metadata + my $label = shift(@lines); + $label =~ s/\r//g; + my @title = split("\t", $label); + + my $data = { 'artist' => $title[0], 'album' => $title[1], 'cddb_id' => $title[2], 'tracks' => [] }; + foreach my $line (@lines) { + chomp($line); + #print $line, "\n"; + my @entry = split("\t", $line); + splice(@entry, -3, 1); + push(@{$data->{'tracks'}}, \@entry); + } + + return $data; +} ###############################################################################