-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamarok_analysis
More file actions
executable file
·67 lines (59 loc) · 1.47 KB
/
amarok_analysis
File metadata and controls
executable file
·67 lines (59 loc) · 1.47 KB
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
#!/usr/bin/perl -w
use strict;
use DBI;
my $dbname = 'amarok_analysis';
my $table = 'music';
my $homedir = $ENV{HOME};
my $sep = '||'; # Must match what's in amaroksql_dump
my $csv = qq{$homedir/amarok_export.csv};
my $tbcreate = "CREATE TABLE $table (path TEXT UNIQUE, rating INT, artist TEXT, album TEXT, title TEXT);"; # Must line up with whatever fields you import
main();
##################################
# Loads an amarok resultsfile into a PostgreSQL database for analysis
sub main
{
system("dropdb $dbname");
system("createdb $dbname");
open(CSV, $csv) || die "Could not open CSV [$csv]:$!\n";
my $searcher = regex_quote();
print "Splitting on $searcher\n";
my @header = split(/$searcher/, readline(CSV)); # Was going to automate table creation from this,
# but no way to come up with good types that way.
my $dbh = DBI->connect("DBI:Pg:dbname=$dbname",'','');
my $tc = $dbh->do($tbcreate);
print "Entering " . scalar(@header) . " fields...\n";
my $ins = $dbh->prepare("INSERT INTO $table VALUES(" . placeholders(scalar(@header)) . ");");
while(<CSV>)
{
my @bits = split(/$searcher/, $_);
$ins->execute(@bits);
}
close(CSV);
}
sub placeholders
{
my ($num) = @_;
my $ret;
if($num)
{
$num--;
$ret = '?';
$ret .= ',?' x $num;
}
return $ret;
}
sub regex_quote
{
my @bits = split(//,$sep);
my $ret = '';
foreach my $bit (@bits)
{
if($bit =~ /[|.?*\\\[\]]/) # Add any characters that need escaping here
{
$ret .= '\\' . $bit;
}
else
{$ret .= $bit;}
}
return $ret;
}