Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AuthenticatedUser.get_name() still returns empty string pylast 3.1.0 #300

Open
Qwerty-Space opened this issue Mar 13, 2019 · 2 comments
Open

Comments

@Qwerty-Space
Copy link

What did you do?

Specifically the contents of main()
I tried to print my username using the AuthenticatedUser class

What did you expect to happen?

I expected it to print my username

What actually happened?

image

It prints an empty string

What versions of OS, Python and pylast are you using?

Manjaro 18.0.4 Illyria

>>> pylast.__version__
'3.1.0'
$ python -V
Python 3.7.2

Code

Python Code:
import time
import pylast
from os import path
from config import api_key, api_secret

script_dir = path.dirname(path.realpath(__file__))  # Set the location of the script
session_file = path.join(script_dir, "python-lastfm.session_key")

network = pylast.LastFMNetwork(api_key=api_key, api_secret=api_secret)

def login():
    skg = pylast.SessionKeyGenerator(network)
    url = skg.get_web_auth_url()

    print(f"Please follow the link to authorise your account:\n\n{url}")

    time.sleep(3)

    while True:
        try:
            session_key = skg.get_web_auth_session_key(url)
            with open(session_file, "w") as f:
                f.write(session_key)
            print("Signed in!")
            break
        except pylast.WSError:
            time.sleep(3)

def main():
    print("Hi")
    u = network.get_authenticated_user()
    print(u, u.get_name())


if __name__ == "__main__":
    if not path.exists(session_file):
        login()
    else:
        session_key = open(session_file).read()
        network.session_key = session_key
        main()
@purpl3F0x
Copy link

purpl3F0x commented Jun 2, 2019

I have the same issue. Also If I try to do this

print(skg.get_web_auth_session_key_username(url))

The programm just waits forever.

@ndm13
Copy link
Contributor

ndm13 commented Jan 1, 2023

Confirming this issue. It seems that AuthenticatedUser doesn't set the username. It also propagates to any user methods that require the username to be set. Workaround: provide a username in the network constructor:

# Works
pylast = LastFMNetwork(getenv('LASTFM_API'), getenv('LASTFM_SECRET'), user.token, user.name)
history = pylast.get_authenticated_user().get_recent_tracks()

# Doesn't work
pylast = LastFMNetwork(getenv('LASTFM_API'), getenv('LASTFM_SECRET'), user.token)
history = pylast.get_authenticated_user().get_recent_tracks()

The broken example results in the following trace:

INFO:pylast:user.getRecentTracks
INFO:pylast:user.getRecentTracks
INFO:pylast:user.getRecentTracks
Traceback (most recent call last):
  File "C:\project-root\venv\lib\site-packages\pylast\__init__.py", line 2769, in _stream_collect_nodes
    doc = sender._request(method_name, cacheable, params)
  File "C:\project-root\venv\lib\site-packages\pylast\__init__.py", line 1112, in _request
    return _Request(self.network, method_name, params).execute(cacheable)
  File "C:\project-root\venv\lib\site-packages\pylast\__init__.py", line 947, in execute
    response = self._download_response()
  File "C:\project-root\venv\lib\site-packages\pylast\__init__.py", line 936, in _download_response
    self._check_response_for_errors(response_text)
  File "C:\project-root\venv\lib\site-packages\pylast\__init__.py", line 965, in _check_response_for_errors
    raise WSError(self.network, status, details)
pylast.WSError: Operation failed - Most likely the backend service failed. Please try again.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\project-root\daemon\__main__.py", line 31, in <module>
    run()
  File "C:\project-root\daemon\__main__.py", line 26, in run
    runner.run_user(user)
  File "C:\project-root\core\runner.py", line 31, in run_user
    lfm_history = pylast.get_authenticated_user().get_recent_tracks(len(history) * 1.5, True, user.last_run)
  File "C:\project-root\venv\lib\site-packages\pylast\__init__.py", line 2359, in get_recent_tracks
    return _get_recent_tracks() if stream else list(_get_recent_tracks())
  File "C:\project-root\venv\lib\site-packages\pylast\__init__.py", line 2343, in _get_recent_tracks
    for track_node in _collect_nodes(
  File "C:\project-root\venv\lib\site-packages\pylast\__init__.py", line 2803, in _collect_nodes
    return _stream_collect_nodes() if stream else list(_stream_collect_nodes())
  File "C:\project-root\venv\lib\site-packages\pylast\__init__.py", line 2773, in _stream_collect_nodes
    raise PyLastError() from e
pylast.PyLastError

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants