source: ProjectBuilder/projects/CDDBeditor/bin/CDDBeditor@ 858

Last change on this file since 858 was 858, checked in by Bruno Cornec, 15 years ago
  • Proto version 6 for UTF-8 support
  • CDDB_get 2.27 now required for revision support
  • new ce_prepare_mail function
  • Property svn:executable set to *
File size: 12.7 KB
Line 
1#!/usr/bin/perl -w
2#
3# Reads CDDB info, allow modifications and send back to CDDB server
4#
5# $Id$
6#
7# Copyright B. Cornec 2009
8# Provided under the GPL v2
9
10# Syntax: see at end
11
12use strict 'vars';
13use Getopt::Long qw(:config auto_abbrev no_ignore_case);
14use Data::Dumper;
15use English;
16use ProjectBuilder::Base;
17use ProjectBuilder::Conf;
18use Mail::Sendmail;
19use POSIX qw(strftime :sys_wait_h);
20use Newt qw(NEWT_FLAG_WRAP NEWT_ENTRY_SCROLL NEWT_ANCHOR_LEFT NEWT_ANCHOR_RIGHT :entry :textbox :macros);
21use lib qw (lib);
22use locale;
23# > 2.27
24use CDDB_get qw( get_cddb get_discids );
25use Encode;
26
27# Global variables
28my %opts; # CLI Options
29my $action; # action to realize
30
31my @date = pb_get_date();
32my $pbdate = strftime("%Y-%m-%d", @date);
33
34pb_temp_init();
35
36=pod
37
38=head1 NAME
39
40CDDBeditor, read, modidy and send your CDDB contrinutions
41
42=head1 DESCRIPTION
43
44CDDBeditor helps you read, modidy and send your CDDB contrinutions.
45
46=head1 SYNOPSIS
47
48CDDBeditor [-vhmq][-t|-g]
49
50CDDBeditor [--verbose][--help][--man][--quiet][--text|--graphic]
51
52=head1 OPTIONS
53
54=over 4
55
56=item B<-v|--verbose>
57
58Print a brief help message and exits.
59
60=item B<-q|--quiet>
61
62Do not print any output.
63
64=item B<-h|--help>
65
66Print a brief help message and exits.
67
68=item B<--man>
69
70Prints the manual page and exits.
71
72=item B<-t|--text>
73=item B<-g|--graphic>
74
75Interface is either text mode or graphical mode (newt based)
76
77=back
78
79=head1 ARGUMENTS
80
81=head1 WEB SITES
82
83The main Web site of the project is available at L<http://www.project-builder.org/>. Bug reports should be filled using the trac instance of the project at L<http://trac.project-builder.org/>.
84
85=head1 USER MAILING LIST
86
87None exists for the moment.
88
89=head1 CONFIGURATION FILES
90
91=head1 AUTHORS
92
93The CDDBeditor team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
94
95=head1 COPYRIGHT
96
97CDDBeditor is distributed under the GPL v2.0 license
98described in the file C<COPYING> included with the distribution.
99
100=cut
101
102# ---------------------------------------------------------------------------
103
104#Avoids an error from Newt
105no AutoLoader;
106
107
108# Initialize the syntax string
109
110my $projver = "PBVER";
111my $projrev = "PBREV";
112
113pb_syntax_init("CDDBeditor Version $projver-$projrev\n");
114
115GetOptions("help|?|h" => \$opts{'h'},
116 "man" => \$opts{'man'},
117 "verbose|v+" => \$opts{'v'},
118 "quiet|q" => \$opts{'q'},
119 "text|t" => \$opts{'t'},
120 "graphic|g" => \$opts{'g'},
121 "version|V=s" => \$opts{'V'},
122) || pb_syntax(-1,0);
123
124if (defined $opts{'h'}) {
125 pb_syntax(0,1);
126}
127if (defined $opts{'man'}) {
128 pb_syntax(0,2);
129}
130if (defined $opts{'v'}) {
131 $pbdebug = $opts{'v'};
132}
133if (defined $opts{'q'}) {
134 $pbdebug=-1;
135}
136pb_log_init($pbdebug, $pbLOG);
137
138
139Newt::Init();
140my ($ce_sl, $ce_sh) = Newt::GetScreenSize();
141
142# String definitions
143my $ce_title = "CDDBeditor";
144my $ce_help = "(c) Bruno Cornec 2009 - All right reversed under the GPL v2";
145my $artist_label = Newt::Label("Artist: ");
146my $empty_label = Newt::Label(" ");
147#my $data_label = Newt::Label("Data: ");
148#my $raw_label = Newt::Label("Raw: ");
149my $title_label = Newt::Label("Title: ");
150my $year_label = Newt::Label("Year: ");
151my $id_label = Newt::Label("Id: ");
152my $genre_label = Newt::Label("Genre: ");
153my $category_label = Newt::Label("Category: ");
154my $info_label = Newt::Label("Info: ");
155my $tr_track_label = Newt::Label("Track");
156my $tr_frames_label = Newt::Label("Frames");
157my $tno_label = Newt::Label("Track #: ");
158my $tr_title_label = Newt::Label("Title");
159my $tr_author_label = Newt::Label("Author");
160my $tr_length_label = Newt::Label("Length");
161
162my $host_label = Newt::Label("Host: ");
163my $port_label = Newt::Label("Port: ");
164my $mode_label = Newt::Label("Mode: ");
165my $dev_label = Newt::Label("Device: ");
166my $proxy_label = Newt::Label("Proxy: ");
167my $user_label = Newt::Label("Multi CD: ");
168
169my $wait_label = Newt::Label("Please wait while searching ...");
170
171# To be put in conf file
172my %ce_config;
173$ce_config{"CDDB_HOST"}="freedb.freedb.org";
174$ce_config{"CDDB_PORT"}=888;
175#$ce_config{"CDDB_PORT"}=80;
176my %ce_cddb_mode;
177$ce_cddb_mode{"0"} = "cddb";
178$ce_cddb_mode{"1"} = "http";
179
180$ce_config{"CDDB_MODE"}=$ce_cddb_mode{"1"};
181$ce_config{"CD_DEVICE"}="/dev/sr0";
182$ce_config{"HTTP_PROXY"}=$ENV{http_proxy} if $ENV{http_proxy};
183$ce_config{"input"}=0;
184$ce_config{"HELLO_ID"} = "bruno musique-ancienne.org fastrip 0.77";
185$ce_config{"PROTO_VERSION"} = 6;
186
187# First panel for configuration parameters
188Newt::Cls();
189Newt::DrawRootText(ce_center_string($ce_title), 1, $ce_title);
190Newt::PushHelpLine($ce_help);
191Newt::Refresh();
192
193my $flag = NEWT_ENTRY_SCROLL;
194my $width = 40;
195my $host_entry = Newt::Entry($width, $flag, $ce_config{"CDDB_HOST"});
196$width = 5;
197my $port_entry = Newt::Entry($width, $flag, $ce_config{"CDDB_PORT"});
198my $mode_group;
199my $proxy_entry = undef;
200$mode_group = Newt::HRadiogroup($ce_cddb_mode{"0"}, $ce_cddb_mode{"1"});
201if ($ce_config{"CDDB_MODE"} eq "http" ) {
202 $proxy_entry = Newt::Entry($width, $flag, $ce_config{"HTTP_PROXY"});
203}
204$width = 40;
205my $dev_entry = Newt::Entry($width, $flag, $ce_config{"CD_DEVICE"});
206my $user_group;
207if ($ce_config{input} == 0 ) {
208 $user_group = Newt::HRadiogroup('FALSE', 'TRUE');
209} else {
210 $user_group = Newt::HRadiogroup('TRUE', 'FALSE');
211}
212my $quit_button = Newt::Button("Quit");
213$quit_button->Tag("Quit");
214my $next_button = Newt::Button("Next");
215$next_button->Tag("Next");
216
217my $ce_panel = Newt::Panel(2, 20, "CDDBeditor Configuration");
218my $flage = NEWT_ANCHOR_RIGHT;
219$ce_panel->Add(0, 0, $host_label, $flage);
220$flage = NEWT_ANCHOR_LEFT;
221$ce_panel->Add(1, 0, $host_entry, $flage);
222$flage = NEWT_ANCHOR_RIGHT;
223$ce_panel->Add(0, 1, $port_label, $flage);
224$flage = NEWT_ANCHOR_LEFT;
225$ce_panel->Add(1, 1, $port_entry, $flage);
226$flage = NEWT_ANCHOR_RIGHT;
227$ce_panel->Add(0, 2, $mode_label, $flage);
228$flage = NEWT_ANCHOR_LEFT;
229$ce_panel->Add(1, 2, $mode_group, $flage);
230if ($ce_config{"CDDB_MODE"} eq "http" ) {
231 $flage = NEWT_ANCHOR_RIGHT;
232 $ce_panel->Add(0, 3, $proxy_label, $flage);
233 $flage = NEWT_ANCHOR_LEFT;
234 $ce_panel->Add(1, 3, $proxy_entry, $flage);
235 }
236$flage = NEWT_ANCHOR_RIGHT;
237$ce_panel->Add(0, 4, $dev_label, $flage);
238$flage = NEWT_ANCHOR_LEFT;
239$ce_panel->Add(1, 4, $dev_entry, $flage);
240$flage = NEWT_ANCHOR_RIGHT;
241$ce_panel->Add(0, 5, $user_label, $flage);
242$flage = NEWT_ANCHOR_LEFT;
243$ce_panel->Add(1, 5, $user_group, $flage);
244$ce_panel->Add(0, 6, $empty_label);
245$ce_panel->Add(0, 7, $next_button);
246$ce_panel->Add(1, 7, $quit_button);
247
248Newt::Refresh();
249my ($reason, $data) = $ce_panel->Run();
250
251Newt::Cls();
252exit 0 if ($data->Tag() eq "Quit");
253
254if ($ce_config{"CDDB_MODE"} eq "http" ) {
255 $ce_config{"HTTP_PROXY"}=$proxy_entry->Get();
256 }
257$ce_config{"CDDB_HOST"}=$host_entry->Get();
258$ce_config{"CDDB_PORT"}=$port_entry->Get();
259$ce_config{"CDDB_MODE"}=$ce_cddb_mode{$mode_group->Get()};
260$ce_config{"CD_DEVICE"}=$dev_entry->Get();
261$ce_config{"input"}=$user_group->Get();
262
263# Components
264my $reload_button = Newt::Button("Reload");
265$reload_button->Tag("Reload");
266my $send_button = Newt::Button("Send");
267$send_button->Tag("Send");
268
269# Entering the loop
270for (my $i=0 ; ; $i++) {
271 # Second panel to make user wait
272 Newt::Cls();
273 Newt::DrawRootText(ce_center_string($ce_title), 1, $ce_title);
274 Newt::PushHelpLine($ce_help);
275 $ce_panel = Newt::Panel(3, 20, "CDDB Search");
276 $ce_panel->Add(0, 0, $wait_label);
277
278 ($reason, $data) = $ce_panel->Draw();
279 Newt::Refresh();
280
281 # CDDB query
282 my %ce_cd = get_cddb(\%ce_config);
283
284 # 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});
287
288 # Third panel to display CD Infos
289 Newt::DrawRootText(ce_center_string($ce_title), 1, $ce_title);
290 Newt::PushHelpLine($ce_help);
291
292 # Components
293 $flag = NEWT_ENTRY_SCROLL;
294 $width = 40;
295 my $artist_entry = Newt::Entry($width, $flag, $ce_cd{artist});
296 my $title_entry = Newt::Entry($width, $flag, $ce_cd{title});
297 # http://www.freedb.org/en/faq.3.html
298 # 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});
304 $width = 5;
305 my $year_entry = Newt::Entry($width, $flag, $ce_cd{year});
306
307 # Build interface
308 $ce_panel = Newt::Panel(3, $ce_sh, "CDDB Info");
309 $flage = NEWT_ANCHOR_RIGHT;
310 $ce_panel->Add(0, 0, $artist_label,$flage);
311 $flage = NEWT_ANCHOR_LEFT;
312 $ce_panel->Add(1, 0, $artist_entry, $flage);
313 $flage = NEWT_ANCHOR_RIGHT;
314 $ce_panel->Add(0, 1, $title_label,$flage);
315 $flage = NEWT_ANCHOR_LEFT;
316 $ce_panel->Add(1, 1, $title_entry, $flage);
317 $flage = NEWT_ANCHOR_RIGHT;
318 $ce_panel->Add(0, 2, $year_label,$flage);
319 $flage = NEWT_ANCHOR_LEFT;
320 $ce_panel->Add(1, 2, $year_entry, $flage);
321 $flage = NEWT_ANCHOR_RIGHT;
322 $ce_panel->Add(0, 3, $category_label,$flage);
323 $flage = NEWT_ANCHOR_LEFT;
324 $ce_panel->Add(1, 3, $category_entry, $flage);
325 $flage = NEWT_ANCHOR_RIGHT;
326 $ce_panel->Add(0, 4, $genre_label,$flage);
327 $flage = NEWT_ANCHOR_LEFT;
328 $ce_panel->Add(1, 4, $genre_entry, $flage);
329 $flage = NEWT_ANCHOR_RIGHT;
330 $ce_panel->Add(0, 5, $tno_label,$flage);
331 $flage = NEWT_ANCHOR_LEFT;
332 $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;
341 $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;
346 my $dur=$to-$from;
347 my $min=int($dur/75/60);
348 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;
351 my $track_label = Newt::Label($tr);
352 $tr = sprintf " %2d':%.2d", $min, $sec;
353 # Adapt encoding of track if proto < 5
354 #$ce_cd{track}[$n -1] = decode("iso8859-1",$ce_cd{track}[$n -1]);
355
356 my $dur_label = Newt::Label($tr);
357 $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);
362 $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);
369
370 Newt::Cls();
371 Newt::Refresh();
372 # Build interface
373 ($reason, $data) = $ce_panel->Run();
374
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();
380 Newt::Refresh();
381 Newt::Cls();
382 exit 0 if ($data->Tag() eq "Quit");
383
384 # Send the data
385 if ($data->Tag() eq "Send") {
386 $flag = NEWT_FLAG_WRAP;
387 $width = 40;
388 my $height = 25;
389
390 Newt::DrawRootText(ce_center_string($ce_title), 1, $ce_title);
391 Newt::PushHelpLine($ce_help);
392
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 }
401 my $info_tb = Newt::Textbox($width, $height, $flag, $txt);
402
403 Newt::Cls();
404 $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);
408 Newt::Refresh();
409 ($reason, $data) = $ce_panel->Run();
410 }
411}
412Newt::Finished();
413exit 0;
414
415# return the char to start printing a centered string based on screen size
416sub ce_center_string {
417
418 my $string = shift || "";
419
420 my $res = ($ce_sl / 2 ) - (length($string) / 2);
421 if ($res < 0 ) {
422 return 0;
423 } else {
424 return ($res);
425 }
426}
427
428sub ce_prepare_mail() {
429
430my $ce_cd = shift;
431
432open(MAIL, "> $ENV{'PBTMP'}/mail.txt") || die "Unable to create $ENV{'PBTMP'}/mail.txt";
433print MAIL "# xmcd CD database file\n#\n# Track frame offsets:\n";
434my $n=1;
435while ( $n <= $ce_cd->{tno} ) {
436 print MAIL "# ".$ce_cd->{frames}[$n-1]."\n";
437 $n++;
438}
439my $from=$ce_cd->{frames}[0];
440my $to=$ce_cd->{frames}[$ce_cd->{tno}];
441my $dur=$to-$from;
442my $sec=int($dur/75);
443my $rev = $ce_cd->{revision} + 1;
444print MAIL "#\n# Disc length: $sec seconds\n#\n";
445print MAIL "# Revision: $rev\n";
446print MAIL "# Processed by: cddbd v1.5PL3 Copyright (c) Steve Scherf et al.\n";
447print MAIL "# Submitted via: CDDBEditor $projver-$projrev\n";
448print MAIL "DISCID=$ce_cd->{id}\n";
449print MAIL "DTITLE=$ce_cd->{title}\n";
450print MAIL "DYEAR=$ce_cd->{year}\n";
451print MAIL "DGENRE=$ce_cd->{genre}\n";
452$n=1;
453while ( $n <= $ce_cd->{tno} ) {
454 print MAIL "TTITLE$n=$ce_cd->{track}[$n-1]\n";
455 $n++;
456}
457print MAIL "EXTD=\n";
458$n=1;
459while ( $n <= $ce_cd->{tno} ) {
460 print MAIL "EXTT$n=\n";
461 $n++;
462}
463print MAIL "PLAYORDER=\n";
464close(MAIL);
465}
466
4671;
Note: See TracBrowser for help on using the repository browser.