brain.cpp 15.7 KB
Newer Older
1 2
#include "brain.h"
#include "../services/local/taginfo.h"
3
#include "../utils/babeconsole.h"
4

5
Brain::Brain(QObject *parent) : QObject (parent)
6
{
7

8 9 10 11 12 13 14 15
}

Brain::~Brain()
{
    qDebug()<<"Deleting Brainz obj";
}


16 17


18

19
void Brain::parseAlbumInfo(FMH::MODEL &track, const INFO_K &response)
20 21 22 23 24
{

    for(auto info : response.keys())
        switch(info)
        {
25 26 27
            case PULPO::INFO::TAGS:
            {
                for(auto context : response[info].keys())
28

29 30 31 32
                    if(!response[info][context].toMap().isEmpty())
                    {
                        for(auto tag : response[info][context].toMap().keys() )
                            this->db->tagsAlbum(track, tag, CONTEXT_MAP[context]);
33

34 35 36 37
                    }else if (!response[info][context].toStringList().isEmpty())
                    {
                        for(auto tag : response[info][context].toStringList() )
                            this->db->tagsAlbum(track, tag, CONTEXT_MAP[context]);
38

39 40 41 42 43 44
                    } else if (!response[info][context].toString().isEmpty())
                    {
                        this->db->tagsAlbum(track, response[info][context].toString(), CONTEXT_MAP[context]);
                    }
                break;
            }
45

46 47 48
            case PULPO::INFO::ARTWORK:
            {
                if(!response[info].isEmpty())
49

50 51 52 53
                    if(!response[info][CONTEXT::IMAGE].toString().isEmpty())
                    {
                        qDebug()<<"SAVING ARTWORK FOR: " << track[FMH::MODEL_KEY::ALBUM];
                        auto downloader = new FMH::Downloader;
54 55 56



57 58 59 60 61 62
                        connect(downloader, &FMH::Downloader::fileSaved, [=](QString path)
                        {
                            qDebug()<< "Saving artwork file to" << path;
                            FMH::MODEL newTrack = track;
                            newTrack[FMH::MODEL_KEY::ARTWORK] = path;
                            this->db->insertArtwork(newTrack);
63

64
                            downloader->deleteLater();
65

66
                        });
67

68 69 70 71 72 73 74
                        QStringList filePathList = response[info][CONTEXT::IMAGE].toString().split('/');
                        const auto format = "." + filePathList.at(filePathList.count() - 1).split(".").last();
                        QString name = !track[FMH::MODEL_KEY::ALBUM].isEmpty() ? track[FMH::MODEL_KEY::ARTIST] + "_" + track[FMH::MODEL_KEY::ALBUM] : track[FMH::MODEL_KEY::ARTIST];
                        name.replace("/", "-");
                        name.replace("&", "-");
                        downloader->setFile(response[info][CONTEXT::IMAGE].toString(),  BAE::CachePath + name + format);
                    }
75

76 77
                break;
            }
78

79 80 81 82 83 84 85
            case PULPO::INFO::WIKI:
            {
                if(!response[info].isEmpty())
                    for (auto context : response[info].keys())
                        this->db->wikiAlbum(track, response[info][context].toString());
                break;
            }
86

87
            default: continue;
88 89 90
        }
}

