def import_artists():
    """Import artist information from API V2."""
    limit = 200
    offset = 0
    total = 0
    processing = True

    print("Importing artists from V2")

    while processing is True:
        uri = "https://numutracker.com/v2/json.php?all_artists"
        + "&key={}&limit={}&offset={}".format(
            numu_app.config.get('NUMU_V2_API_KEY'), limit, offset)
        data = grab_json(uri)
        if not data:
            processing = False

        for artist in data:
            found_artist = Artist.query.filter_by(mbid=artist['mbid']).first()
            if found_artist is None:
                print("ADDED: {} - {}".format(artist['name'], artist['mbid']))
                new_artist = Artist(mbid=artist['mbid'],
                                    name=artist['name'],
                                    sort_name=artist['sort_name'],
                                    disambiguation=artist['disambiguation'])
                db.session.add(new_artist)
                db.session.commit()

        offset += limit
        total += limit

        print("Processed {} Next offset: {}".format(total, offset))

    print("Importing artist AKA from V2")

    data = grab_json(
        "https://numutracker.com/v2/json.php?all_artist_aka&key={}".format(
            numu_app.config.get('NUMU_V2_API_KEY')))

    for aka in data:
        found_artist = Artist.query.filter_by(mbid=aka['mbid']).first()
        if found_artist is not None:
            found_aka = ArtistAka.query.filter_by(
                artist_mbid=aka['mbid'], name=aka['aka_name']).first()
            if found_aka is None:
                print("ADDED AKA: {} - AKA: {}".format(found_artist.name,
                                                       aka['aka_name']))
                new_aka = ArtistAka(artist_mbid=found_artist.mbid,
                                    name=aka['aka_name'])
                db.session.add(new_aka)

    db.session.commit()
def import_from_lastfm(user, username, limit=500, period='overall', page=1):
    """Download artists from a LastFM account into the user's library.

    Period options: overall | 7day | 1month | 3month | 6month | 12month"""

    data = grab_json(
        "http://ws.audioscrobbler.com/2.0/?method=user.gettopartists" +
        "&user={}&limit={}&api_key={}&period={}&page={}&format=json".format(
            username, limit, numu_app.config.get('LAST_FM_API_KEY'), period,
            page))

    artists_added = 0

    for artist in data.get('topartists').get('artist'):
        found_import = ArtistImport.query.filter_by(
            user_id=user.id, import_name=artist['name']).first()
        if found_import is None:
            new_import = ArtistImport(user_id=user.id,
                                      import_name=artist['name'],
                                      import_mbid=artist['mbid'],
                                      import_method='lastfm')
            db.session.add(new_import)
            artists_added += 1

    if artists_added > 0:
        db.session.commit()

    return artists_added
Beispiel #3
0
def get_release_art(release):
    data = grab_json(
        "http://ws.audioscrobbler.com/2.0/?method=album.getinfo"
        + "&artist={}&album={}&api_key={}&format=json".format(
            quote_plus(release.artist_names),
            quote_plus(release.title),
            numu_app.config.get("LAST_FM_API_KEY"),
        )
    )

    images = data.get("album", {}).get("image")
    if images:
        thumb_url = images[1]["#text"]
        full_url = images[2]["#text"]
        large_url = images[3]["#text"]

        if thumb_url and full_url and large_url:
            image_name = release.mbid + ".png"
            try:
                put_image_from_url(thumb_url, "release/thumb/" + image_name)
                put_image_from_url(full_url, "release/full/" + image_name)
                put_image_from_url(large_url, "release/large/" + image_name)
                return True
            except Exception as e:
                numu_app.logger.error("Put release image failed: {}".format(e))
                return False
    numu_app.logger.info("No release art found.")
    return False
    def import_from_lastfm(self,
                           user_id,
                           lastfm_username,
                           limit=500,
                           period="overall",
                           page=1):
        """Download artists from a LastFM account into the user's library.

        Period options: overall | 7day | 1month | 3month | 6month | 12month"""

        data = utils.grab_json(
            "http://ws.audioscrobbler.com/2.0/?method=user.gettopartists" +
            "&user={}&limit={}&api_key={}&period={}&page={}&format=json".
            format(
                lastfm_username,
                limit,
                numu_app.config.get("LAST_FM_API_KEY"),
                period,
                page,
            ))
        artists_list = []
        for artist in data.get("topartists", {}).get("artist"):
            artists_list.append({
                "name": artist["name"],
                "mbid": artist["mbid"]
            })

        return self.save_imports(user_id, artists_list, import_method="lastfm")
