Как скопировать все файлы из списка воспроизведения xspf в каталог?
Я создал список воспроизведения песен xspf, который мне нужно скопировать в определенный каталог и перенести на другой компьютер... Как мне это сделать с помощью Ubuntu?
2 ответа
Решение
Сам не смог найти это где-то еще, поэтому я сделал быстрый и грязный хак PHP, чтобы сделать это (только сейчас). Это ни в коем случае не элегантное решение, но достаточно хорошо для моих целей. Это может быть хорошей идеей закомментировать copy
часть в самом конце и заменить его на некоторые echo
конструкция, чтобы убедиться, что ваш код копирует нужные файлы.
Справочная запись:
Usage example: ./copy_xspf.php --e -i myMusic.xspf -o /home/foo/bar
-i filename or --input filename - XSPF file to parse
-o directory or --input directory - directory to copy to (needs to exist!)
--help - display this help and terminate
Источник скрипта:
#!/usr/bin/php
<?php
// Quick and dirty hack.
for($i = 1; $i < count($argv); ++$i) {
if($argv[$i] == '--help' || $argv[$i] == '-e') {
echo "Usage example: ./copy_xspf.php --e -i myMusic.xspf -o /home/foo/bar\n";
echo "-i filename or --input filename - XSPF file to parse\n";
echo "-o directory or --input directory - directory to copy to (needs to exist!)\n";
echo "--help - display this help and terminate\n";
die();
}
if($argv[$i] == '--input' || $argv[$i] == '-i') {
if(!isset($argv[$i+1])) {
die("No input filename given (xspf file), use -i filename or --input filename\n");
} else {
$filename = $argv[$i+1];
}
}
if($argv[$i] == '--output' || $argv[$i] == '-o') {
if(!isset($argv[$i+1])) {
die("No output directory given, use -o directory or --output directory\n");
} else {
$outputDir = $argv[$i+1];
}
}
}
if(!isset($filename) || empty($filename)) {
die("No input filename given (xspf file), use -i filename or -input filename\n");
}
if(!isset($outputDir) || empty($outputDir)) {
die("No output directory given, use -o directory or --output directory\n");
} else {
$outputDir = rtrim($outputDir, '/');
}
$xml = file_get_contents($filename);
preg_match_all('#<location>(.*?)</location>#', $xml, $matches);
$matches = $matches[1]; // Select only the contents of (.*?), not the entire pattern
$matches = preg_replace('#file://(.*)#', '\\1', $matches); // Remove file://
foreach($matches as $key => $value) {
$matches[$key] = urldecode($value);
$matches[$key] = html_entity_decode($matches[$key]);
}
foreach($matches as $value) {
$base = basename($value);
echo "Copying $base ...\n";
copy($value, "$outputDir/$base");
}
Я создал скрипт Python, чтобы сделать то же самое!
https://gist.github.com/coppolaemilio/13cdff09abb93ee6ed50f8f300c1327b
import os
from urllib.parse import unquote
from xml.dom import minidom
import shutil
import sys
# Read all files alongside this script
for FILE in os.listdir('.'):
if '.xspf' in FILE:
DEST_DIR = FILE.replace('.xspf', '')
# Parse xml and get the file list
file_array = []
xmldoc = minidom.parse(FILE)
itemlist = xmldoc.getElementsByTagName('location')
for s in itemlist:
filevalue = s.firstChild.nodeValue
filevalue = filevalue.replace('file:///', '')
filevalue = unquote(filevalue)
file_array.append(filevalue)
# Creating folder
if not os.path.exists(DEST_DIR):
os.makedirs(DEST_DIR)
# Copy files to new folder
for file in file_array:
head, tail = os.path.split(file)
print(file)
shutil.copy(file, './' + DEST_DIR + '/' + tail)