91
void Brain::parseArtistInfo(FMH::MODEL &track, const INFO_K &response)
92 93 94 95 96
{
    for(auto info : response.keys())
    {
        switch(info)
        {
97
            case PULPO::INFO::TAGS:
98
            {
99
                if(!response[info].isEmpty())
100
                {
101
                    for(auto context : response[info].keys())
102
                    {
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
                        if(!response[info][context].toMap().isEmpty())
                        {
                            for(auto tag : response[info][context].toMap().keys() )
                                this->db->tagsArtist(track, tag, CONTEXT_MAP[context]);

                        }else if(!response[info][context].toStringList().isEmpty())
                        {
                            for(auto tag : response[info][context].toStringList() )
                                this->db->tagsArtist(track, tag, CONTEXT_MAP[context]);

                        }else if(!response[info][context].toString().isEmpty())
                        {
                            this->db->tagsArtist(track, response[info][context].toString(), CONTEXT_MAP[context]);
                        }
                    }
118

119 120
                } break;
            }
121

122 123 124 125 126
            case PULPO::INFO::ARTWORK:
            {
                if(!response[info].isEmpty())
                {
                    if(!response[info][CONTEXT::IMAGE].toByteArray().isEmpty())
127
                    {
128 129
                        BAE::saveArt(track, response[info][CONTEXT::IMAGE].toByteArray(), BAE::CachePath);
                        this->db->insertArtwork(track);
130 131 132
                    }
                }

133 134
                break;
            }
135

136
            case PULPO::INFO::WIKI:
137
            {
138
                if(!response[info].isEmpty())
139
                {
140 141
                    for (auto context : response[info].keys())
                        this->db->wikiArtist(track, response[info][context].toString());
142
                }
143

144
                break;
145 146
            }

147
            default: continue;
148 149 150 151
        }
    }
}

152
void Brain::parseTrackInfo(FMH::MODEL &track, const INFO_K &response)
153 154 155 156
{
    for(auto info : response.keys())
        switch(info)
        {
157
            case PULPO::INFO::TAGS:
158
            {
159
                if(!response[info].isEmpty())
160
                {
161
                    for(auto context : response[info].keys())
162
                    {
163 164 165 166 167 168 169 170
                        if (!response[info][context].toStringList().isEmpty())
                        {
                            for(auto tag : response[info][context].toStringList() )
                                this->db->tagsTrack(track, tag, CONTEXT_MAP[context]);
                        }

                        if (!response[info][context].toString().isEmpty())
                            this->db->tagsTrack(track, response[info][context].toString(), CONTEXT_MAP[context]);
171
                    }
172
                }
173

174
                break;
175
            }
Camilo higuita's avatar
Camilo higuita committed
176

177
            case PULPO::INFO::WIKI:
178
            {
179
                if(!response[info].isEmpty())
Camilo higuita's avatar
Camilo higuita committed
180
                {
181 182 183
                    if (!response[info][CONTEXT::WIKI].toString().isEmpty())
                        this->db->wikiTrack(track, response[info][CONTEXT::WIKI].toString());

184
                }
185

186 187
                break;
            }
188

189
            case PULPO::INFO::ARTWORK:
190
            {
191
                if(!response[info].isEmpty())
192
                {
193
                    if(!response[info][CONTEXT::IMAGE].toByteArray().isEmpty())
194
                    {
195 196
                        BAE::saveArt(track, response[info][CONTEXT::IMAGE].toByteArray(),CachePath);
                        this->db->insertArtwork(track);
197
                    }
Camilo higuita's avatar
Camilo higuita committed
198 199
                }

200 201
                break;
            }
202

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
            case PULPO::INFO::METADATA:
            {
                TagInfo tag;
                for(auto context :response[info].keys())
                {
                    switch(context)
                    {
                        case CONTEXT::ALBUM_TITLE:
                        {
                            qDebug()<<"SETTING TRACK MISSING METADATA";

                            tag.feed(track[FMH::MODEL_KEY::URL]);
                            if(!response[info][context].toString().isEmpty())
                            {
                                tag.setAlbum(response[info][context].toString());
                                this->db->albumTrack(track, response[info][context].toString());
                            }

                            break;
                        }

                        case CONTEXT::TRACK_NUMBER:
                        {
                            tag.feed(track[FMH::MODEL_KEY::URL]);
                            if(!response[info][context].toString().isEmpty())
                                tag.setTrack(response[info][context].toInt());

                            break;
                        }

                        default: continue;
                    }
235 236
                }

237
                break;
238
            }
Camilo higuita's avatar
Camilo higuita committed
239

240 241 242 243 244 245
            case PULPO::INFO::LYRICS:
            {
                if(!response[info][CONTEXT::LYRIC].toString().isEmpty())
                    this->db->lyricsTrack(track, response[info][CONTEXT::LYRIC].toString());
                break;
            }
246

247
            default: continue;
248 249 250 251 252 253 254 255 256
        }
}

