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

Any known pylast script that works with pianobar #438

Open
bonelifer opened this issue Oct 15, 2023 · 2 comments
Open

Any known pylast script that works with pianobar #438

bonelifer opened this issue Oct 15, 2023 · 2 comments
Labels
question Further information is requested

Comments

@bonelifer
Copy link

Looking for way to scrobble pianobar(pandora). Wonder if anyone is doing this and can share code,

@hugovk hugovk added the question Further information is requested label Oct 15, 2023
@bonelifer
Copy link
Author

bonelifer commented Oct 15, 2023

Managed to find this: https://gist.github.com/dlo/832632 Got Chatai to remove Growl and replace with Pushover/notify-send. That works, but I guess I overloaded Last.FM api maybe? Working code first then error message:

#!/usr/bin/python3

"""
Last.fm scrobbling for Pianobar, the command-line Pandora client.

Copyright (c) 2011
Jon Pierce <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Dependencies:
1) https://github.com/PromyLOPh/pianobar/
2) http://python.org/
3) http://code.google.com/p/pylast/
4) http://www.last.fm/api/account
5) Pushover API credentials (user key and app token)

Installation:
1) Copy this script and pylast.py to the Pianobar config directory, ~/.config/pianobar/, and make sure this script is executable
2) Supply your own Last.fm credentials and Pushover API credentials below
3) Update Pianobar's config file to use this script as its event_command
"""

import sys
import time
import http.client
import urllib.parse
import pylast
import subprocess  # For using notify-send

API_KEY = 'xxxx'
API_SECRET = 'xxxx'
USERNAME = 'xxxx'
PASSWORD = 'xxxx'
THRESHOLD = 50  # the percentage of the song that must have been played to scrobble
ENABLE_PUSHOVER_NOTIFICATIONS = False  # Set this to True to enable Pushover notifications, False to disable
ENABLE_NOTIFY_SEND_NOTIFICATIONS = True  # Set this to True to enable notify-send notifications, False to disable

# I just found a random Last.fm icon off Google.
image = 'YOUR_ICON_URL'  # Replace with your icon URL

def send_pushover_notification(title, message):
    # Pushover API endpoint
    pushover_url = "/1/messages.json"

    # Establish HTTPS connection to Pushover
    conn = http.client.HTTPSConnection("api.pushover.net", 443)

    # Send the message to Pushover
    conn.request("POST", pushover_url,
                 urllib.parse.urlencode({
                     "token": "xxxx",
                     "user": "xxxx",
                     "url": "https://www.last.fm/user/bonelifer",
                     "url_title": "LastFM Profile",
                     "title": title,
                     "message": message
                 }), {"Content-type": "application/x-www-form-urlencoded"})
    conn.getresponse()

    print("Message successfully sent to Pushover.")

def send_notify_send_notification(title, message):
    subprocess.run(['notify-send', title, message])

def main():
    event = sys.argv[1]
    lines = sys.stdin.readlines()
    fields = dict([line.strip().split("=", 1) for line in lines])

    artist = fields["artist"]
    title = fields["title"]
    song_duration = int(fields["songDuration"])
    song_played = int(fields["songPlayed"])
    network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET, username=USERNAME, password_hash=pylast.md5(PASSWORD))
    track = pylast.Track(artist, title, network)

    if event == "songfinish" and 100.0 * song_played / song_duration > THRESHOLD:
        song_started = int(time.time() - song_played / 1000.0)
        network.scrobble(artist=artist, title=title, timestamp=song_started)

        if ENABLE_PUSHOVER_NOTIFICATIONS:
            send_pushover_notification("Pianobar", f'Song finished: {artist} - {title}')
        if ENABLE_NOTIFY_SEND_NOTIFICATIONS:
            send_notify_send_notification(f'Song finished: {artist} - {title}', 'Scrobbled to Last.fm')
    elif event == "songbookmark":
        track.love()

        if ENABLE_PUSHOVER_NOTIFICATIONS:
            send_pushover_notification("Pianobar", f'Song Loved: {artist} - {title}')
        if ENABLE_NOTIFY_SEND_NOTIFICATIONS:
            send_notify_send_notification(f'Song Loved: {artist} - {title}', 'Loved on Last.fm')
    elif event == "songban":
        track.ban()

        if ENABLE_PUSHOVER_NOTIFICATIONS:
            send_pushover_notification("Pianobar", f'Song Banned: {artist} - {title}')
        if ENABLE_NOTIFY_SEND_NOTIFICATIONS:
            send_notify_send_notification(f'Song Banned: {artist} - {title}', 'Banned on Last.fm')

if __name__ == "__main__":
    main()
