Commit e0e31f6a authored by Camilo higuita's avatar Camilo higuita

not more pulpo brain breaks and redo the thing and fight the pointers and make it all async

parent bc6ce57d
......@@ -158,10 +158,10 @@ void Babe::trackLyrics(const QString &url)
if(track.isEmpty()) return;
qDebug()<< "Getting lyrics for track"<< track.first()[FMH::MODEL_KEY::TITLE];
if(!track.first()[FMH::MODEL_KEY::LYRICS].isEmpty() && track.first()[FMH::MODEL_KEY::LYRICS] != SLANG[W::NONE])
emit this->trackLyricsReady(track.first()[FMH::MODEL_KEY::LYRICS], url);
else
// qDebug()<< "Getting lyrics for track"<< track.first()[FMH::MODEL_KEY::TITLE];
// if(!track.first()[FMH::MODEL_KEY::LYRICS].isEmpty() && track.first()[FMH::MODEL_KEY::LYRICS] != SLANG[W::NONE])
// emit this->trackLyricsReady(track.first()[FMH::MODEL_KEY::LYRICS], url);
// else
this->fetchTrackLyrics(track.first());
}
......@@ -208,15 +208,31 @@ QString Babe::albumArt(const QString &album, const QString &artist)
}
void Babe::fetchTrackLyrics(FMH::MODEL &song)
{
this->pulpo->registerServices({SERVICES::LyricWikia, SERVICES::Genius});
this->pulpo->setOntology(PULPO::ONTOLOGY::TRACK);
this->pulpo->setInfo(PULPO::INFO::LYRICS);
{/*
auto p = new Pulpo(this);
connect(p, &Pulpo::infoReady, [=](const FMH::MODEL &track, const PULPO::RESPONSE &res)
{
qDebug()<<"GOT THE LYRICS";
if(!res[PULPO::ONTOLOGY::TRACK][PULPO::INFO::LYRICS].isEmpty())
{
auto lyrics = res[PULPO::ONTOLOGY::TRACK][PULPO::INFO::LYRICS][PULPO::CONTEXT::LYRIC].toString();
// this->db->lyricsTrack(track, lyrics);
emit this->trackLyricsReady(lyrics, track[FMH::MODEL_KEY::URL]);
p->deleteLater();
}
});
p->registerServices({SERVICES::Genius});
p->setOntology(PULPO::ONTOLOGY::TRACK);
p->setInfo(PULPO::INFO::LYRICS);
qDebug()<<"STARTED FETCHING LYRICS";
this->pulpo->feed(song, PULPO::RECURSIVE::OFF);
p->feed(song, PULPO::RECURSIVE::OFF);
qDebug()<<"DONE FETCHING LYRICS";
qDebug()<<"DONE FETCHING LYRICS";*/
}
//void Babe::linkDecoder(QString json)
......
This diff is collapsed.
......@@ -26,20 +26,27 @@
//#include "qgumbodocument.h"
//#include "qgumbonode.h"
Pulpo::Pulpo(const FMH::MODEL &song,QObject *parent)
Pulpo::Pulpo(const FMH::MODEL &song, QObject *parent)
: QObject(parent), track(song) {}
Pulpo::Pulpo(QObject *parent): QObject(parent) {}
Pulpo::~Pulpo() {}
Pulpo::~Pulpo()
{
qDebug()<< "DELETING PULPO INSTANCE";
for(auto &instance : this->serviceInstances)
delete instance;
}
bool Pulpo::feed(const FMH::MODEL &song, const RECURSIVE &recursive)
{
if(song.isEmpty())
return false;
this->track = song;
this->recursive = recursive;
if(this->registeredServices.isEmpty()) return false;
if(this->track.isEmpty()) return false;
this->initServices();
......@@ -101,106 +108,111 @@ void Pulpo::initServices()
for(auto service : this->registeredServices)
switch (service)
{
case SERVICES::LastFm:
{
lastfm lastfm(this->track);
connect(&lastfm, &lastfm::infoReady, this, &Pulpo::passSignal);
if(lastfm.setUpService(this->ontology, this->info))
case SERVICES::LastFm:
{
if(recursive == RECURSIVE::OFF) return;
auto lastfm = new class lastfm(this->track);
this->serviceInstances.push_back(lastfm);
connect(lastfm, &lastfm::infoReady,[=](FMH::MODEL track, PULPO::RESPONSE response)
{
this->passSignal(track, response);
// lastfm->deleteLater();
});
}else qDebug()<<"Error settingUp lastfm service";
if(lastfm->setUpService(this->ontology, this->info))
{
if(recursive == RECURSIVE::OFF) return;
break;
}
}else qDebug()<<"Error settingUp lastfm service";
case SERVICES::Spotify:
{
spotify spotify(this->track);
connect(&spotify, &spotify::infoReady, this, &Pulpo::passSignal);
break;
}
if(spotify.setUpService(this->ontology,this->info))
case SERVICES::Spotify:
{
if(recursive== RECURSIVE::OFF) return;
spotify spotify(this->track);
connect(&spotify, &spotify::infoReady, this, &Pulpo::passSignal);
}else qDebug()<<"Error settingUp spotify service";
if(spotify.setUpService(this->ontology,this->info))
{
if(recursive== RECURSIVE::OFF) return;
break;
}
case SERVICES::Genius:
{
genius genius(this->track);
connect(&genius, &genius::infoReady, this, &Pulpo::passSignal);
}else qDebug()<<"Error settingUp spotify service";
if(genius.setUpService(this->ontology,this->info))
break;
}
case SERVICES::Genius:
{
if(recursive== RECURSIVE::OFF) return;
auto genius = new class genius(this->track);
connect(genius, &genius::infoReady, this, &Pulpo::passSignal);
}else qDebug()<<"Error settingUp genius service";
if(genius->setUpService(this->ontology,this->info))
{
if(recursive== RECURSIVE::OFF) return;
break;
}
case SERVICES::MusicBrainz:
{
musicBrainz musicbrainz(this->track);
connect(&musicbrainz, &musicBrainz::infoReady, this, &Pulpo::passSignal);
}else qDebug()<<"Error settingUp genius service";
if(musicbrainz.setUpService(this->ontology,this->info))
break;
}
case SERVICES::MusicBrainz:
{
if(recursive== RECURSIVE::OFF) return;
musicBrainz musicbrainz(this->track);
connect(&musicbrainz, &musicBrainz::infoReady, this, &Pulpo::passSignal);
}else qDebug()<<"Error settingUp musicBrainz service";
if(musicbrainz.setUpService(this->ontology,this->info))
{
if(recursive== RECURSIVE::OFF) return;
break;
}
case SERVICES::iTunes:
{
break;
}
case SERVICES::WikiLyrics:
{
break;
}
case SERVICES::LyricWikia:
{
lyricWikia lyricwikia(this->track);
connect(&lyricwikia, &lyricWikia::infoReady, this, &Pulpo::passSignal);
}else qDebug()<<"Error settingUp musicBrainz service";
if(lyricwikia.setUpService(this->ontology,this->info))
break;
}
case SERVICES::iTunes:
{
break;
}
case SERVICES::WikiLyrics:
{
if(recursive== RECURSIVE::OFF) return;
break;
}
case SERVICES::LyricWikia:
{
auto lyricwikia = new lyricWikia(this->track);
connect(lyricwikia, &lyricWikia::infoReady, this, &Pulpo::passSignal);
}else qDebug()<<"Error settingUp lyricwikia service";
if(lyricwikia->setUpService(this->ontology, this->info))
{
if(recursive == RECURSIVE::OFF) return;
break;
}
case SERVICES::Wikipedia:
{
break;
}
}else qDebug()<<"Error settingUp lyricwikia service";
case SERVICES::Deezer:
{
deezer deezer(this->track);
connect(&deezer, &deezer::infoReady, this, &Pulpo::passSignal);
break;
}
case SERVICES::Wikipedia:
{
break;
}
if(deezer.setUpService(this->ontology, this->info))
case SERVICES::Deezer:
{
if(recursive== RECURSIVE::OFF) return;
deezer deezer(this->track);
connect(&deezer, &deezer::infoReady, this, &Pulpo::passSignal);
}else qDebug()<<"Error settingUp deezer service";
if(deezer.setUpService(this->ontology, this->info))
{
if(recursive== RECURSIVE::OFF) return;
break;
}
case SERVICES::ALL:
{
break;
}
case SERVICES::NONE:
{
break;
}
}else qDebug()<<"Error settingUp deezer service";
break;
}
case SERVICES::ALL:
{
break;
}
case SERVICES::NONE:
{
break;
}
}
}
......@@ -226,10 +238,10 @@ bool Pulpo::parseArray()
switch(this->ontology)
{
case PULPO::ONTOLOGY::ALBUM: return this->parseAlbum();
case PULPO::ONTOLOGY::ARTIST: return this->parseArtist();
case PULPO::ONTOLOGY::TRACK: return this->parseTrack();
default: return false;
case PULPO::ONTOLOGY::ALBUM: return this->parseAlbum();
case PULPO::ONTOLOGY::ARTIST: return this->parseArtist();
case PULPO::ONTOLOGY::TRACK: return this->parseTrack();
default: return false;
}
}
......@@ -278,12 +290,12 @@ void Pulpo::startConnectionAsync(const QString &url, const QMap<QString,QString>
auto downloader = new FMH::Downloader;
connect(downloader, &FMH::Downloader::dataReady, [=](QByteArray array)
{
qDebug()<< "DATA READY << " << array;
// qDebug()<< "DATA READY << " << array;
emit this->arrayReady(array);
// downloader->deleteLater();
downloader->deleteLater();
});
qDebug()<< "trying to get AYNC DOWNLOADER for "<< url;
// qDebug()<< "trying to get ASYNC DOWNLOADER for "<< url;
downloader->getArray(url);
}
}
......
......@@ -48,6 +48,8 @@ private:
QNetworkAccessManager *manager;
QNetworkReply *reply;
QList<Pulpo *> serviceInstances;
protected:
QByteArray array;
FMH::MODEL track;
......
......@@ -7,6 +7,12 @@ genius::genius(const FMH::MODEL &song)
this->availableInfo.insert(ONTOLOGY::TRACK, {INFO::TAGS, INFO::WIKI, INFO::ARTWORK, INFO::LYRICS});
this->track = song;
connect(this, &genius::arrayReady, [this](QByteArray data)
{
this->array = data;
this->parseArray();
});
}
bool genius::setUpService(const PULPO::ONTOLOGY &ontology, const PULPO::INFO &info)
......@@ -28,22 +34,22 @@ bool genius::setUpService(const PULPO::ONTOLOGY &ontology, const PULPO::INFO &in
switch(this->ontology)
{
case PULPO::ONTOLOGY::ARTIST:
{
url.append("/search?q=");
url.append(encodedArtist.toString());
break;
}
case PULPO::ONTOLOGY::ARTIST:
{
url.append("/search?q=");
url.append(encodedArtist.toString());
break;
}
case PULPO::ONTOLOGY::TRACK:
{
url.append("/search?q=");
url.append(encodedTrack.toString());
url.append(" " + encodedArtist.toString());
break;
}
case PULPO::ONTOLOGY::TRACK:
{
url.append("/search?q=");
url.append(encodedTrack.toString());
url.append(" " + encodedArtist.toString());
break;
}
default: return false;
default: return false;
}
qDebug()<< "[genius service]: "<< url;
......@@ -52,10 +58,8 @@ bool genius::setUpService(const PULPO::ONTOLOGY &ontology, const PULPO::INFO &in
qDebug()<< "[genius service]: "<< newUrl;
this->array = this->startConnection(newUrl, {{"Authorization", this->KEY}} );
if(this->array.isEmpty()) return false;
return this->parseArray();
this->startConnectionAsync(newUrl, {{"Authorization", this->KEY}} );
return true;
}
QString genius::getID(const QString &url)
......@@ -82,17 +86,17 @@ QString genius::getID(const QString &url)
switch(this->ontology)
{
case ONTOLOGY::ARTIST:
{
id = hits.first().toMap().value("result").toMap().value("primary_artist").toMap().value("api_path").toString();
break;
}
case ONTOLOGY::TRACK:
{
id = hits.first().toMap().value("result").toMap().value("api_path").toString();
break;
}
default: break;
case ONTOLOGY::ARTIST:
{
id = hits.first().toMap().value("result").toMap().value("primary_artist").toMap().value("api_path").toString();
break;
}
case ONTOLOGY::TRACK:
{
id = hits.first().toMap().value("result").toMap().value("api_path").toString();
break;
}
default: break;
}
return !id.isEmpty()? this->API+id : id;
......@@ -162,7 +166,6 @@ bool genius::parseArtist()
bool genius::parseTrack()
{
QJsonParseError jsonParseError;
QJsonDocument jsonResponse = QJsonDocument::fromJson(static_cast<QString>(this->array).toUtf8(), &jsonParseError);
......
#include "lastfmService.h"
lastfm::lastfm(const FMH::MODEL &song)
{
this->availableInfo.insert(ONTOLOGY::ALBUM, {INFO::ARTWORK, INFO::WIKI, INFO::TAGS});
......@@ -7,6 +8,19 @@ lastfm::lastfm(const FMH::MODEL &song)
this->availableInfo.insert(ONTOLOGY::TRACK, {INFO::TAGS, INFO::WIKI, INFO::ARTWORK, INFO::METADATA});
this->track = song;
connect(this, &lastfm::arrayReady, [this](QByteArray data)
{
qDebug()<< "GOT THE ARRAY ON LASTFM";
this->array = data;
if(!this->parseArray())
emit this->infoReady(this->track, PULPO::RESPONSE{});
});
}
lastfm::~lastfm()
{
qDebug()<< "DELETING LASTFM INSTANCE";
}
bool lastfm::setUpService(const PULPO::ONTOLOGY &ontology, const PULPO::INFO &info)
......@@ -24,49 +38,47 @@ bool lastfm::setUpService(const PULPO::ONTOLOGY &ontology, const PULPO::INFO &in
switch(this->ontology)
{
case PULPO::ONTOLOGY::ARTIST:
{
url.append("?method=artist.getinfo");
url.append(KEY);
url.append("&artist=" + encodedArtist.toString());
break;
}
case PULPO::ONTOLOGY::ALBUM:
{
QUrl encodedAlbum(this->track[FMH::MODEL_KEY::ALBUM]);
encodedAlbum.toEncoded(QUrl::FullyEncoded);
case PULPO::ONTOLOGY::ARTIST:
{
url.append("?method=artist.getinfo");
url.append(KEY);
url.append("&artist=" + encodedArtist.toString());
break;
}
url.append("?method=album.getinfo");
url.append(KEY);
url.append("&artist=" + encodedArtist.toString());
url.append("&album=" + encodedAlbum.toString());
break;
}
case PULPO::ONTOLOGY::ALBUM:
{
QUrl encodedAlbum(this->track[FMH::MODEL_KEY::ALBUM]);
encodedAlbum.toEncoded(QUrl::FullyEncoded);
url.append("?method=album.getinfo");
url.append(KEY);
url.append("&artist=" + encodedArtist.toString());
url.append("&album=" + encodedAlbum.toString());
break;
}
case PULPO::ONTOLOGY::TRACK:
{
QUrl encodedTrack(this->track[FMH::MODEL_KEY::TITLE]);
encodedTrack.toEncoded(QUrl::FullyEncoded);
case PULPO::ONTOLOGY::TRACK:
{
QUrl encodedTrack(this->track[FMH::MODEL_KEY::TITLE]);
encodedTrack.toEncoded(QUrl::FullyEncoded);
url.append("?method=track.getinfo");
url.append(KEY);
url.append("&artist=" + encodedArtist.toString());
url.append("&track=" + encodedTrack.toString());
url.append("&format=json");
url.append("?method=track.getinfo");
url.append(KEY);
url.append("&artist=" + encodedArtist.toString());
url.append("&track=" + encodedTrack.toString());
url.append("&format=json");
break;
}
break;
}
default: return false;
default: return false;
}
qDebug()<< "[lastfm service]: "<< url;
this->array = this->startConnection(url);
if(this->array.isEmpty()) return false;
return this->parseArray();
this->startConnectionAsync(url);
return true;
}
......@@ -101,7 +113,7 @@ bool lastfm::parseArtist()
{
auto imgSize = n.attributes().namedItem("size").nodeValue();
if (imgSize == "extralarge" && n.isElement())
if (imgSize == "medium" && n.isElement())
{
auto artistArt_url = n.toElement().text();
......@@ -209,14 +221,22 @@ bool lastfm::parseArtist()
bool lastfm::parseAlbum()
{
qDebug()<< "PARSING ALBUM ON LASTFM";
QString xmlData(this->array);
QDomDocument doc;
if (!doc.setContent(xmlData))
{
qDebug()<< "LASTFM XML FAILED 1";
return false;
}
if (doc.documentElement().toElement().attributes().namedItem("status").nodeValue()!="ok")
{
qDebug()<< "LASTFM XML FAILED 2";
return false;
}
const auto nodeList = doc.documentElement().namedItem("album").childNodes();
......@@ -233,11 +253,16 @@ bool lastfm::parseAlbum()
{
auto imgSize = n.attributes().namedItem("size").nodeValue();
if (imgSize == "extralarge" && n.isElement())
if (imgSize == "large" && n.isElement())
{
auto albumArt_url = n.toElement().text();
emit this->infoReady(this->track,this->packResponse(ONTOLOGY::ALBUM, INFO::ARTWORK,CONTEXT::IMAGE,startConnection(albumArt_url)));
qDebug()<< "GOT THE ARTWORK URL" << albumArt_url;
emit this->infoReady(this->track,
this->packResponse(ONTOLOGY::ALBUM,
INFO::ARTWORK,
CONTEXT::IMAGE,
albumArt_url));
if(this->info == INFO::ARTWORK) return true;
else continue;
......
......@@ -17,6 +17,7 @@ private:
public:
explicit lastfm(const FMH::MODEL &song);
virtual ~lastfm();
virtual bool setUpService(const PULPO::ONTOLOGY &ontology, const PULPO::INFO &info);
protected:
......
......@@ -4,6 +4,19 @@ lyricWikia::lyricWikia(const FMH::MODEL &song)
{
this->availableInfo.insert(ONTOLOGY::TRACK, {INFO::LYRICS});
this->track = song;
connect(this, &lyricWikia::arrayReady, [this](QByteArray data)
{
qDebug()<< "GOT THE ARRAY";
this->array = data;
this->parseArray();
});
}
lyricWikia::~lyricWikia()
{
}
bool lyricWikia::setUpService(const PULPO::ONTOLOGY &ontology, const PULPO::INFO &info)
......@@ -18,30 +31,29 @@ bool lyricWikia::setUpService(const PULPO::ONTOLOGY &ontology, const PULPO::INFO
switch(this->ontology)
{
case PULPO::ONTOLOGY::TRACK:
{
QUrl encodedArtist(this->track[FMH::MODEL_KEY::ARTIST]);
encodedArtist.toEncoded(QUrl::FullyEncoded);
case PULPO::ONTOLOGY::TRACK:
{
QUrl encodedArtist(this->track[FMH::MODEL_KEY::ARTIST]);
encodedArtist.toEncoded(QUrl::FullyEncoded);
QUrl encodedTrack(this->track[FMH::MODEL_KEY::TITLE]);
encodedTrack.toEncoded(QUrl::FullyEncoded);
QUrl encodedTrack(this->track[FMH::MODEL_KEY::TITLE]);
encodedTrack.toEncoded(QUrl::FullyEncoded);
url.append("&artist=" + encodedArtist.toString());
url.append("&song=" + encodedTrack.toString());
url.append("&fmt=xml");
url.append("&artist=" + encodedArtist.toString());
url.append("&song=" + encodedTrack.toString());
url.append("&fmt=xml");
break;
}
default: return false;
break;
}
default: return false;
}
qDebug()<< "[lyricwikia service]: "<< url;
this->array = this->startConnection(url);
if(this->array.isEmpty()) return false;
this->startConnectionAsync(url);
return this->parseArray();
return true;
}
bool lyricWikia::parseTrack()
......@@ -58,33 +70,28 @@ bool lyricWikia::parseTrack()
temp = temp_u.toString();
temp.replace("http://lyrics.wikia.com/","http://lyrics.wikia.com/index.php?title=");
temp.replace("http://lyrics.fandom.com/","http://lyrics.fandom.com/index.php?title=");
temp.append("&action=edit");
QRegExp url_regexp("<url>(.*)</url>");
url_regexp.setMinimal(true);
QUrl url = QUrl::fromEncoded(temp.toLatin1());
QString referer = url_regexp.cap(1);
QNetworkRequest request;
request.setUrl(url);
request.setRawHeader("Referer", referer.toLatin1());
qDebug("Receiving lyrics");
QNetworkAccessManager m_http;
QNetworkReply *reply = m_http.get(request);
auto downloader = new FMH::Downloader;
connect(downloader, &FMH::Downloader::dataReady, [=] (QByteArray data)
{
qDebug() << "Receiving lyrics" << data;
QEventLoop loop;
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), &loop, SLOT(quit()));
loop.exec();
if(data.isEmpty())
return;
QByteArray array(reply->readAll());
delete reply;
this->extractLyrics(data);
if(array.isEmpty()) return false;
downloader->deleteLater();
});
downloader->getArray(QUrl(url).toEncoded(), {{"Referer", referer.toLatin1()}});
return this->extractLyrics(array);
return true;
}
bool lyricWikia::extractLyrics(const QByteArray &array)
......
......@@ -8,12 +8,13 @@ class lyricWikia : public Pulpo
Q_OBJECT
private:
const QString API = "http://lyrics.wikia.com/api.php?action=lyrics";
const QString API = "https://lyrics.fandom.com/api.php?action=lyrics";
bool extractLyrics(const QByteArray &array);
public:
explicit lyricWikia(const FMH::MODEL &song);
~lyricWikia();
virtual bool setUpService(const ONTOLOGY &ontology, const INFO &info);
protected:
......
......@@ -68,13 +68,14 @@ BabeSettings::BabeSettings(QObject *parent) : QObject(parent)
connect(this->brainDeamon, &Brain::done, [this](const TABLE type)
{
qDebug()<< "FINISHED WITH" << TABLEMAP[type];
emit this->refreshATable(type);
});
connect(&this->fileLoader, &FileLoader::finished, [this](int size)
{
// if(size > 0)
// this->startBrainz(true, BAE::SEG::HALF);
this->startBrainz(true, BAE::SEG::HALF);
// else
// this->startBrainz(BAE::loadSettings("AUTO", "BRAINZ", true).toBool(), BAE::SEG::TWO);
......
......@@ -29,7 +29,7 @@ function playTrack(index)
}
// if(currentTrack.lyrics.length < 1)
// bae.trackLyrics(currentTrack.url)
bae.trackLyrics(currentTrack.url)