Example #1
0
def test_get_scrobbles():
    scrobbles = lastfm.get_scrobbles("MrRikkerts",
                                     limit=10,
                                     time_to="1584285600")
    assert scrobbles is not None
    assert len(scrobbles) == 10
    assert scrobbles[0].track.title == "Dispossession / Piano Ver."
Example #2
0
def test_get_scrobbles_range2():
    scrobbles = lastfm.get_scrobbles("MrRikkerts",
                                     limit=10,
                                     time_to="1584286000",
                                     time_from="1584285529")
    assert scrobbles is not None
    assert len(scrobbles) == 2
    assert scrobbles[-1].track.title == "Dependent Weakling"
Example #3
0
def test_get_scrobbles_range():
    scrobbles = lastfm.get_scrobbles("MrRikkerts",
                                     limit=10,
                                     time_to="1584286000",
                                     time_from="1584285528")
    assert scrobbles is not None
    assert len(scrobbles) == 3
    assert scrobbles[-1].track.title == "Dispossession / Piano Ver."
Example #4
0
def sync_lastfm_scrobbles(username: str):
    """Sync the user's LastFm scrobbles with the database.
    It will get all scrobbles if the user has never synced before.
    If the user has synced before, it will sync everything since the last sync.

    ## Arguments:
    - `username`: `str`:
        - The user's LastFm username

    ## Returns:
    - `int`:
        - The amount of scrobbles synced
    """
    last_scrobble = get_last_scrobble()
    last_date = 0
    if last_scrobble:
        # Make timestamp timezone aware. All timestamps are stored as UTC,
        # but python interprets all datetimes without timezones as local.
        # So when making a timestamp it first converts the datetime to UTC,
        # even though it was alreadu UTC.
        last_date = pytz.utc.localize(last_scrobble.date)
        last_date = int(last_date.timestamp())
    scrobbles = lastfm.get_scrobbles(
        username=username,
        limit=None,
        # +60 seconds to exclude the last scrobble from lastfm
        time_from=last_date + 60 if last_date is not None else None,
    )
    for _scrobble in scrobbles:
        timestamp = _scrobble.timestamp  # + timedelta(hours=1)
        try:
            scrobble(
                scrobble=ScrobbleIn(
                    date=timestamp,
                    artist=_scrobble.track.artist.name.lower(),
                    album=_scrobble.album.lower(),
                    title=_scrobble.track.title.lower(),
                )
            )
        except Exception as e:
            logger.debug(repr(e))
            logger.debug(_scrobble)
    return len(scrobbles)
Example #5
0
def test_get_scrobbles_non_exisiting_user():
    with pytest.raises(PyLastError):
        with pytest.raises(LastFmError):
            lastfm.get_scrobbles(username="******", limit=1)
Example #6
0
def test_get_scrobbles_empty_username():
    with pytest.raises(ValueError):
        lastfm.get_scrobbles(username="", limit=1)
Example #7
0
def test_get_scrobbles_no_username():
    with pytest.raises(TypeError):
        lastfm.get_scrobbles(limit=1)