void Brain::trackInfo()
{
    //        auto queryTxt =  QString("SELECT %1, %2, %3 FROM %4 WHERE %3 = 'UNKNOWN' GROUP BY %2, a.%3 ").arg(KEYMAP[KEY::TITLE],
    //                KEYMAP[KEY::ARTIST],KEYMAP[KEY::ALBUM],TABLEMAP[TABLE::TRACKS]);
    //        QSqlQuery query (queryTxt);
    //        pulpo.setInfo(INFO::METADATA);
257
    //        for(auto track : this->getDBData(query))
258 259 260 261 262 263 264 265
    //        {
    //            qDebug()<<"UNKOWN TRACK TITLE:"<<track[KEY::TITLE];
    //            pulpo.feed(track,RECURSIVE::OFF);
    //            if(!go) return;
    //        }

    // emit this->done(TABLE::TRACKS);

266 267 268 269
    this->trackArtworks();
    this->trackLyrics();
    this->trackTags();
    this->trackWikis();
270 271 272
}

void Brain::albumInfo()
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
{
    this->albumArtworks();
    this->albumTags();
    this->albumWikis();
}

void Brain::artistInfo()
{
    this->artistArtworks();
    this->artistTags();
    this->artistWikis();
}


void Brain::tags()
{
    this->albumTags();
    this->artistTags();
    this->trackTags();
}

void Brain::wikis()
{
    this->albumWikis();
    this->artistWikis();
    this->trackWikis();
}

301

302

303 304 305 306
void Brain::albumTags()
{
    auto services = {PULPO::SERVICES::LastFm, PULPO::SERVICES::Spotify, PULPO::SERVICES::MusicBrainz};
    auto ontology = PULPO::ONTOLOGY::ALBUM;
307

308
    //select album, artist from albums where  album  not in (select album from albums_tags) and artist  not in (select  artist from albums_tags)
Camilo higuita's avatar
Camilo higuita committed
309
    qDebug() << ("Getting missing albums tags");
310
    auto queryTxt =  QString("SELECT %1, %2 FROM %3 WHERE %1 NOT IN ( SELECT %1 FROM %4 ) AND %2 NOT IN ( SELECT %2 FROM %4 )").arg(KEYMAP[KEY::ALBUM],
311 312 313
            KEYMAP[KEY::ARTIST],
            TABLEMAP[TABLE::ALBUMS],
            TABLEMAP[TABLE::ALBUMS_TAGS]);
314

315 316 317 318 319 320 321 322 323
    //    REQUEST request;
    //    request.data = this->db->getDBData(queryTxt);
    //    request.ontology =  ontology;
    //    request.services = services;
    //    request.info = PULPO::INFO::TAGS;
    //    request.recursive = PULPO::RECURSIVE::ON;
    //    request.cb = nullptr;

    //    this->appendRequest(request);
324
}
325

326 327 328 329
void Brain::albumWikis()
{
    auto services = {PULPO::SERVICES::LastFm, PULPO::SERVICES::Spotify, PULPO::SERVICES::MusicBrainz};
    auto ontology = PULPO::ONTOLOGY::ALBUM;
330

Camilo higuita's avatar
Camilo higuita committed
331
    qDebug() << ("Getting missing albums wikis");
332
    auto queryTxt =  QString("SELECT %1, %2 FROM %3 WHERE %4 = '' ").arg(KEYMAP[KEY::ALBUM],
333 334 335
            KEYMAP[KEY::ARTIST],
            TABLEMAP[TABLE::ALBUMS],
            KEYMAP[KEY::WIKI]);
336 337 338


    //    this->setInfo(this->db->getDBData(queryTxt), ontology, services, PULPO::INFO::WIKI, PULPO::RECURSIVE::OFF, nullptr);
339 340
}

