Example #1
0
def initDbCollections(fromApi='Musixmatch'):
    '''
    Initialize MongoDb collection with genres and tracks foreach genre

    :param fromApi: choose the APIs from which to retrieve the genres (available: MusixMatch)
    :return:
    '''
    if (fromApi == 'Musixmatch'):
        client = Musixmatch(__musixmatchAPIKey)
        # Genres
        if DbManager.getCollectionGenre().count() == 0:
            print("Init data genres...\n")
            count_add = 0
            list_genres = client.genres_get(
            )["message"]["body"]["music_genre_list"]
            for g in list_genres:
                count_add += 1
                genre = g["music_genre"]
                genre["localName"] = DbManager.fromServerToLocalGenreName(
                    genre["music_genre_vanity"])
                DbManager.getCollectionGenre().insert_one(genre)
            print("√ Added {0} genre/s".format(count_add))
        else:
            print("- No genres added, {0} on database".format(
                DbManager.getCollectionGenre().count()))

        # Music
        if DbManager.getCollectionMusic().count() == 0:
            print("Init data music...")
            count_add = 0
            if len(__map_genres_id.items()) == 0:
                print("You have to a hashmap {genre:id,..}")
            for keyGen, valGen in __map_genres_id.items():
                count_item = 0
                for page_count in range(1, 3):
                    '''
                    list_tracks = client.chart_tracks_get(f_has_lyrics=False, page=page_count, page_size=100, country=country)["message"]["body"][
                        "track_list"]
                    '''
                    list_tracks = client.track_by_genre_id(
                        page_size=100,
                        page=page_count,
                        f_music_genre_id=valGen
                    )["message"]["body"]["track_list"]

                    for t in list_tracks:
                        current = t["track"]

                        primary_genres = list()
                        exist = {}
                        exist_genre_locally = False
                        for pg in current["primary_genres"][
                                "music_genre_list"]:
                            music_genre = pg["music_genre"]
                            music_genre_local = DbManager.fromServerToLocalGenreName(
                                music_genre["music_genre_vanity"])
                            if DbManager.fromServerToLocalGenreName(music_genre["music_genre_vanity"]) \
                                    and (music_genre_local not in exist):
                                music_genre['localName'] = music_genre_local
                                primary_genres.append(music_genre)
                                exist[music_genre_local] = True
                                exist_genre_locally = True

                        # Add track to mongoDb only if exist
                        if exist_genre_locally:
                            count_add += 1
                            count_item += 1
                            DbManager.getCollectionMusic().insert_one({
                                "artist_name":
                                current["artist_name"],
                                "track_name":
                                current["track_name"],
                                "primary_genres":
                                primary_genres,
                                "instrumental":
                                current["instrumental"],
                                "track_length":
                                current["track_length"]
                                if "track_length" in current else 0
                            })
                print("√ Added {0} track/s for genre {1}".format(
                    count_item, keyGen))

            if count_add > 0:
                print("√ Added {0} track/s".format(count_add))
        else:
            print("- No music added, {0} on database".format(
                DbManager.getCollectionMusic().count()))

    else:
        print("This API is not available\n")
        return 0