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()) { if (temp.isValid()) {
song->set_filename(temp.toString()); song->set_filename(temp.toString());
song->set_filetype(Song::Type_Stream); song->set_filetype(Song::Type_Stream);
song->set_valid(true);
return true; return true;
} else { } else {
return false; return false;

View File

@ -16,8 +16,7 @@
#include "plsparser.h" #include "plsparser.h"
#include <QTemporaryFile> #include <QTextStream>
#include <QSettings>
#include <QtDebug> #include <QtDebug>
PLSParser::PLSParser(QObject* parent) PLSParser::PLSParser(QObject* parent)
@ -26,73 +25,44 @@ PLSParser::PLSParser(QObject* parent)
} }
SongList PLSParser::Load(QIODevice *device, const QDir &dir) const { SongList PLSParser::Load(QIODevice *device, const QDir &dir) const {
QTemporaryFile temp_file; QMap<int, Song> songs;
temp_file.open(); QRegExp n_re("\\d+$");
temp_file.write(device->readAll());
temp_file.flush();
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; n_re.indexIn(key);
// Use the first group, probably "playlist" but it doesn't matter int n = n_re.cap(0).toInt();
if (s.childGroups().isEmpty())
return ret;
s.beginGroup(s.childGroups()[0]);
// We try not to rely on NumberOfEntries (it might not be present), so go if (key.startsWith("file")) {
// through each key in the file and look at ones that start with "File" if (!ParseTrackLocation(value, dir, &songs[n]))
QStringList keys(s.childKeys()); qWarning() << "Failed to parse location: " << value;
keys.sort(); // Make sure we get the tracks in order } else if (key.startsWith("title")) {
songs[n].set_title(value);
foreach (const QString& key, keys) { } else if (key.startsWith("length")) {
if (!key.toLower().startsWith("file")) songs[n].set_length(value.toInt());
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;
} }
} }
return ret; return songs.values();
} }
void PLSParser::Save(const SongList &songs, QIODevice *device, const QDir &dir) const { void PLSParser::Save(const SongList &songs, QIODevice *device, const QDir &dir) const {
QTemporaryFile temp_file; QTextStream s(device);
temp_file.open(); s << "[playlist]" << endl;
s << "Version=2" << endl;
QSettings s(temp_file.fileName(), QSettings::IniFormat); s << "NumberOfEntries=" << songs.count() << endl;
s.beginGroup("playlist");
s.setValue("Version", 2);
s.setValue("NumberOfEntries", songs.count());
int n = 1; int n = 1;
foreach (const Song& song, songs) { foreach (const Song& song, songs) {
s.setValue("File" + QString::number(n), MakeRelativeTo(song.filename(), dir)); s << "File" << n << "=" << MakeRelativeTo(song.filename(), dir) << endl;
s.setValue("Title" + QString::number(n), song.title()); s << "Title" << n << "=" << song.title() << endl;
s.setValue("Length" + QString::number(n), song.length()); s << "Length" << n << "=" << song.length() << endl;
++n; ++n;
} }
s.sync();
temp_file.reset();
device->write(temp_file.readAll());
} }
bool PLSParser::TryMagic(const QByteArray &data) const { 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.xspf</file>
<file>test.asx</file> <file>test.asx</file>
<file>secretagent.asx</file> <file>secretagent.asx</file>
<file>secretagent.pls</file>
</qresource> </qresource>
</RCC> </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-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://streamer-ntc-aa04.somafm.com:80/stream/1018", songs[2].filename());
EXPECT_EQ("http://ice.somafm.com/groovesalad", songs[3].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(-1, songs[0].length());
EXPECT_EQ(Song::Type_Stream, songs[0].filetype()); EXPECT_EQ(Song::Type_Stream, songs[0].filetype());
} }
@ -66,7 +85,7 @@ TEST_F(PLSParserTest, ParseSomaFM) {
TEST_F(PLSParserTest, SaveAndLoad) { TEST_F(PLSParserTest, SaveAndLoad) {
Song one; Song one;
one.set_filename("http://www.example.com/foo.mp3"); one.set_filename("http://www.example.com/foo.mp3");
one.set_title("Foo"); one.set_title("Foo, with, some, commas");
Song two; Song two;
two.set_filename("relative/bar.mp3"); two.set_filename("relative/bar.mp3");