def run_command():
    limit = 100
    users = (User.query.order_by(
        User.date_v2_import.asc().nullsfirst()).limit(limit).all())
    for user in users:
        username = user.email if user.email else user.icloud

        data = utils.grab_json(
            "https://www.numutracker.com/v2/json2.php?importv2={}&key={}".
            format(username, numu_app.config.get("NUMU_V2_API_KEY")))

        filters = data.get("filters")
        if filters:
            user.album = bool(filters["album"])
            user.ep = bool(filters["ep"])
            user.single = bool(filters["single"])
            user.live = bool(filters["live"])
            user.soundtrack = bool(filters["soundtrack"])
            user.remix = bool(filters["remix"])
            user.other = bool(filters["other"])
            repo.save(user)

        artists = data.get("artists")
        if artists:
            imported = import_processor.save_imports(user.id, artists, "v2")
            print("Imported {} artists from V2...".format(imported))

        listens = data.get("listens", [])
        releases_added = 0
        for listen in listens:
            release_mbid = listen.get("mbid")
            release = repo.get_release_by_mbid(release_mbid)

            if release:
                user_release, notify = release_processor.add_user_release(
                    release, user_id=user.id, follow_method="v2")
                user_release.listened = True
                user_release.date_listened = listen.get("listen_date")
                repo.save(user_release)
                if notify:
                    releases_added += 1
        print("Listens imported: {}".format(releases_added))

        user.date_v2_import = utils.now()
        repo.save(user)
        repo.commit()
Beispiel #6
0
def get_artist_art(artist):
    data = grab_json("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo"
                     + "&artist={}&api_key={}&format=json".format(
                        quote_plus(artist.name),
                        numu_app.config.get('LAST_FM_API_KEY')))

    images = data.get('artist', {}).get('image')
    if images:
        thumb_url = images[1]['#text']
        full_url = images[2]['#text']
        large_url = images[3]['#text']

        if thumb_url and full_url and large_url:
            image_name = artist.mbid + ".png"
            try:
                put_image_from_url(thumb_url, "artist/thumb/" + image_name)
                put_image_from_url(full_url, "artist/full/" + image_name)
                put_image_from_url(large_url, "artist/large/" + image_name)
                return True
            except Exception as e:
                numu_app.logger.error("Put artist image failed: {}".format(e))
                return False
    numu_app.logger.error("No artist art found.")
    return False
def import_numu_v2():
    """
    Import data from Numu API v2
    Imports:
    - artists
    - listening history
    - filters
    """
    user = g.user
    repo = Repo()
    import_processor = ImportProcessor()
    release_processor = ReleaseProcessor()
    username = user.email if user.email else user.icloud

    result = {}

    data = grab_json(
        "https://www.numutracker.com/v2/json2.php?importv2={}&key={}".format(
            username, numu_app.config.get("NUMU_V2_API_KEY")
        )
    )

    result["raw_data"] = data

    filters = data.get("filters")
    if filters:
        user.album = bool(filters["album"])
        user.ep = bool(filters["ep"])
        user.single = bool(filters["single"])
        user.live = bool(filters["live"])
        user.soundtrack = bool(filters["soundtrack"])
        user.remix = bool(filters["remix"])
        user.other = bool(filters["other"])
        repo.save(user)
        repo.commit()

    artists = data.get("artists")
    if artists:
        imported = import_processor.save_imports(user.id, artists, "v2")
        result["artists_imported"] = imported

    listens = data.get("listens")
    if listens:
        releases_added = 0
        for listen in listens:
            release_mbid = listen.get("mbid")
            release = release_processor.add_release(release_mbid)

            if release:
                user_release, notify = release_processor.add_user_release(
                    release, user_id=user.id
                )
                user_release.listened = True
                user_release.date_listened = listen.get("listen_date")
                repo.save(user_release)
                if notify:
                    releases_added += 1
        repo.commit()

        result["releases_added"] = releases_added

    return response.success(result)
Beispiel #8
0
def import_numu_v2():
    """
    Import data from Numu API v2
    Imports:
    - artists
    - listening history
    - filters
    """
    user = g.user
    username = user.email if user.email else user.icloud

    result = {}

    data = grab_json(
        "https://www.numutracker.com/v2/json2.php?importv2={}".format(
            username))

    result['raw_data'] = data

    filters = data.get('filters')
    if filters:
        user.album = bool(filters['album'])
        user.ep = bool(filters['ep'])
        user.single = bool(filters['single'])
        user.live = bool(filters['live'])
        user.soundtrack = bool(filters['soundtrack'])
        user.remix = bool(filters['remix'])
        user.other = bool(filters['other'])
        db.session.add(user)
        db.session.commit()

    artists = data.get('artists')
    if artists:
        imported = import_processing.import_artists_v2(user, artists)
        if imported > 0:
            result['artists_imported'] = imported

    listens = data.get('listens')
    if listens:
        releases_added = 0
        for listen in listens:
            release_mbid = listen.get('mbid')
            # Check for release in Numu
            release = repo.get_numu_release(release_mbid)

            # If releases doesn't exist, find in MB
            if release is None:
                mb_release = musicbrainz.get_release(release_mbid)
                if mb_release and mb_release.get('status') == 200:
                    release = data_processing.create_or_update_numu_release(
                        mb_release['release'])

            if release:
                # Add user release
                user_release, notify = data_processing.create_or_update_user_release(
                    user.id, release, 'v2')

                # Update listen status
                user_release.listened = True
                user_release.date_listened = listen.get('listen_date')
                db.session.add(user_release)
                db.session.commit()
                releases_added += 1
        result['releases_listened'] = releases_added

    return response.success(result)