#!/usr/bin/perl -w use strict; use warnings FATAL => 'all'; use DBI; use SDBM_File; use Fcntl; use File::Basename qw(basename); $| = 1; use constant DEBUG => 0; #chdir '/path/to/dbm/files' || die; my $dbh = DBI->connect ( 'DBI:mysql:host=;user=user;password=pass;database=db', { PrintError => 0, }, ); my (%config, @base_dirs); my $query_config = "SELECT `key`, value FROM tbl_config WHERE `key` IN('base_dir', 'streamlocation', 'streamurl')"; my $sth_config = $dbh->prepare($query_config); if ($sth_config->execute) { my $rows = $sth_config->rows; for (my $i = 1; $i <= $rows; $i++) { my $ref = $sth_config->fetchrow_hashref; if ($ref->{key} eq 'base_dir') { @base_dirs = split /;/, $ref->{value}; } else { $config{$ref->{key}} = $ref->{value}; } } } else { printf "DB ERROR: %s\n", $dbh->errstr; exit 1; } my $query = "SELECT MD5(id) as md5, artist, title, dirname as dir, free as file, lengths as length, drive as dirnum FROM tbl_search"; my $sth = $dbh->prepare($query); if ($sth->execute) { my $rows = $sth->rows; my ($m3u, %songs, %titles, %filenames); unless (DEBUG) { $m3u = "#EXTM3U\r\n"; #tie (%songs, 'SDBM_File', 'songs', O_WRONLY | O_TRUNC | O_CREAT, 0644) || die; #tie (%titles, 'SDBM_File', 'titles', O_WRONLY | O_TRUNC | O_CREAT, 0644) || die; #tie (%filenames, 'SDBM_File', 'filenames', O_WRONLY | O_TRUNC | O_CREAT, 0644) || die; } for (my $i = 1; $i <= $rows; $i++) { my $ref = $sth->fetchrow_hashref; my $title; if (length $ref->{artist} && length $ref->{title}) { $title = sprintf "%s - %s", $ref->{artist}, $ref->{title}; } elsif (length $ref->{artist}) { $title = $ref->{artist}; } elsif (length $ref->{title}) { $title = $ref->{title}; } else { $title = $ref->{file}; $title =~ s/\.mp3$//i; $title =~ s/_/ /g; } $title =~ s/\s+/ /g; $title =~ s/^\s+//; $title =~ s/\s+$//; my $filename = sprintf "%s%s%s", $base_dirs[$ref->{dirnum}], $ref->{dir}, $ref->{file}; if (DEBUG) { #printf "%s = (%s) '%s'\n", $ref->{md5}, $title, $filename; printf "%s\n", $title; } #$songs{$ref->{md5}} = $filename; # #$titles{$ref->{md5}} = $title; # #$filenames{$ref->{md5}} = basename $filename; unless (DEBUG) { $m3u .= sprintf "#EXTINF:%s,%s\r\n", $ref->{length}, $title; $m3u .= sprintf "%s%s%s\r\n", $config{streamurl}, $config{streamlocation}, $ref->{md5}; } } unless (DEBUG) { #untie %titles; #untie %songs; #untie %filenames; if (open F, '> /path/to/full-playlist.m3u') { print F $m3u; close F; } my $duration = time - $^T; printf "%d file%s added in ~%d second%s\n", $rows, $rows != 1 ? 's' : '', $duration, $duration != 1 ? 's' : '', } } else { printf "DB ERROR: %s\n", $dbh->errstr; }