341
void Brain::artistArtworks()
342 343 344 345
{
    auto services = {PULPO::SERVICES::LastFm, PULPO::SERVICES::Spotify, PULPO::SERVICES::MusicBrainz, PULPO::SERVICES::Genius};
    auto ontology = PULPO::ONTOLOGY::ARTIST;

Camilo higuita's avatar
Camilo higuita committed
346
    qDebug() << ("Getting missing artists artworks");
347 348 349
    auto queryTxt = QString("SELECT %1 FROM %2 WHERE %3 = ''").arg(KEYMAP[KEY::ARTIST],
            TABLEMAP[TABLE::ARTISTS],
            KEYMAP[KEY::ARTWORK]);
350
    auto artworks = this->db->getDBData(queryTxt);
351

352

353
    /* BEFORE FETCHING ONLINE LOOK UP IN THE CACHE FOR THE IMAGE */
354 355 356
    //    for(auto artist : artworks)
    //        if(BAE::artworkCache(artist, KEY::ARTIST))
    //            this->insertArtwork(artist);
357

358
    artworks = this->db->getDBData(queryTxt);
359
    //    this->setInfo(artworks, ontology, services, PULPO::INFO::ARTWORK, PULPO::RECURSIVE::OFF, nullptr);
360

361
    emit this->done(TABLE::ARTISTS);
362 363 364 365 366 367
}

void Brain::artistTags()
{
    auto services = {PULPO::SERVICES::LastFm, PULPO::SERVICES::Spotify, PULPO::SERVICES::MusicBrainz, PULPO::SERVICES::Genius};
    auto ontology = PULPO::ONTOLOGY::ARTIST;
368

369
    //select artist from artists where  artist  not in (select album from albums_tags)
Camilo higuita's avatar
Camilo higuita committed
370
    qDebug() << ("Getting missing artists tags");
371
    auto queryTxt =  QString("SELECT %1 FROM %2 WHERE %1 NOT IN ( SELECT %1 FROM %3 ) ").arg(KEYMAP[KEY::ARTIST],
372 373
            TABLEMAP[TABLE::ARTISTS],
            TABLEMAP[TABLE::ARTISTS_TAGS]);
374
    //    this->setInfo(this->db->getDBData(queryTxt), ontology, services, PULPO::INFO::TAGS, PULPO::RECURSIVE::ON, nullptr);
375

376 377 378 379 380 381
}

void Brain::artistWikis()
{
    auto services = {PULPO::SERVICES::LastFm, PULPO::SERVICES::Spotify, PULPO::SERVICES::MusicBrainz, PULPO::SERVICES::Genius};
    auto ontology = PULPO::ONTOLOGY::ARTIST;
382

Camilo higuita's avatar
Camilo higuita committed
383
    qDebug() << ("Getting missing artists wikis");
384
    auto queryTxt =  QString("SELECT %1 FROM %2 WHERE %3 = '' ").arg(KEYMAP[KEY::ARTIST],
385 386
            TABLEMAP[TABLE::ARTISTS],
            KEYMAP[KEY::WIKI]);
387
    //    this->setInfo(this->db->getDBData(queryTxt), ontology, services, PULPO::INFO::WIKI, PULPO::RECURSIVE::OFF, nullptr);
388 389 390 391 392 393 394
}

