Changeset 860


Ignore:
Timestamp:
Oct 11, 2009, 3:18:59 AM (10 years ago)
Author:
bruno
Message:
  • Highly improved version which allows caching of CDDB resuls, and sending to freedb
File:
1 edited

Legend:

Unmodified
Added
Removed
  • projects/CDDBeditor/bin/CDDBeditor

    r858 r860  
    2424use CDDB_get qw( get_cddb get_discids );
    2525use Encode;
     26#use Linux::CDROM;
     27use Device::Cdio::Device;
     28use Device::Cdio::Track;
     29use File::Copy;
     30use File::Basename;
    2631
    2732# Global variables
     
    145150my $artist_label = Newt::Label("Artist: ");
    146151my $empty_label = Newt::Label("      ");
    147 #my $data_label = Newt::Label("Data: ");
    148 #my $raw_label = Newt::Label("Raw: ");
    149152my $title_label = Newt::Label("Title: ");
    150153my $year_label = Newt::Label("Year: ");
    151 my $id_label = Newt::Label("Id: ");
     154my $id_label = "Id: ";
     155my $rev_label = "Revision: ";
    152156my $genre_label = Newt::Label("Genre: ");
    153157my $category_label = Newt::Label("Category: ");
     
    172176my %ce_config;
    173177$ce_config{"CDDB_HOST"}="freedb.freedb.org";
     178$ce_config{"CDDB_USER"}="freedb-submit\@freedb.org";
     179$ce_config{"CDDB_CC"}="bruno\@victoria.frmug.org";
    174180$ce_config{"CDDB_PORT"}=888;
    175181#$ce_config{"CDDB_PORT"}=80;
     
    182188$ce_config{"HTTP_PROXY"}=$ENV{http_proxy} if $ENV{http_proxy};
    183189$ce_config{"input"}=0;
    184 $ce_config{"HELLO_ID"} = "bruno musique-ancienne.org fastrip 0.77";
    185190$ce_config{"PROTO_VERSION"} = 6;
     191$ce_config{"DOMAIN"} = "musique-ancienne.org";
     192$ce_config{"USER"} = "bruno";
     193$ce_config{"HELLO_ID"} = "$ce_config{\"USER\"} $ce_config{\"DOMAIN\"} fastrip 0.77";
     194
     195# Copied from CDDB.pm
     196# Determine whether we can submit changes by e-mail.
    186197
    187198# First panel for configuration parameters
     
    267278$send_button->Tag("Send");
    268279
     280my $firsttime = 1;
     281my $ce_cd;
     282my $artist_entry;
     283my $title_entry;
     284my $category_entry;
     285my $genre_entry;
     286my $tno_entry;
     287my $year_entry;
     288my @track_entry;
     289
    269290# Entering the loop
    270291for (my $i=0 ; ; $i++) {
     
    279300    Newt::Refresh();
    280301
    281     # CDDB query
    282     my %ce_cd = get_cddb(\%ce_config);
    283 
     302    # CDDB query if first time
     303    if ($firsttime eq 1) {
     304        ($ce_cd) = get_cddb(\%ce_config)
     305    }
     306    $firsttime = 0;
     307
     308    if (not defined $ce_cd->{title}) {
     309        # Get the disc id first
     310        my $id = get_discids($ce_config{"CD_DEVICE"});
     311        $ce_cd->{id} = sprintf "%08x", $id->[0];
     312
     313        # Try to find it locally
     314        my @d = glob("$ENV{'HOME'}/.cddb/*/$ce_cd->{id}");
     315        foreach my $d (@d) {
     316            $ce_cd->{cat} = basename(dirname($d));
     317            open(DISC,$d) || die "Unable to open $d";
     318            # Idea taken from CDDB_get
     319            while (<DISC>) {
     320                push @{$ce_cd->{raw}},$_;
     321                if (/^TTITLE(\d+)\=\s*(\S.*)/) {
     322                    chomp($2);
     323                    $ce_cd->{track}[$1]=$2;
     324                } elsif(/^DTITLE=\s*(\S.*)/) {
     325                    my $t = $1;
     326                    if ($t =~ /\//) {
     327                        ($ce_cd->{artist},$ce_cd->{title}) = split(/\s\/\s/,$t);
     328                    } else {
     329                        $ce_cd->{artist}=$t;
     330                        $ce_cd->{title}=$t;
     331                    }
     332                    chomp($ce_cd->{title});
     333                } elsif(/^DYEAR=\s*(\d+)/) {
     334                    $ce_cd->{year} = $1;
     335                } elsif(/^DGENRE=\s*(\S.*)/) {
     336                    $ce_cd->{genre} = $1;
     337                } elsif(/^\#\s+Revision:\s+(\d+)/) {
     338                    $ce_cd->{revision} = $1;
     339                }
     340            }
     341            close(DISC);
     342        }
     343
     344        # Pre allocate from the physical media
     345        my $device = Device::Cdio::Device->new(-source=>$ce_config{"CD_DEVICE"});
     346        my $track  = $device->get_last_track();
     347        $ce_cd->{tno} = $track->{track};
     348        my $n = 0;
     349
     350        # If they were not found
     351        $ce_cd->{artist} = "" if (not defined $ce_cd->{artist});
     352        $ce_cd->{title} = "" if (not defined $ce_cd->{title});
     353        $ce_cd->{genre} = "" if (not defined $ce_cd->{genre});
     354        $ce_cd->{cat} = "" if (not defined $ce_cd->{cat});
     355        $ce_cd->{year} = "" if (not defined $ce_cd->{year});
     356        $ce_cd->{revision} = 0 if (not defined $ce_cd->{revision});
     357
     358        # Going one track further for frame computation.
     359        # Cf: http://www.gnu.org/software/libcdio/doxygen/track_8h.html
     360        while ( $n <= $ce_cd->{tno}) {
     361            $track->set_track($n+1);
     362            # Again only if not found previously
     363            $ce_cd->{track}[$n] = "" if (not defined $ce_cd->{track}[$n]);
     364            my ($m, $s, $f) = split(/:/,$track->get_msf());
     365            $ce_cd->{frames}[$n] = $f + 75*$s + 75*60*$m;
     366            $n++;
     367        }
     368    }
    284369    # Modify enconding of some fields if proto < 5
    285     #$ce_cd{artist} = decode("iso8859-1",$ce_cd{artist});
    286     #$ce_cd{title} = decode("iso8859-1",$ce_cd{title});
     370    #$ce_cd->{artist} = decode("iso8859-1",$ce_cd->{artist});
     371    #$ce_cd->{title} = decode("iso8859-1",$ce_cd->{title});
    287372
    288373    # Third panel to display CD Infos
     
    293378    $flag = NEWT_ENTRY_SCROLL;
    294379    $width = 40;
    295     my $artist_entry = Newt::Entry($width, $flag, $ce_cd{artist});
    296     my $title_entry = Newt::Entry($width, $flag, $ce_cd{title});
     380    $artist_entry = Newt::Entry($width, $flag, $ce_cd->{artist});
     381    $title_entry = Newt::Entry($width, $flag, $ce_cd->{title});
    297382    # http://www.freedb.org/en/faq.3.html
    298383    # Category list is data, folk, jazz, misc, rock, country, blues, newage, reggae, classical, and soundtrack
    299     my $category_entry = Newt::Entry($width, $flag, $ce_cd{cat});
    300     my $genre_entry = Newt::Entry($width, $flag, $ce_cd{genre});
    301     #my $data_entry = Newt::Entry($width, $flag, Dumper(@{$ce_cd{data}}));
    302     #my $raw_entry = Newt::Entry($width, $flag, Dumper(@{$ce_cd{raw}}));
    303     my $tno_entry = Newt::Label($ce_cd{tno});
     384    $category_entry = Newt::Entry($width, $flag, $ce_cd->{cat});
     385    $genre_entry = Newt::Entry($width, $flag, $ce_cd->{genre});
     386    $tno_entry = Newt::Label($ce_cd->{tno}."   -   ".$id_label.$ce_cd->{id}."   -   ".$rev_label.$ce_cd->{revision});
    304387    $width = 5;
    305     my $year_entry = Newt::Entry($width, $flag, $ce_cd{year});
     388    $year_entry = Newt::Entry($width, $flag, $ce_cd->{year});
    306389   
    307390    # Build interface
    308     $ce_panel = Newt::Panel(3, $ce_sh, "CDDB Info");
     391    $ce_panel = Newt::Panel(4, $ce_sh, "CDDB Info");
    309392    $flage = NEWT_ANCHOR_RIGHT;
    310393    $ce_panel->Add(0, 0, $artist_label,$flage);
     
    331414    $flage = NEWT_ANCHOR_LEFT;
    332415    $ce_panel->Add(1, 5, $tno_entry, $flage);
    333     #$ce_panel->Add(0, 5, $data_label);
    334     #$ce_panel->Add(1, 5, $data_entry, $flage);
    335     #$ce_panel->Add(0, 6, $raw_label);
    336     #$ce_panel->Add(1, 6, $raw_entry, $flage);
    337     #$ce_panel->Add(0, 5, $info_label);
    338     #$ce_panel->Add(1, 5, $info_entry, $flage);
    339 
    340     my $n=1;
     416
     417    my $n=0;
    341418    $width = 40;
    342     while ( $n <= $ce_cd{tno} ) {
    343         last if ($n > $ce_sh - 8);
    344         my $from=$ce_cd{frames}[$n-1];
    345         my $to=$ce_cd{frames}[$n]-1;
     419    while ( $n < $ce_cd->{tno} ) {
     420        last if ($n > $ce_sh - 9);
     421        my $from=$ce_cd->{frames}[$n];
     422        my $to=$ce_cd->{frames}[$n+1]-1;
    346423        my $dur=$to-$from;
    347424        my $min=int($dur/75/60);
    348425        my $sec=int($dur/75)-$min*60;
    349         #my $frm=($dur-$sec*75-$min*75*60)*100/75;
    350         my $tr = sprintf "Track %2d:", $n;
     426        my $tr = sprintf "Track %2d:", $n+1;
    351427        my $track_label = Newt::Label($tr);
    352428        $tr = sprintf " %2d':%.2d", $min, $sec;
    353429        # Adapt encoding of track if proto < 5
    354         #$ce_cd{track}[$n -1] = decode("iso8859-1",$ce_cd{track}[$n -1]);
     430        #$ce_cd->{track}[$n] = decode("iso8859-1",$ce_cd->{track}[$n]);
    355431
    356432        my $dur_label = Newt::Label($tr);
    357433        $flag = NEWT_FLAG_WRAP|NEWT_ENTRY_SCROLL;
    358         my $track_entry = Newt::Entry($width, $flag, $ce_cd{track}[$n-1]);
    359         $ce_panel->Add(0, 8+$n, $track_label);
    360         $ce_panel->Add(1, 8+$n, $track_entry, $flage);
    361         $ce_panel->Add(2, 8+$n, $dur_label, $flage);
     434        $track_entry[$n] = Newt::Entry($width, $flag, $ce_cd->{track}[$n]);
     435        $ce_panel->Add(0, 9+$n, $track_label);
     436        $ce_panel->Add(1, 9+$n, $track_entry[$n], $flage);
     437        $ce_panel->Add(2, 9+$n, $dur_label, $flage);
    362438        $n++;
    363     }
    364     $ce_panel->Add(0, 8+$n, $empty_label);
    365 
    366     $ce_panel->Add(0, 8+$n+1, $send_button);
    367     $ce_panel->Add(1, 8+$n+1, $reload_button);
    368     $ce_panel->Add(2, 8+$n+1, $quit_button);
     439    }
     440    $ce_panel->Add(0, 9+$n, $empty_label);
     441
     442    $ce_panel->Add(0, 9+$n+1, $send_button);
     443    $ce_panel->Add(1, 9+$n+1, $reload_button);
     444    $ce_panel->Add(2, 9+$n+1, $quit_button);
    369445   
    370446    Newt::Cls();
    371447    Newt::Refresh();
     448
    372449    # Build interface
    373450    ($reason, $data) = $ce_panel->Run();
    374451
    375     $ce_cd{artist} = $artist_entry->Get();
    376     $ce_cd{title} = $title_entry->Get();
    377     $ce_cd{year} = $year_entry->Get();
    378     $ce_cd{cat} = $category_entry->Get();
    379     $ce_cd{genre} = $genre_entry->Get();
     452    $ce_cd->{artist} = $artist_entry->Get();
     453    $ce_cd->{title} = $title_entry->Get();
     454    $ce_cd->{year} = $year_entry->Get();
     455    $ce_cd->{cat} = $category_entry->Get();
     456    $ce_cd->{genre} = $genre_entry->Get();
     457    $n=1;
     458    while ( $n <= $ce_cd->{tno} ) {
     459        $ce_cd->{track}[$n-1] = $track_entry[$n-1]->Get();
     460        $n++;
     461    }
    380462    Newt::Refresh();
    381463    Newt::Cls();
     
    386468        $flag = NEWT_FLAG_WRAP;
    387469        $width = 40;
    388         my $height = 25;
     470        my $height = 10;
    389471
    390472        Newt::DrawRootText(ce_center_string($ce_title), 1, $ce_title);
    391473        Newt::PushHelpLine($ce_help);
    392474
    393         ce_prepare_mail(\%ce_cd);
    394         open(TEXT,"$ENV{'PBTMP'}/mail.txt") || die "Unable to open $ENV{'PBTMP'}/mail.txt";
    395         my $txt = "";
    396         #my $txt = pb_get_content("$ENV{'PBTMP'}/mail.txt");
    397         while (<TEXT>) {
    398             next if (/^#/);
    399             $txt .= $_;
    400         }
     475        my $txt = "Please wait while sending disc $ce_cd->{id} ($ce_cd->{cat}) to $ce_config{\"CDDB_USER\"}";
    401476        my $info_tb = Newt::Textbox($width, $height, $flag, $txt);
    402477
    403478        Newt::Cls();
    404479        $ce_panel = Newt::Panel(3, $width, "CDDB Sending Infos");
    405         $ce_panel->Add(0, 0, $wait_label);
    406         $ce_panel->Add(0, 1, $info_tb);
    407         $ce_panel->Add(0, 2, $quit_button);
     480        $ce_panel->Add(0, 0, $info_tb);
     481        $ce_panel->Add(0, 1, $quit_button);
    408482        Newt::Refresh();
     483        # TODO: Mail::Sendmail ?
     484        ce_prepare_mail($ce_cd);
     485        pb_system("mutt -s \"cddb $ce_cd->{cat} $ce_cd->{id}\" -c $ce_config{\"CDDB_CC\"} $ce_config{\"CDDB_USER\"} < $ENV{'HOME'}/.cddb/$ce_cd->{cat}/$ce_cd->{id}", "quiet");
    409486        ($reason, $data) = $ce_panel->Run();
     487
     488        # Force reloading info from cache
     489        $ce_cd->{title} = undef;
     490    }
     491    if ($data->Tag() eq "Reload") {
     492        # Then force CDDB query
     493        $firsttime = 1;
    410494    }
    411495}
     
    430514my $ce_cd = shift;
    431515
    432 open(MAIL, "> $ENV{'PBTMP'}/mail.txt") || die "Unable to create $ENV{'PBTMP'}/mail.txt";
     516pb_mkdir_p("$ENV{'HOME'}/.cddb/$ce_cd->{cat}");
     517open(MAIL, "> $ENV{'HOME'}/.cddb/$ce_cd->{cat}/$ce_cd->{id}") || die "Unable to create $ENV{'HOME'}/.cddb/$ce_cd->{cat}/$ce_cd->{id}";
    433518print MAIL "# xmcd CD database file\n#\n# Track frame offsets:\n";
    434 my $n=1;
    435 while ( $n <= $ce_cd->{tno} ) {
    436     print MAIL "#       ".$ce_cd->{frames}[$n-1]."\n";
     519my $n=0;
     520while ( $n < $ce_cd->{tno} ) {
     521    print MAIL "#       ".$ce_cd->{frames}[$n]."\n";
    437522    $n++;
    438523}
    439 my $from=$ce_cd->{frames}[0];
    440 my $to=$ce_cd->{frames}[$ce_cd->{tno}];
    441 my $dur=$to-$from;
    442 my $sec=int($dur/75);
     524#my $from=int($ce_cd->{frames}[0]/75);
     525my $sec=int($ce_cd->{frames}[$ce_cd->{tno}]/75);
     526#my $sec=$to-$from;
    443527my $rev = $ce_cd->{revision} + 1;
     528# Taken from CDDB.pm
    444529print MAIL "#\n# Disc length: $sec seconds\n#\n";
    445530print MAIL "# Revision: $rev\n";
     
    447532print MAIL "# Submitted via: CDDBEditor $projver-$projrev\n";
    448533print MAIL "DISCID=$ce_cd->{id}\n";
    449 print MAIL "DTITLE=$ce_cd->{title}\n";
     534print MAIL "DTITLE=$ce_cd->{artist} / $ce_cd->{title}\n";
    450535print MAIL "DYEAR=$ce_cd->{year}\n";
    451536print MAIL "DGENRE=$ce_cd->{genre}\n";
    452 $n=1;
    453 while ( $n <= $ce_cd->{tno} ) {
    454     print MAIL "TTITLE$n=$ce_cd->{track}[$n-1]\n";
     537$n=0;
     538while ( $n < $ce_cd->{tno} ) {
     539    print MAIL "TTITLE$n=$ce_cd->{track}[$n]\n";
    455540    $n++;
    456541}
    457542print MAIL "EXTD=\n";
    458 $n=1;
    459 while ( $n <= $ce_cd->{tno} ) {
     543$n=0;
     544while ( $n < $ce_cd->{tno} ) {
    460545    print MAIL "EXTT$n=\n";
    461546    $n++;
Note: See TracChangeset for help on using the changeset viewer.