Welcome to pianobar (2020.11.28)! Press ? for a list of commands.
(i) Login... Ok.
Traceback (most recent call last):
  File "/home/william/.config/pianobar/scrobble.py", line 118, in <module>
    main()
  File "/home/william/.config/pianobar/scrobble.py", line 91, in main
    network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET, username=USERNAME, password_hash=pylast.md5(PASSWORD))
  File "/home/william/.local/lib/python3.10/site-packages/pylast/__init__.py", line 670, in __init__
    super().__init__(
  File "/home/william/.local/lib/python3.10/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.10/site-packages/pylast/__init__.py", line 1072, in get_session_key
    doc = request.execute()
  File "/home/william/.local/lib/python3.10/site-packages/pylast/__init__.py", line 947, in execute
    response = self._download_response()
  File "/home/william/.local/lib/python3.10/site-packages/pylast/__init__.py", line 936, in _download_response
    self._check_response_for_errors(response_text)
  File "/home/william/.local/lib/python3.10/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
(i) Get stations... Ok.
Traceback (most recent call last):
  File "/home/william/.config/pianobar/scrobble.py", line 118, in <module>
    main()
  File "/home/william/.config/pianobar/scrobble.py", line 91, in main
    network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET, username=USERNAME, password_hash=pylast.md5(PASSWORD))
  File "/home/william/.local/lib/python3.10/site-packages/pylast/__init__.py", line 670, in __init__
    super().__init__(
  File "/home/william/.local/lib/python3.10/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.10/site-packages/pylast/__init__.py", line 1072, in get_session_key
    doc = request.execute()
  File "/home/william/.local/lib/python3.10/site-packages/pylast/__init__.py", line 947, in execute
    response = self._download_response()
  File "/home/william/.local/lib/python3.10/site-packages/pylast/__init__.py", line 936, in _download_response
    self._check_response_for_errors(response_text)
  File "/home/william/.local/lib/python3.10/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
|>  Station "The Beatles Radio" (819603184678337385)
(i) Receiving new playlist... Ok.
Traceback (most recent call last):
  File "/home/william/.config/pianobar/scrobble.py", line 118, in <module>
    main()
  File "/home/william/.config/pianobar/scrobble.py", line 91, in main
    network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET, username=USERNAME, password_hash=pylast.md5(PASSWORD))
  File "/home/william/.local/lib/python3.10/site-packages/pylast/__init__.py", line 670, in __init__
    super().__init__(
  File "/home/william/.local/lib/python3.10/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.10/site-packages/pylast/__init__.py", line 1072, in get_session_key
    doc = request.execute()
  File "/home/william/.local/lib/python3.10/site-packages/pylast/__init__.py", line 947, in execute
    response = self._download_response()
  File "/home/william/.local/lib/python3.10/site-packages/pylast/__init__.py", line 936, in _download_response
    self._check_response_for_errors(response_text)
  File "/home/william/.local/lib/python3.10/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
|>  "You Decorated My Life" by "Kenny Rogers" on "Love Songs" <3
Traceback (most recent call last):
  File "/home/william/.config/pianobar/scrobble.py", line 118, in <module>
    main()
  File "/home/william/.config/pianobar/scrobble.py", line 91, in main
    network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET, username=USERNAME, password_hash=pylast.md5(PASSWORD))
  File "/home/william/.local/lib/python3.10/site-packages/pylast/__init__.py", line 670, in __init__
    super().__init__(
  File "/home/william/.local/lib/python3.10/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.10/site-packages/pylast/__init__.py", line 1072, in get_session_key
    doc = request.execute()
  File "/home/william/.local/lib/python3.10/site-packages/pylast/__init__.py", line 947, in execute
    response = self._download_response()
  File "/home/william/.local/lib/python3.10/site-packages/pylast/__init__.py", line 936, in _download_response
    self._check_response_for_errors(response_text)
  File "/home/william/.local/lib/python3.10/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
Traceback (most recent call last):
  File "/home/william/.config/pianobar/scrobble.py", line 118, in <module>
    main()
  File "/home/william/.config/pianobar/scrobble.py", line 91, in main
    network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET, username=USERNAME, password_hash=pylast.md5(PASSWORD))
  File "/home/william/.local/lib/python3.10/site-packages/pylast/__init__.py", line 670, in __init__
    super().__init__(
  File "/home/william/.local/lib/python3.10/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.10/site-packages/pylast/__init__.py", line 1072, in get_session_key
    doc = request.execute()
  File "/home/william/.local/lib/python3.10/site-packages/pylast/__init__.py", line 947, in execute
    response = self._download_response()
  File "/home/william/.local/lib/python3.10/site-packages/pylast/__init__.py", line 936, in _download_response
    self._check_response_for_errors(response_text)
  File "/home/william/.local/lib/python3.10/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
|>  "Texas Bound and Flyin'" by "Jerry Reed" on "The Essential Jerry Reed" <3
Traceback (most recent call last):
  File "/home/william/.config/pianobar/scrobble.py", line 118, in <module>
    main()
  File "/home/william/.config/pianobar/scrobble.py", line 91, in main
    network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET, username=USERNAME, password_hash=pylast.md5(PASSWORD))
  File "/home/william/.local/lib/python3.10/site-packages/pylast/__init__.py", line 670, in __init__
    super().__init__(
  File "/home/william/.local/lib/python3.10/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.10/site-packages/pylast/__init__.py", line 1072, in get_session_key
    doc = request.execute()
  File "/home/william/.local/lib/python3.10/site-packages/pylast/__init__.py", line 947, in execute
    response = self._download_response()
  File "/home/william/.local/lib/python3.10/site-packages/pylast/__init__.py", line 936, in _download_response
    self._check_response_for_errors(response_text)
  File "/home/william/.local/lib/python3.10/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

@bonelifer
Copy link
Author

Just trying to understand, so it doesn't happen again the future.

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

No branches or pull requests

2 participants