void Brain::trackArtworks()
{
    auto ontology = PULPO::ONTOLOGY::TRACK;
    auto services = {PULPO::SERVICES::LastFm, PULPO::SERVICES::Spotify, PULPO::SERVICES::MusicBrainz, PULPO::SERVICES::Genius};

Camilo higuita's avatar
Camilo higuita committed
395
    qDebug() << ("Getting missing track artwork");
396 397 398 399 400 401 402 403 404
    //select url, title, album, artist from tracks t inner join albums a on a.album=t.album and a.artist=t.artist where a.artwork = ''
    auto queryTxt =  QString("SELECT DISTINCT t.%1, t.%2, t.%3, t.%4 FROM %5 t INNER JOIN %6 a ON a.%3 = t.%3 AND a.%4 = t.%4  WHERE a.%7 = '' GROUP BY a.%3, a.%4 ").arg(KEYMAP[KEY::URL],
            KEYMAP[KEY::TITLE],
            KEYMAP[KEY::ARTIST],
            KEYMAP[KEY::ALBUM],
            TABLEMAP[TABLE::TRACKS],
            TABLEMAP[TABLE::ALBUMS],
            KEYMAP[KEY::ARTWORK]);

405
    auto artworks = this->db->getDBData(queryTxt);
406
    //    this->setInfo(artworks, ontology, services, PULPO::INFO::ARTWORK, PULPO::RECURSIVE::OFF);
407 408 409 410 411 412 413 414 415 416

    emit this->done(TABLE::ALBUMS);

}

void Brain::trackLyrics()
{
    auto ontology = PULPO::ONTOLOGY::TRACK;
    auto services = {PULPO::SERVICES::LyricWikia, PULPO::SERVICES::Genius};

Camilo higuita's avatar
Camilo higuita committed
417
    qDebug() << ("Getting missing track lyrics");
418 419 420 421 422 423
    auto queryTxt = QString("SELECT %1, %2, %3 FROM %4 WHERE %5 = ''").arg(KEYMAP[KEY::URL],
            KEYMAP[KEY::TITLE],
            KEYMAP[KEY::ARTIST],
            TABLEMAP[TABLE::TRACKS],
            KEYMAP[KEY::LYRICS]);

424
    //    this->setInfo(this->db->getDBData(queryTxt), ontology, services, PULPO::INFO::LYRICS, PULPO::RECURSIVE::OFF);
425 426 427

}

428 429 430 431 432
void Brain::trackTags()
{
    auto ontology = PULPO::ONTOLOGY::TRACK;
    auto services = {PULPO::SERVICES::LastFm, PULPO::SERVICES::Spotify, PULPO::SERVICES::MusicBrainz, PULPO::SERVICES::Genius};

Camilo higuita's avatar
Camilo higuita committed
433
    qDebug() << ("Getting missing track tags");
434 435 436 437 438 439 440
    // select title, artist, album from tracks t where url not in (select url from tracks_tags)
    auto queryTxt = QString("SELECT %1, %2, %3, %4 FROM %5 WHERE %1 NOT IN ( SELECT %1 FROM %6 )").arg(KEYMAP[KEY::URL],
            KEYMAP[KEY::TITLE],
            KEYMAP[KEY::ARTIST],
            KEYMAP[KEY::ALBUM],
            TABLEMAP[TABLE::TRACKS],
            TABLEMAP[TABLE::TRACKS_TAGS]);
441
    //    this->setInfo(this->db->getDBData(queryTxt), ontology, services, PULPO::INFO::TAGS, RECURSIVE::ON, nullptr);
442 443 444 445 446 447 448
}

void Brain::trackWikis()
{
    auto ontology = PULPO::ONTOLOGY::TRACK;
    auto services = {PULPO::SERVICES::LastFm, PULPO::SERVICES::Spotify, PULPO::SERVICES::MusicBrainz, PULPO::SERVICES::Genius};

Camilo higuita's avatar
Camilo higuita committed
449
    qDebug() << ("Getting missing track wikis");
450 451 452 453 454 455
    auto queryTxt = QString("SELECT %1, %2, %3, %4 FROM %5 WHERE %6 = ''").arg(KEYMAP[KEY::URL],
            KEYMAP[KEY::TITLE],
            KEYMAP[KEY::ARTIST],
            KEYMAP[KEY::ALBUM],
            TABLEMAP[TABLE::TRACKS],
            KEYMAP[KEY::WIKI]);
456
    //    this->setInfo(this->db->getDBData(queryTxt), ontology, services, PULPO::INFO::WIKI, RECURSIVE::OFF);
457 458

}