1
0
mirror of https://github.com/clementine-player/Clementine synced 2024-12-18 12:28:31 +01:00

PLS playlists aren't really INI files - they don't follow the same rules with escaping characters, so parse them the old fashioned way.

This commit is contained in:
David Sansome 2010-06-15 17:34:00 +00:00
parent 8c7539f02e
commit 02d01b1314
5 changed files with 62 additions and 56 deletions

View File

@ -31,6 +31,7 @@ bool ParserBase::ParseTrackLocation(const QString& filename_or_url,
if (temp.isValid()) {
song->set_filename(temp.toString());
song->set_filetype(Song::Type_Stream);
song->set_valid(true);
return true;
} else {
return false;

View File

@ -16,8 +16,7 @@
#include "plsparser.h"
#include <QTemporaryFile>
#include <QSettings>
#include <QTextStream>
#include <QtDebug>
PLSParser::PLSParser(QObject* parent)
@ -26,73 +25,44 @@ PLSParser::PLSParser(QObject* parent)
}
SongList PLSParser::Load(QIODevice *device, const QDir &dir) const {
QTemporaryFile temp_file;
temp_file.open();
temp_file.write(device->readAll());
temp_file.flush();
QMap<int, Song> songs;
QRegExp n_re("\\d+$");
QSettings s(temp_file.fileName(), QSettings::IniFormat);
while (!device->atEnd()) {
QString line = QString::fromUtf8(device->readLine()).trimmed();
int equals = line.indexOf('=');
QString key = line.left(equals).toLower();
QString value = line.mid(equals + 1);
SongList ret;
// Use the first group, probably "playlist" but it doesn't matter
if (s.childGroups().isEmpty())
return ret;
s.beginGroup(s.childGroups()[0]);
n_re.indexIn(key);
int n = n_re.cap(0).toInt();
// We try not to rely on NumberOfEntries (it might not be present), so go
// through each key in the file and look at ones that start with "File"
QStringList keys(s.childKeys());
keys.sort(); // Make sure we get the tracks in order
foreach (const QString& key, keys) {
if (!key.toLower().startsWith("file"))
continue;
bool ok = false;
int n = key.mid(4).toInt(&ok); // 4 == "file".length
if (!ok)
continue;
QString filename = s.value(key).toString();
QString title = s.value("Title" + QString::number(n)).toString();
int length = s.value("Length" + QString::number(n)).toInt();
Song song;
song.set_title(title);
song.set_length(length);
if (!ParseTrackLocation(filename, dir, &song)) {
qWarning() << "Failed to parse location: " << filename;
} else {
ret << song;
if (key.startsWith("file")) {
if (!ParseTrackLocation(value, dir, &songs[n]))
qWarning() << "Failed to parse location: " << value;
} else if (key.startsWith("title")) {
songs[n].set_title(value);
} else if (key.startsWith("length")) {
songs[n].set_length(value.toInt());
}
}
return ret;
return songs.values();
}
void PLSParser::Save(const SongList &songs, QIODevice *device, const QDir &dir) const {
QTemporaryFile temp_file;
temp_file.open();
QSettings s(temp_file.fileName(), QSettings::IniFormat);
s.beginGroup("playlist");
s.setValue("Version", 2);
s.setValue("NumberOfEntries", songs.count());
QTextStream s(device);
s << "[playlist]" << endl;
s << "Version=2" << endl;
s << "NumberOfEntries=" << songs.count() << endl;
int n = 1;
foreach (const Song& song, songs) {
s.setValue("File" + QString::number(n), MakeRelativeTo(song.filename(), dir));
s.setValue("Title" + QString::number(n), song.title());
s.setValue("Length" + QString::number(n), song.length());
s << "File" << n << "=" << MakeRelativeTo(song.filename(), dir) << endl;
s << "Title" << n << "=" << song.title() << endl;
s << "Length" << n << "=" << song.length() << endl;
++n;
}
s.sync();
temp_file.reset();
device->write(temp_file.readAll());
}
bool PLSParser::TryMagic(const QByteArray &data) const {

View File

@ -0,0 +1,15 @@
[playlist]
numberofentries=4
File1=http://streamer-ntc-aa03.somafm.com:80/stream/1021
Title1=SomaFM: Secret Agent (#1 128k mp3): The soundtrack for your stylish, mysterious, dangerous life. For Spies and PIs too!
Length1=-1
File2=http://streamer-mtc-aa04.somafm.com:80/stream/1021
Title2=SomaFM: Secret Agent (#2 128k mp3): The soundtrack for your stylish, mysterious, dangerous life. For Spies and PIs too!
Length2=-1
File3=http://streamer-dtc-aa05.somafm.com:80/stream/1021
Title3=SomaFM: Secret Agent (#3 128k mp3): The soundtrack for your stylish, mysterious, dangerous life. For Spies and PIs too!
Length3=-1
File4=http://ice.somafm.com/secretagent
Title4=SomaFM: Secret Agent (Firewall-friendly 128k mp3) The soundtrack for your stylish, mysterious, dangerous life. For Spies and PIs too!
Length4=-1
Version=2

View File

@ -13,5 +13,6 @@
<file>test.xspf</file>
<file>test.asx</file>
<file>secretagent.asx</file>
<file>secretagent.pls</file>
</qresource>
</RCC>

View File

@ -59,6 +59,25 @@ TEST_F(PLSParserTest, ParseSomaFM) {
EXPECT_EQ("http://streamer-mtc-aa03.somafm.com:80/stream/1018", songs[1].filename());
EXPECT_EQ("http://streamer-ntc-aa04.somafm.com:80/stream/1018", songs[2].filename());
EXPECT_EQ("http://ice.somafm.com/groovesalad", songs[3].filename());
EXPECT_EQ("SomaFM: Groove Salad (#1 128k mp3): A nicely chilled plate of ambient beats and grooves.", songs[0].title());
EXPECT_EQ("SomaFM: Groove Salad (#2 128k mp3): A nicely chilled plate of ambient beats and grooves.", songs[1].title());
EXPECT_EQ("SomaFM: Groove Salad (#3 128k mp3): A nicely chilled plate of ambient beats and grooves.", songs[2].title());
EXPECT_EQ(-1, songs[0].length());
EXPECT_EQ(Song::Type_Stream, songs[0].filetype());
}
TEST_F(PLSParserTest, ParseSomaFM2) {
shared_ptr<QFile> file(Open("secretagent.pls"));
SongList songs = parser_.Load(file.get());
ASSERT_EQ(4, songs.length());
EXPECT_EQ("http://streamer-ntc-aa03.somafm.com:80/stream/1021", songs[0].filename());
EXPECT_EQ("http://streamer-mtc-aa04.somafm.com:80/stream/1021", songs[1].filename());
EXPECT_EQ("http://streamer-dtc-aa05.somafm.com:80/stream/1021", songs[2].filename());
EXPECT_EQ("http://ice.somafm.com/secretagent", songs[3].filename());
EXPECT_EQ("SomaFM: Secret Agent (#1 128k mp3): The soundtrack for your stylish, mysterious, dangerous life. For Spies and PIs too!", songs[0].title());
EXPECT_EQ("SomaFM: Secret Agent (#2 128k mp3): The soundtrack for your stylish, mysterious, dangerous life. For Spies and PIs too!", songs[1].title());
EXPECT_EQ("SomaFM: Secret Agent (#3 128k mp3): The soundtrack for your stylish, mysterious, dangerous life. For Spies and PIs too!", songs[2].title());
EXPECT_EQ(-1, songs[0].length());
EXPECT_EQ(Song::Type_Stream, songs[0].filetype());
}
@ -66,7 +85,7 @@ TEST_F(PLSParserTest, ParseSomaFM) {
TEST_F(PLSParserTest, SaveAndLoad) {
Song one;
one.set_filename("http://www.example.com/foo.mp3");
one.set_title("Foo");
one.set_title("Foo, with, some, commas");
Song two;
two.set_filename("relative/bar.mp3");