ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/truck/MusicLibrary/MusicLibrary.pl
Revision: 22
Committed: 2008-02-08T06:19:12-08:00 (14 years, 7 months ago) by douglas
Content type: text/x-perl
File size: 2991 byte(s)
Log Message:
Build system improvements and new projects...

File Contents

# Content
1 #!/usr/bin/perl
2 # Music Library
3 #
4 # Douglas Thrift
5 #
6 # $Id$
7
8 use strict;
9 use warnings;
10
11 use Audio::WMA;
12 use DBI;
13 use File::Basename;
14 use File::Find;
15 use MP3::Info;
16
17 my @directories;
18
19 for (@ARGV)
20 {
21 push @directories, $_;
22 }
23
24 Usage() if ($#directories == -1);
25
26 my $db = DBI->connect('dbi:SQLite:dbname=' . dirname($0) . '/MusicLibrary.db', '', '', {RaiseError => 1});
27
28 $db->prepare(<<EOF)->execute;
29 create table if not exists artist (
30 id integer primary key,
31 name text unique
32 )
33 EOF
34 $db->prepare(<<EOF)->execute;
35 create table if not exists album (
36 id integer primary key,
37 name text,
38 artist integer references artist on delete cascade,
39 year integer,
40 unique (name, artist)
41 )
42 EOF
43 $db->prepare(<<EOF)->execute;
44 create table if not exists song (
45 id integer primary key,
46 name text not null,
47 path text,
48 album integer references album on delete cascade,
49 track integer,
50 unique (path, album)
51 )
52 EOF
53 $db->prepare(<<EOF)->execute;
54 create index if not exists song_track on song (track)
55 EOF
56
57 my $select_artist = $db->prepare(<<EOF);
58 select id
59 from artist
60 where name = ?
61 EOF
62 my $insert_artist = $db->prepare(<<EOF);
63 insert into artist
64 (name)
65 values (?)
66 EOF
67 my $select_album = $db->prepare(<<EOF);
68 select id
69 from album
70 where name = ?
71 and artist = ?
72 EOF
73 my $insert_album = $db->prepare(<<EOF);
74 insert into album
75 (name, artist, year)
76 values (?, ?, ?)
77 EOF
78 my $select_song = $db->prepare(<<EOF);
79 select id
80 from song
81 where path = ?
82 and album = ?
83 EOF
84 my $insert_song = $db->prepare(<<EOF);
85 insert into song
86 (name, path, album, track)
87 values (?, ?, ?, ?)
88 EOF
89
90 find({wanted => \&File, follow => 1}, @directories);
91
92 sub Usage
93 {
94 print 'Usage: ' . basename($0) . " [directory ...]\n";
95 exit 1;
96 }
97
98 sub File
99 {
100 if ($_ =~ /\.(wma|mp3)$/i)
101 {
102 my %tags;
103
104 if ($1 =~ /^wma$/i)
105 {
106 %tags = %{new Audio::WMA($_)->tags};
107 }
108 elsif ($1 =~ /^mp3$/i)
109 {
110 %tags = %{get_mp3tag($File::Find::name)};
111 }
112
113 my $artist;
114
115 $artist = $tags{ALBUMARTIST};
116 $artist = $tags{TPE2} if (!$artist);
117 $artist = $tags{ARTIST} if (!$artist);
118
119 $select_artist->execute($artist);
120
121 my @row = $select_artist->fetchrow_array;
122
123 if ($#row == -1)
124 {
125 $insert_artist->execute($artist);
126 $select_artist->execute($artist);
127
128 @row = $select_artist->fetchrow_array;
129 }
130
131 my $artist_id = $row[0];
132 my ($album, $year);
133
134 $album = $tags{ALBUMTITLE};
135 $album = $tags{ALBUM} if (!$album);
136 $year = $tags{YEAR};
137
138 $select_album->execute($album, $artist_id);
139
140 @row = $select_album->fetchrow_array;
141
142 if ($#row == -1)
143 {
144 $insert_album->execute($album, $artist_id, $year);
145 $select_album->execute($album, $artist_id);
146
147 @row = $select_album->fetchrow_array;
148 }
149
150 my $album_id = $row[0];
151 my ($song, $track);
152
153 $song = $tags{TITLE};
154 $track = $tags{TRACKNUMBER};
155 $track = $tags{TRACKNUM} if (!$track);
156
157 $select_song->execute($File::Find::name, $album_id);
158
159 @row = $select_song->fetchrow_array;
160
161 $insert_song->execute($song, $File::Find::name, $album_id, $track) if ($#row == -1);
162 }
163 }

Properties

Name Value
svn:executable *