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

Can't Love track using example code #407

Closed
bonelifer opened this issue Jun 22, 2022 · 4 comments
Closed

Can't Love track using example code #407

bonelifer opened this issue Jun 22, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@bonelifer
Copy link

What did you do?

Tried using the example from the readme, to love a trrack.

What did you expect to happen?

Love specified track

What actually happened?

Some sort of authentication error.

What versions are you using?

  • OS: Xubuntu 20.04.4
  • Python: 3.8
  • pylast: most recent from Ubuntu package manager/pip3

Please include code that reproduces the issue.
Code in the Github readme

Traceback (most recent call last):
  File "last.py", line 11, in <module>
    network = pylast.LastFMNetwork(
  File "/home/william/.local/lib/python3.8/site-packages/pylast/__init__.py", line 670, in __init__
    super().__init__(
  File "/home/william/.local/lib/python3.8/site-packages/pylast/__init__.py", line 216, in __init__
    self.session_key = sk_gen.get_session_key(self.username, self.password_hash)
  File "/home/william/.local/lib/python3.8/site-packages/pylast/__init__.py", line 1072, in get_session_key
    doc = request.execute()
  File "/home/william/.local/lib/python3.8/site-packages/pylast/__init__.py", line 947, in execute
    response = self._download_response()
  File "/home/william/.local/lib/python3.8/site-packages/pylast/__init__.py", line 936, in _download_response
    self._check_response_for_errors(response_text)
  File "/home/william/.local/lib/python3.8/site-packages/pylast/__init__.py", line 965, in _check_response_for_errors
    raise WSError(self.network, status, details)
pylast.WSError: Authentication Failed - You do not have permissions to access the service

@hugovk hugovk added the bug Something isn't working label Jun 23, 2022
@hugovk
Copy link
Member

hugovk commented Jun 23, 2022

It's failing to authenticate before even calling track.love, when creating the network object:

network = pylast.LastFMNetwork(
    api_key=API_KEY,
    api_secret=API_SECRET,
    username=username,
    password_hash=password_hash,
)

I can reproduce.

Maybe Last.fm have removed that authentication method?

Instead of that line, can you try this lot instead?

SESSION_KEY_FILE = os.path.join(os.path.expanduser("~"), ".session_key")
network = pylast.LastFMNetwork(API_KEY, API_SECRET)
if not os.path.exists(SESSION_KEY_FILE):
    skg = pylast.SessionKeyGenerator(network)
    url = skg.get_web_auth_url()

    print(f"Please authorize this script to access your account: {url}\n")
    import time
    import webbrowser

    webbrowser.open(url)

    while True:
        try:
            session_key = skg.get_web_auth_session_key(url)
            with open(SESSION_KEY_FILE, "w") as f:
                f.write(session_key)
            break
        except pylast.WSError:
            time.sleep(1)
else:
    session_key = open(SESSION_KEY_FILE).read()

network.session_key = session_key

Remember to call the track.love stuff afterwards:

# Now you can use that object everywhere
track = network.get_track("Iron Maiden", "The Nomad")
track.love()
track.add_tags(("awesome", "favorite"))

@bonelifer
Copy link
Author

bonelifer commented Jun 23, 2022

Thanks. That worked. Now off to love my scrobbles. Scrobble scrobble. Just wish LastFM would try to keep some consistency in their API longer term.

@hugovk
Copy link
Member

hugovk commented Jun 23, 2022

Good to hear!

Maybe related to #357.

Let's update the README to use this web authentication instead of username and password authentication. It's probably worth adding a helper function to pylast with the above snippet.

@hugovk hugovk changed the title Can't Love track using example coder. Can't Love track using example code Jun 23, 2022
@bonelifer
Copy link
Author

bonelifer commented Jun 23, 2022

Not a programmer, so shooting in the dark, blind as hell. What do I do to make this work? I'm more of a bash type of person. I need to get the answer from those two mpc commands, so that I can pass it to the command below it. I got as far as getting the top portion working, but not sure how to pass Finally got it working, example below.

artist = os.popen("/usr/bin/mpc -f %albumartist%").read()
artist = artist.split('\n')[0]

title = os.popen("/usr/bin/mpc -f %title%").read()
title = title.split('\n')[0]

# Now you can use that object everywhere
track = network.get_track(artist, title)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants