lyricwikiaService.cpp 3.03 KB
Newer Older
1 2
#include "lyricwikiaService.h"

3
lyricWikia::lyricWikia(const FMH::MODEL &song)
4 5 6
{
    this->availableInfo.insert(ONTOLOGY::TRACK, {INFO::LYRICS});
    this->track = song;
7 8 9 10 11 12 13 14 15 16 17 18 19

    connect(this, &lyricWikia::arrayReady, [this](QByteArray data)
    {

        qDebug()<< "GOT THE ARRAY";
        this->array = data;
        this->parseArray();
    });
}

lyricWikia::~lyricWikia()
{

20 21 22 23 24 25 26 27 28 29 30 31 32 33
}

bool lyricWikia::setUpService(const PULPO::ONTOLOGY &ontology, const PULPO::INFO &info)
{
    this->ontology = ontology;
    this->info = info;

    if(!this->availableInfo[this->ontology].contains(this->info))
        return false;

    auto url = this->API;

    switch(this->ontology)
    {
34 35 36 37
        case PULPO::ONTOLOGY::TRACK:
        {
            QUrl encodedArtist(this->track[FMH::MODEL_KEY::ARTIST]);
            encodedArtist.toEncoded(QUrl::FullyEncoded);
38

39 40
            QUrl encodedTrack(this->track[FMH::MODEL_KEY::TITLE]);
            encodedTrack.toEncoded(QUrl::FullyEncoded);
41

42 43 44
            url.append("&artist=" + encodedArtist.toString());
            url.append("&song=" + encodedTrack.toString());
            url.append("&fmt=xml");
45

46 47
            break;
        }
48

49
        default: return false;
50 51 52 53
    }

    qDebug()<< "[lyricwikia service]: "<< url;

54
    this->startConnectionAsync(url);
55

56
    return true;
57 58 59
}

bool lyricWikia::parseTrack()
60
{
61 62 63 64 65 66 67 68 69 70
    QString xmlData(this->array);

    QDomDocument doc;

    if (!doc.setContent(xmlData)) return false;

    QString temp = doc.documentElement().namedItem("url").toElement().text().toLatin1();
    QUrl temp_u (temp);
    temp_u.toEncoded(QUrl::FullyEncoded);

Camilo higuita's avatar
Camilo higuita committed
71
    temp = temp_u.toString();
72

73
    temp.replace("http://lyrics.fandom.com/","http://lyrics.fandom.com/index.php?title=");
74 75 76 77 78 79
    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);

80 81 82 83
    auto downloader = new FMH::Downloader;
    connect(downloader, &FMH::Downloader::dataReady, [=] (QByteArray data)
    {
        qDebug() << "Receiving lyrics" << data;
84

85 86
        if(data.isEmpty())
            return;
87

88
        this->extractLyrics(data);
89

90 91 92
        downloader->deleteLater();
    });
    downloader->getArray(QUrl(url).toEncoded(), {{"Referer", referer.toLatin1()}});
93

94
    return true;
95 96 97 98 99 100 101 102 103
}

bool lyricWikia::extractLyrics(const QByteArray &array)
{
    QString content = QString::fromUtf8(array.constData());
    content.replace("&lt;", "<");
    QRegExp lyrics_regexp("<lyrics>(.*)</lyrics>");
    lyrics_regexp.indexIn(content);
    QString lyrics = lyrics_regexp.cap(1);
Camilo higuita's avatar
Camilo higuita committed
104 105 106

    if(lyrics.isEmpty()) return false;

107 108
    lyrics = lyrics.trimmed();
    lyrics.replace("\n", "<br>");
Camilo higuita's avatar
Camilo higuita committed
109 110 111

    QString text;

112 113
    if(!lyrics.contains("PUT LYRICS HERE")&&!lyrics.isEmpty())
    {
114
        text = "<h2 align='center' >" + this->track[FMH::MODEL_KEY::TITLE] + "</h2>";
115 116 117 118 119 120 121 122
        text += lyrics;

        text= "<div align='center'>"+text+"</div>";
    }

    emit this->infoReady(this->track, this->packResponse(ONTOLOGY::TRACK, INFO::LYRICS,CONTEXT::LYRIC,text));
    return true;
}