brain.h 9.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#ifndef BRAIN_H
#define BRAIN_H

/* This deamon keeps on running while there are missing information about a track,
 *  it should have the option to turn it off, but the main idea is to here have the
 * brains of the app and collection. so this must be a very good a neat implementation */

#include <QObject>
#include <QThread>

#include "bae.h"
#include "../pulpo/pulpo.h"
13
#include "../db/collectionDB.h"
14 15 16 17

using namespace BAE;
using namespace PULPO;

18 19
namespace BRAIN
{
20 21 22
struct QUEUE
{
private:
23
    QList<PULPO::REQUEST> requests;
24 25 26
    int index = -1;

public:
27
    PULPO::REQUEST next()
28 29 30
    {
        index++;
        if(index < 0 || index >= requests.size())
31
            return PULPO::REQUEST{};
32 33 34 35 36

        const auto res = requests.at(index);
        return res;
    }

37 38 39 40 41
    int currentIndex()
    {
        return index;
    }

42 43 44 45 46 47 48 49 50 51
    bool hasNext() const
    {
        return index + 1 < requests.size();
    }

    int size() const
    {
        return requests.size();
    }

52
    void append(const PULPO::REQUEST &request)
53 54 55 56
    {
        requests << request;
    }

57
    void operator<< (const PULPO::REQUEST &request)
58 59 60 61
    {
        append(request);
    }

62 63 64 65 66
    void operator<< (const QUEUE &request)
    {
        requests << request.requests;
    }

67 68
};

69 70

inline static QUEUE artistArtworks()
71
{
72
    QUEUE requests;
73

74 75
    auto services = {PULPO::SERVICES::LastFm, PULPO::SERVICES::Spotify, PULPO::SERVICES::MusicBrainz, PULPO::SERVICES::Genius};
    auto ontology = PULPO::ONTOLOGY::ARTIST;
76

77 78 79
    qDebug() << ("Getting missing artists artworks");
    auto queryTxt = QString("SELECT %1 FROM %2 WHERE %3 = ''").arg(KEYMAP[KEY::ARTIST],
            TABLEMAP[TABLE::ARTISTS], KEYMAP[KEY::ARTWORK]);
80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    auto db = CollectionDB::getInstance();
    auto artworks = db->getDBData(queryTxt);


    /* BEFORE FETCHING ONLINE LOOK UP IN THE CACHE FOR THE IMAGE */
    for(auto artist : artworks)
        if(BAE::artworkCache(artist, FMH::MODEL_KEY::ARTIST))
            db->insertArtwork(artist);

    artworks = db->getDBData(queryTxt);
    //    this->setInfo(artworks, ontology, services, PULPO::INFO::ARTWORK, PULPO::RECURSIVE::OFF, nullptr);
    qDebug()<< "MISSING ARTIST IMAGES"<< artworks.size() << queryTxt;
    for(const auto &item : artworks)
    {
        REQUEST request;
        request.track = item;
        request.ontology =  ontology;
        request.services = services;
        request.info = {PULPO::INFO::ARTWORK};
        request.callback = [=](PULPO::REQUEST request, PULPO::RESPONSES responses)
        {
            qDebug() << "DONE WITH " << request.track ;

            for(const auto &res : responses)
            {
                if(res.context == PULPO::CONTEXT::IMAGE && !res.value.toString().isEmpty())
                {
                    qDebug()<<"SAVING ARTWORK FOR: " << request.track[FMH::MODEL_KEY::ARTIST];
                    auto downloader = new FMH::Downloader;
                    QObject::connect(downloader, &FMH::Downloader::fileSaved, [=](QString path)
                    {
                        qDebug()<< "Saving artwork file to" << path;
                        FMH::MODEL newTrack = request.track;
                        newTrack[FMH::MODEL_KEY::ARTWORK] = path;
                        db->insertArtwork(newTrack);

                        downloader->deleteLater();

                    });
120

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
                    QStringList filePathList = res.value.toString().split('/');
                    const auto format = "." + filePathList.at(filePathList.count() - 1).split(".").last();
                    QString name = !request.track[FMH::MODEL_KEY::ALBUM].isEmpty() ? request.track[FMH::MODEL_KEY::ARTIST] + "_" + request.track[FMH::MODEL_KEY::ALBUM] : request.track[FMH::MODEL_KEY::ARTIST];
                    name.replace("/", "-");
                    name.replace("&", "-");
                    downloader->setFile(res.value.toString(),  BAE::CachePath + name + format);
                }
            }
        };

        requests<< request;
    }

    return requests;
}

inline static QUEUE albumArtworks()
{
    QUEUE requests;
    auto services = {PULPO::SERVICES::LastFm, PULPO::SERVICES::Spotify, PULPO::SERVICES::MusicBrainz};
    auto ontology = PULPO::ONTOLOGY::ALBUM;

    const auto queryTxt = QString("SELECT %1, %2 FROM %3 WHERE %4 = ''").arg(KEYMAP[KEY::ALBUM],
            KEYMAP[KEY::ARTIST], TABLEMAP[TABLE::ALBUMS], KEYMAP[KEY::ARTWORK]);

    auto db = CollectionDB::getInstance();
    /* BEFORE FETCHING ONLINE LOOK UP IN THE CACHE FOR THE IMAGES*/
    auto artworks = db->getDBData(queryTxt);
    for(auto album : artworks)
        if(BAE::artworkCache(album, FMH::MODEL_KEY::ALBUM))
            db->insertArtwork(album);

    artworks = db->getDBData(queryTxt);
    qDebug() << "Getting missing albums artworks"<< artworks.length();

    for(const auto &item : artworks)
    {
        REQUEST request;
        request.track = item;
        request.ontology =  ontology;
        request.services = services;
        request.info = {PULPO::INFO::ARTWORK};
        request.callback = [=](PULPO::REQUEST request, PULPO::RESPONSES responses)
        {
            qDebug() << "DONE WITH " << request.track ;

            for(const auto &res : responses)
            {
                if(res.context == PULPO::CONTEXT::IMAGE && !res.value.toString().isEmpty())
                {
                    qDebug()<<"SAVING ARTWORK FOR: " << request.track[FMH::MODEL_KEY::ALBUM];
                    auto downloader = new FMH::Downloader;
                    QObject::connect(downloader, &FMH::Downloader::fileSaved, [=](QString path)
                    {
                        qDebug()<< "Saving artwork file to" << path;
                        FMH::MODEL newTrack = request.track;
                        newTrack[FMH::MODEL_KEY::ARTWORK] = path;
                        db->insertArtwork(newTrack);

                        downloader->deleteLater();

                    });

                    QStringList filePathList = res.value.toString().split('/');
                    const auto format = "." + filePathList.at(filePathList.count() - 1).split(".").last();
                    QString name = !request.track[FMH::MODEL_KEY::ALBUM].isEmpty() ? request.track[FMH::MODEL_KEY::ARTIST] + "_" + request.track[FMH::MODEL_KEY::ALBUM] : request.track[FMH::MODEL_KEY::ARTIST];
                    name.replace("/", "-");
                    name.replace("&", "-");
                    downloader->setFile(res.value.toString(),  BAE::CachePath + name + format);
                }
            }
        };

        requests<< request;
    }
196

197 198
    return requests;
}
199

200 201 202 203 204

struct PACKAGE
{
    PULPO::ONTOLOGY ontology;
    PULPO::INFO info;
205
    std::function<void(int index)> callback = nullptr;
206
};
207 208 209 210 211 212 213 214 215 216 217
typedef QList<PACKAGE> PACKAGES;

inline void synapse(const BRAIN::PACKAGES &packages)
{
    if(packages.isEmpty())
        return;

    Pulpo pulpo;
    QEventLoop loop;
    QObject::connect(&pulpo, &Pulpo::finished, &loop, &QEventLoop::quit);

218
    auto func = [&](QUEUE &m_requests, std::function<void(int index)> cb = nullptr)
219 220 221 222
    {
        while(m_requests.hasNext())
        {
            pulpo.request(m_requests.next());
223 224
            if(cb)
                cb(m_requests.currentIndex());
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
            loop.exec();
        }
    };

    for(const auto &package : packages)
    {
        switch(package.ontology)
        {
            case PULPO::ONTOLOGY::ALBUM :
            {
                switch(package.info)
                {
                    case PULPO::INFO::ARTWORK:
                    {
                        QUEUE request = BRAIN::albumArtworks();
                        func(request, package.callback);
                        break;
                    }

                    case PULPO::INFO::TAGS:
                    {
                        break;
                    }

                    case PULPO::INFO::WIKI:
                    {
                        break;
                    }
                }

                break;
            }

            case PULPO::ONTOLOGY::ARTIST :
            {
                switch(package.info)
                {
                    case PULPO::INFO::ARTWORK:
                    {
                        QUEUE request = BRAIN::artistArtworks();
                        func(request, package.callback);
                        break;
                    }

                    case PULPO::INFO::TAGS:
                    {
                        break;
                    }

                    case PULPO::INFO::WIKI:
                    {
                        break;
                    }
                }
                break;
            }

            case PULPO::ONTOLOGY::TRACK :
            {
                switch(package.info)
                {
                    case PULPO::INFO::ARTWORK:
                    {

                        break;
                    }

                    case PULPO::INFO::TAGS:
                    {
                        break;
                    }

                    case PULPO::INFO::WIKI:
                    {
                        break;
                    }
                }
                break;
            }
        }
    }
}


void parseAlbumInfo(FMH::MODEL &track, const PULPO::INFO_K &response);
void parseArtistInfo(FMH::MODEL &track, const PULPO::INFO_K &response);
void parseTrackInfo(FMH::MODEL &track, const PULPO::INFO_K &response);
void trackInfo();
void artistInfo();
void albumInfo();





void tags();
void wikis();
void albumTags();
void albumWikis();

void artistTags();
void artistWikis();

void trackArtworks();
void trackLyrics();
void trackTags();
void trackWikis();
}
333 334

#endif // BRAIN_H