Play streamsong using python and raspberry pi

Hi my name is jaime ans now I am designing a application that can playing stream music using mopidy and spotify. This application must be executed in my Raspberry pi. My idea is to create a script that can get acces (login and pasword) and playing song.

My Question is how can I do this with python?. I compiled this code:

import spotify

username = '*******'
password = '*******'

print spotify.__version__ # is 1.11


def logged_in(session, error_type):
    if error_type is spotify.ErrorType.OK:
        print('Logged in as %s' % session.user)
    else:
        print('Login failed: %s' % error_type)

session = spotify.Session()
session.on(spotify.SessionEvent.LOGGED_IN, logged_in)
session.login(username, password)

and this is the error:

Traceback (most recent call last):
File “/home/jarain78/Dropbox/PythonWorkSpace/MopidyExample/init.py”, line 19, in
session.on(spotify.SessionEvent.LOGGED_IN, logged_in)
AttributeError: ‘spotify.Session’ object has no attribute ‘on’

Thanks for the help

What you have written there looks like pyspotify v2.x code but your version
of pyspotify is v1.x.

Mopidy itself is a music server and already provides a way to stream music
from Spotify through the Mopidy-Spotify extension. Are you sure you can’t
just use this rather than write your own code? I’m not trying to put you
off if you want to do so, just save you some time.

Hi thanks for your answer, you told me that when I have a connection between mopidy and spotify is posible get access to search any song or get acces to my playlist. I need this, but using python script, now my mopidy and my spotify is connect, but only can play music using web server and I don’t want it. I need that when my mopidy and my spotify are connected execute my script in python and search e.g Metallica and play Fuel.

Thanks

You can also control Mopidy through the MPD protocol. You could use python-mpd or python-mpd2. http://pythonhosted.org/python-mpd2/topics/getting-started.html

1 Like

Hi kingosticks thanks for your answer, this is what I was looking for :smile: .

Thanks

Jarain78