/
dudl-mus2job
198 lines (165 loc) · 3.45 KB
/
dudl-mus2job
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/perl -w
#
# Copyright (c) 2008 Rainer Clasen
#
# This program is free software; you can redistribute it and/or modify
# it under the terms described in the file LICENSE included in this
# distribution.
#
use strict;
use Dudl::DB;
use Dudl::Job::Archive;
use Getopt::Long;
my $wanthelp;
my $needhelp;
if( ! GetOptions(
"help|h!" => \$wanthelp,
)){
$needhelp++;
}
if( $wanthelp ){
print <<EOF;
usage: $0 <opt>
(re-)generate archive jobfiles from music database
options:
--help this short usage information.
EOF
exit 0;
}
if( $needhelp ){
print STDERR "use --help for usage info\n";
exit 1;
}
=pod
=head1 NAME
dudl-mus2job - (re-)generate archive jobfiles from music database
=head1 DESCRIPTION
This script exports your tediously maintained music Database to dudl
archive jobfiles. With these jobfiles you can easily copy those directory
and populate another dudl database.
You may want to run this on a periodic basis.
=head1 SEE ALSO
I<Dudl::Overview>,
I<dudl-mus2id>
=head1 AUTHOR
Rainer Clasen
=cut
my $dudl = new Dudl::DB;
my $db = $dudl->db;
my $cdpath = $dudl->conf("cdpath");
my $archive = $dudl->conf("write_jname");
my $sth = $db->prepare("
SELECT
a.id AS album_id,
a.album AS album_name,
aa.nname AS album_artist,
date_part('year',a.publish_date) AS album_year,
f.id AS file_id,
stor_unitpath(u.collection, u.colnum) || '/' || f.dir AS file_dir,
f.fname AS file_name,
f.broken AS file_broken,
f.cmnt AS file_cmt,
f.title AS title_name,
ta.nname AS title_artist,
f.album_pos AS title_num,
mserv_tags(f.id) AS title_genres
FROM
stor_file f INNER JOIN stor_unit u
ON f.unit_id = u.id
INNER JOIN mus_artist ta
ON f.artist_id = ta.id
INNER JOIN mus_album a
ON f.album_id = a.id
INNER JOIN mus_artist aa
ON a.artist_id = aa.id
WHERE
NOT u.autoscan
ORDER BY
u.collection,
u.colnum,
f.dir,
aa.nname,
a.album,
f.album_pos
") or die $db->errstr;
my $res = $sth->execute
or die $sth->errstr;
my(
$album_id,
$album_name,
$album_artist,
$album_year,
$file_id,
$file_dir,
$file_name,
$file_broken,
$file_cmt,
$title_name,
$title_artist,
$title_num,
$title_genres,
);
$sth->bind_columns( \(
$album_id,
$album_name,
$album_artist,
$album_year,
$file_id,
$file_dir,
$file_name,
$file_broken,
$file_cmt,
$title_name,
$title_artist,
$title_num,
$title_genres,
)) or die $sth->errstr;
my $job;
my( $odir, $oalb );
my( $jobs, $albums );
while( defined $sth->fetch ){
if( ! defined $odir || $odir ne $file_dir ){
&finish( $odir, $job ) if defined $job;
$job = new Dudl::Job::Archive( naming => $dudl->naming );
$odir = $file_dir;
$oalb = undef;
$jobs++;
}
if( ! defined $oalb || $oalb != $album_id ){
$job->add_album(
name => $album_name,
artist => $album_artist,
type => $album_artist =~ /^(VARIOUS|UNKNOWN)$/
? "sampler" : "album",
year => $album_year,
);
$oalb = $album_id;
$albums++;
}
$job->add_file(
id => $file_id,
mp3 => $file_name,
broken => $file_broken,
cmt => $file_cmt,
);
$job->add_title(
id => $file_id,
name => $title_name,
artist => $title_artist,
num => $title_num,
genres => $title_genres,
);
}
&finish( $odir, $job ) if defined $job;
print "wrote $albums albums to $jobs job files\n";
sub finish {
my( $dir, $job ) = @_;
return unless -d "$cdpath/$dir";
my $fname = "$cdpath/$dir/$archive";
print "writing $fname\n";
local *J;
open( J, ">$fname" ) or die "open failed: $!";
$job->write( \*J );
close( J );
#$job->write( \*STDOUT );
}