-
Notifications
You must be signed in to change notification settings - Fork 0
/
lyrics-to-sqlite
executable file
·82 lines (69 loc) · 2.71 KB
/
lyrics-to-sqlite
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
#!/usr/bin/perl
# lyrics-to-sqlite, a tool for filling an SQLite database with lyrics.
# Copyright (C) 2019 defanor <[email protected]>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use strict;
use warnings;
use DBI;
use File::Basename;
use LyricsDB::Preprocessing;
die "Usage: lyrics-to-sqlite.pl <database directory> <sqlite db file>"
unless ($#ARGV >= 1);
my $dirname = shift @ARGV;
my $database = shift @ARGV;
die "The database file already exists" if (-f $database);
my $dbh = DBI->connect("DBI:SQLite:dbname=$database", "", "")
or die $DBI::errstr;
$dbh->begin_work;
$dbh->do(q(
CREATE TABLE lyrics (
lyrics_id INTEGER PRIMARY KEY,
artist TEXT NOT NULL,
album TEXT NOT NULL,
title TEXT NOT NULL,
text TEXT NOT NULL
)));
$dbh->do(q(
CREATE TABLE aliases (
lyrics_id INTEGER NOT NULL,
alias TEXT NOT NULL,
alias_column TEXT NOT NULL,
alias_type TEXT NOT NULL,
FOREIGN KEY (lyrics_id) REFERENCES lyrics(lyrics_id)
)));
my $insert_q = $dbh->prepare(
"INSERT INTO lyrics (artist, album, title, text) VALUES (?, ?, ?, ?)");
my $alias_q = $dbh->prepare("INSERT INTO aliases VALUES (?, ?, ?, ?)");
sub add_aliases {
my ($lyrics_id, $alias_column, $original) = @_;
my %aliases = preprocess($original);
for my $alias_type (keys %aliases) {
$alias_q->execute($lyrics_id,
$aliases{$alias_type},
$alias_column,
$alias_type);
}
}
for my $path (glob("$dirname/*/*/*/*")) {
my $title = basename $path;
my $album = basename(dirname $path);
my $artist = basename(dirname(dirname $path));
my $lyrics = do { local( @ARGV, $/ ) = $path; <> };
$insert_q->execute($artist, $album, $title, $lyrics);
my $lyrics_id = $dbh->last_insert_id("", "", "", "");
add_aliases($lyrics_id, 'artist', $artist);
add_aliases($lyrics_id, 'album', $album);
add_aliases($lyrics_id, 'title', $title);
}
$dbh->do("CREATE INDEX idx_aliases_column ON aliases (alias_column, alias)");
$dbh->commit;
$dbh->disconnect();