aboutsummaryrefslogtreecommitdiff
path: root/bin/xspf_extractor
blob: be98e72bed0098eda5d26fbebed1e15aa1417f04 (plain)
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
#!/usr/bin/perl -w
# Copyright (C) 2017-Ω Miquel Sabaté Solà <mikisabate@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use strict;

# Check that XML::LibXML is installed, and die if it isn't. This can be
# installed in openSUSE with the 'perl-XML-LibXML' package.
my $xml = eval {
    require XML::LibXML;
    Term::ReadKey->import();
    1;
};
if (!$xml) {
    print "Please install the 'XML::LibXML' module!\n";
    exit 1;
}

use Getopt::Long;
use File::Copy;
use XML::LibXML;

# Prevent any UTF-8 oddities.
use utf8;
binmode(STDOUT, ":utf8");

##
# Helper methods.

sub help {
    my ($code) = @_;

    print <<EOF;
Usage: $0 <options> <XSPF file> <directory>

Options:
  --version|-v  Print the version of this program.
  --help|-h     Print this message.
EOF

    exit $code;
}

sub version {
    print <<EOF;
xspf_extractor version 0.1
Copyright (C) 2017-Ω Miquel Sabaté Solà <mikisabate\@gmail.com>
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
EOF

    exit 0;
}

##
# Parse flags and check that everything is alright.

my %opt;

GetOptions(\%opt, "help|h", "version|v");

help(0)   if $opt{"help"};
version() if $opt{"version"};

my ($file, $dir) = @ARGV;
if (!defined($file) || !defined($dir)) {
    print "You must provide the source file and the directory\n";
    help(1);
}

if (! -e $file) {
    print "Source XSPF file '$file' does not exist!\n";
    exit 1;
}

if (! -d $dir) {
    print "Destination directory '$dir' does not exist!\n";
    exit 1;
}

##
# Parse the given XSPF document.

my $dom = XML::LibXML->new->parse_file($file);
for my $node ($dom->getElementsByTagName('track')) {
    my $location = $node->getElementsByTagName("location")->get_node(0)->textContent;
    copy($location, $dir);
}