Example #1
0
def main():
    print "uses SPOTIFY(R) CORE"
    ml = MainLoop()
    cb = JukeboxCallbacks(ml)
    s = session.Session(
        cb,
        app_key=appkey,
        user_agent="python ctypes bindings",
        settings_location="../tmp/settings",
        cache_location="../tmp/cache",
    )
    
    c = JukeboxCmd(s, ml)
    c.start()
    ml.loop(s)
Example #2
0
def main():
    print "uses SPOTIFY(R) CORE"
    ml = MainLoop()
    cb = JukeboxCallbacks(ml)
    s = session.Session(
        cb,
        app_key=appkey,
        user_agent="python ctypes bindings",
        settings_location="../tmp/settings",
        cache_location="../tmp/cache",
    )
    
    c = JukeboxCmd(s, ml)
    c.start()
    ml.loop(s)
Example #3
0
 def set_up_session(self):
     if self.session is None:
         self.main_loop = MainLoop()
         self.buffer_manager = BufferManager()
         callbacks = SpotifyCallbacks(self, self.main_loop, self.buffer_manager, self.authenticator)
         self.session = SessionFactory(callbacks, self.settings).create_session()
         self.set_up_authenticator(self.session)
         self.authenticator.lastfm_scrobbling(self.settings.lastfm_enabled, self.settings.lastfm_username, self.settings.lastfm_password)
def main():
    print "uses SPOTIFY(R) CORE"
    ml = MainLoop()
    buf = audio.BufferManager()
    cb = JukeboxCallbacks(ml, buf)
    s = session.Session(
        cb,
        app_key=appkey,
        user_agent="python ctypes bindings",
        settings_location="../tmp/settings",
        cache_location="../tmp/cache",
    )
    
    pr = httpproxy.ProxyRunner(s, buf)
    c = JukeboxCmd(s, ml)
    c.start()
    pr.start()
    ml.loop(s)
    pr.stop()
Example #5
0
  def start(self):
    Log.Debug('PlaybackService: start()')
    data_dir, cache_dir, settings_dir = check_dirs()
    self.mainloop = MainLoop()
    self.session = Session(
        self,
        app_key=appkey,
        user_agent="python ctypes bindings",
        settings_location=settings_dir,
        cache_location=cache_dir,
        initially_unload_playlists=False
    )
    self.session.preferred_bitrate(Bitrate.Rate320k)
    self.session.set_volume_normalization(True)
    self.session.set_volume_normalization(True)
    self.session.set_connection_type(ConnectionType.Wired)

    Thread.Create(self.StartWorkerThread)

    self.status = PlaybackStatus.Started
Example #6
0
def main():
    try:
        app = Application()
        logout_event = Event()
        connstate_event = Event()
        monitor = xbmc.Monitor()
        app.set_var('logout_event', logout_event)
        app.set_var('login_last_error', ErrorType.Ok)
        app.set_var('connstate_event', connstate_event)
        app.set_var('exit_requested', False)
        app.set_var('monitor', monitor)
        data_dir, cache_dir, settings_dir = check_dirs()

        #Initialize spotify stuff
        ml = MainLoop()
        buf = BufferManager(get_audio_buffer_size())
        callbacks = Callbacks(ml, buf, app)
        sess = Session(callbacks,
                       app_key=appkey,
                       user_agent="python ctypes bindings",
                       settings_location=settings_dir,
                       cache_location=cache_dir,
                       initially_unload_playlists=False)
        set_settings(sess)

        ml_runner = MainLoopRunner(ml, sess)
        ml_runner.start()

        #Set the exit flag if login was cancelled
        if not do_login(sess, app):
            WINDOW.setProperty("Spotify.ServiceReady", "error")
            app.set_var('exit_requested', True)

        elif wait_for_connstate(sess, app, ConnectionState.LoggedIn):

            proxy_runner = ProxyRunner(sess,
                                       buf,
                                       host='127.0.0.1',
                                       allow_ranges=True)
            proxy_runner.start()
            logMsg('starting proxy at port {0}'.format(
                proxy_runner.get_port()))
            preloader_cb = get_preloader_callback(sess, buf)
            logMsg('Setting callback ..')
            proxy_runner.set_stream_end_callback(preloader_cb)

            user_agent = try_decode('Spotify/{0} (XBMC/{1})'.format(
                ADDON_VERSION,
                xbmc.getInfoLabel("System.BuildVersion"))).decode(
                    'utf-8', 'ignore')
            logMsg('Obtaining user token ..')
            playtoken = proxy_runner.get_user_token(user_agent)
            header_dict = {
                'User-Agent': user_agent,
                'X-Spotify-Token': playtoken
            }
            logMsg('Encoding header ..')
            url_headers = urlencode(header_dict)
            WINDOW.setProperty("Spotify.PlayToken", url_headers)
            WINDOW.setProperty(
                "Spotify.PlayServer",
                "%s:%s" % (proxy_runner.get_host(), proxy_runner.get_port()))
            WINDOW.setProperty("Spotify.ServiceReady", "ready")

            #wait untill abortrequested
            while not app.get_var('exit_requested'):
                if monitor.abortRequested() or xbmc.abortRequested:
                    logMsg("Shutdown requested!")
                    app.set_var('exit_requested', True)
                monitor.waitForAbort(0.5)
            logMsg("Shutting down background processing...")

            #Playback and proxy deinit sequence
            xbmc.executebuiltin('PlayerControl(stop)')
            logMsg('Clearing stream / stopping ..')
            proxy_runner.clear_stream_end_callback()
            proxy_runner.stop()
            buf.cleanup()

            #Clear some vars and collect garbage
            proxy_runner = None
            preloader_cb = None

            #Logout
            logMsg('Logging out ..')
            if sess.user():
                sess.logout()
                logout_event.wait(2)

        #Stop main loop
        error = login_get_last_error(app)
        WINDOW.setProperty("Spotify.LastError", str(login_get_last_error(app)))
        ml_runner.stop()

    except (Exception) as ex:
        if str(ex) != '':
            # trace = traceback.format_exc()
            logMsg("TRACE: " + (''.join(traceback.format_stack())))
            logMsg("EXCEPTION in background service: " + str(ex))
            # logMsg("STACK: %s" %trace, True)
            if "Unable to find" in str(ex):
                WINDOW.setProperty("Spotify.LastError", "999")
            else:
                error = str(ex)
                WINDOW.clearProperty("Spotify.ServiceReady")
                WINDOW.setProperty("Spotify.LastError", error)
Example #7
0
def main(addon_dir):
    #Check needed directories first
    data_dir, cache_dir, settings_dir = check_dirs()
    
    #Instantiate the settings obj
    settings_obj = SettingsManager()
    
    #Show legal warning
    show_legal_warning(settings_obj)
    
    #Start checking the version
    check_addon_version(settings_obj)
    
    #Don't set cache folder if it's disabled
    if not settings_obj.get_cache_status():
        cache_dir = ''
    
    #Initialize spotify stuff
    ml = MainLoop()
    buf = BufferManager(get_audio_buffer_size())
    logout_event = Event()
    callbacks = SpotimcCallbacks(ml, buf, logout_event)
    sess = Session(
        callbacks,
        app_key=appkey,
        user_agent="python ctypes bindings",
        settings_location=settings_dir,
        cache_location=cache_dir,
        initially_unload_playlists=False,
    )
    
    #Now that we have a session, set settings
    set_settings(settings_obj, sess)
    
    #Initialize libspotify's main loop handler on a separate thread
    ml_runner = MainLoopRunner(ml, sess)
    ml_runner.start()
   
    #If login was successful start main window
    if do_login(sess, addon_dir, "DefaultSkin"):
        proxy_runner = ProxyRunner(sess, buf, host='127.0.0.1')
        proxy_runner.start()
        
        print 'port: %s' % proxy_runner.get_port()
        
        #Instantiate the playlist manager
        playlist_manager = playback.PlaylistManager(proxy_runner)
        
        #Set the track preloader callback
        preloader_cb = get_preloader_callback(sess, playlist_manager, buf)
        proxy_runner.set_stream_end_callback(preloader_cb)
        
        #Start main window and enter it's main loop
        mainwin = windows.MainWindow("main-window.xml", addon_dir, "DefaultSkin")
        mainwin.initialize(sess, proxy_runner, playlist_manager)
        mainwin.doModal()
        
        #Playback and proxy deinit sequence
        proxy_runner.clear_stream_end_callback()
        player = xbmc.Player()
        player.stop()
        proxy_runner.stop()
        buf.cleanup()
        
        #Clear some vars and collect garbage
        proxy_runner = None
        preloader_cb = None
        playlist_manager = None
        mainwin = None
        gc.collect()
        
        #from _spotify.utils.moduletracker import _tracked_modules
        #print "tracked modules after: %d" % len(_tracked_modules)
        
        #import objgraph
        #objgraph.show_backrefs(_tracked_modules, max_depth=10)
        
        #Logout
        sess.logout()
        logout_event.wait(10)
    
    #Stop main loop
    ml_runner.stop()
Example #8
0
def gui_main(addon_dir):
    #Initialize app var storage
    app = Application()
    logout_event = Event()
    connstate_event = Event()
    info_value_manager = InfoValueManager()
    app.set_var('logout_event', logout_event)
    app.set_var('login_last_error', ErrorType.Ok)
    app.set_var('connstate_event', connstate_event)
    app.set_var('exit_requested', False)
    app.set_var('info_value_manager', info_value_manager)

    #Check needed directories first
    data_dir, cache_dir, settings_dir = check_dirs()

    #Instantiate the settings obj
    settings_obj = SettingsManager()

    #Show legal warning
    show_legal_warning(settings_obj)

    #Start checking the version
    check_addon_version(settings_obj)

    #Don't set cache folder if it's disabled
    if not settings_obj.get_cache_status():
        cache_dir = ''

    #Initialize spotify stuff
    ml = MainLoop()
    buf = BufferManager(get_audio_buffer_size())
    callbacks = SpotimcCallbacks(ml, buf, app)
    sess = Session(
        callbacks,
        app_key=appkey,
        user_agent="python ctypes bindings",
        settings_location=settings_dir,
        cache_location=cache_dir,
        initially_unload_playlists=False,
    )

    #Now that we have a session, set settings
    set_settings(settings_obj, sess)

    #Initialize libspotify's main loop handler on a separate thread
    ml_runner = MainLoopRunner(ml, sess)
    ml_runner.start()

    #Stay on the application until told to do so
    while not app.get_var('exit_requested'):

        #Set the exit flag if login was cancelled
        if not do_login(sess, addon_dir, "DefaultSkin", app):
            app.set_var('exit_requested', True)

        #Otherwise block until state is sane, and continue
        elif wait_for_connstate(sess, app, ConnectionState.LoggedIn):

            proxy_runner = ProxyRunner(sess,
                                       buf,
                                       host='127.0.0.1',
                                       allow_ranges=True)
            proxy_runner.start()
            log_str = 'starting proxy at port {0}'.format(
                proxy_runner.get_port())
            get_logger().info(log_str)

            #Instantiate the playlist manager
            playlist_manager = playback.PlaylistManager(proxy_runner)
            app.set_var('playlist_manager', playlist_manager)

            #Set the track preloader callback
            preloader_cb = get_preloader_callback(sess, playlist_manager, buf)
            proxy_runner.set_stream_end_callback(preloader_cb)

            hide_busy_dialog()
            mainwin = windows.MainWindow("main-window.xml", addon_dir,
                                         "DefaultSkin")
            mainwin.initialize(sess, proxy_runner, playlist_manager, app)
            app.set_var('main_window', mainwin)
            mainwin.doModal()
            show_busy_dialog()

            #Playback and proxy deinit sequence
            proxy_runner.clear_stream_end_callback()
            playlist_manager.stop()
            proxy_runner.stop()
            buf.cleanup()

            #Join all the running tasks
            tm = TaskManager()
            tm.cancel_all()

            #Clear some vars and collect garbage
            proxy_runner = None
            preloader_cb = None
            playlist_manager = None
            mainwin = None
            app.remove_var('main_window')
            app.remove_var('playlist_manager')
            gc.collect()

            #Logout
            if sess.user() is not None:
                sess.logout()
                logout_event.wait(10)

    #Stop main loop
    ml_runner.stop()

    #Some deinitializations
    info_value_manager.deinit()
Example #9
0
class PlaybackService(SessionCallbacks):
  def __init__(self):
    Log.Debug("PlaybackService Initializing")
    self.prev_error = 0
    self.status = PlaybackStatus.Created
    self.stream = None
    self.trackLock = threading.Lock()
    self.session = None

  def start(self):
    Log.Debug('PlaybackService: start()')
    data_dir, cache_dir, settings_dir = check_dirs()
    self.mainloop = MainLoop()
    self.session = Session(
        self,
        app_key=appkey,
        user_agent="python ctypes bindings",
        settings_location=settings_dir,
        cache_location=cache_dir,
        initially_unload_playlists=False
    )
    self.session.preferred_bitrate(Bitrate.Rate320k)
    self.session.set_volume_normalization(True)
    self.session.set_volume_normalization(True)
    self.session.set_connection_type(ConnectionType.Wired)

    Thread.Create(self.StartWorkerThread)

    self.status = PlaybackStatus.Started

  def stop(self):
    Log.Debug('PlaybackService: stop()')
    if self.session:
      self.logout()
      self.mainloop.quit()
      self.session.process_events()
      self.session.release()    
      self.session = None
    self.status = PlaybackStatus.Stopped

  def login(self, username, password):
    Log.Debug("PlaybackService do_login(username=%s)", username)
    self.status = PlaybackStatus.Authenticating
    #If no previous errors and we have a remembered user
    if self.prev_error == 0 and try_decode(self.session.remembered_user()) == username:
        self.session.relogin()
        Log( "Cached session found" )
    else:
        #do login with stored credentials
        self.session.login(username, password, True)
    loginEvent.clear()
    loginEvent.wait()

  def logout(self):
    Log.Debug("PlaybackService: logout()")
    self.status = PlaybackStatus.LoggedOut
    self.endTrack()
    if self.session.user():
      Log("PlaybackService: logout() user_name=%s", self.session.user_name())
      self.session.logout()
      logoutEvent.clear()
      logoutEvent.wait()
    

  def StartWorkerThread(self):
    self.mainloop.loop(self.session)

  def startTrack(self, stream, trackId):
    with self.trackLock:
      try:
        Log("playback starting: %s"%(trackId))
        self.endTrack_Internal()
        self.stream = stream
        link_obj = link.create_from_string("spotify:track:%s" % trackId)
        if link_obj is not None:
          track_obj = link_obj.as_track()
          self.track = load_track(self.session, track_obj)
          self.status = PlaybackStatus.Streaming
          self.firstFrame = True
          self.session.player_load(track_obj)
          self.session.player_play(True)
          Log("playback started: %s"%(trackId))
          del link_obj
          del track_obj
      except Exception as e:
        Log("Playback Service: Error Starting Track %s"%(e))
        self.endTrack_Internal()

  def writeTrackData(self, data, num_samples, sample_type, sample_rate, num_channels): 
    with self.trackLock:
      try:
        total_samples = self.get_total_samples(sample_rate)
        sample_width = self.get_sample_width(sample_type)
        return self.stream.write(total_samples, data, num_samples, sample_width, sample_rate, num_channels)
      except Exception as e:
        Log(e)
        self.endTrack_Internal()
        return 0

  def endTrack_Internal(self):
    Log('endTrack_Internal')
    try:
      self.session.player_unload()
      if self.stream:
        Log('closing stream %s'%(self.stream))
        self.stream.finish()
      self.stream = None
      self.track = None
      self.status=PlaybackStatus.Ready
      self.session.flush_caches()
    except Exception as e:
      self.status=PlaybackStatus.Ready
      Log("Playback Service: Error Ending Track %s"%(e))

  def endTrack(self):
    with self.trackLock:
      self.endTrack_Internal()

  def get_total_samples(self, sample_rate):
    return sample_rate * self.track.duration() / 1000

  def get_sample_width(self, sample_type):
    if sample_type == SampleType.Int16NativeEndian:
        return 16
    
    else:
        return -1

  def GetStatus(self):
    return self.lookupConnectionState(self.session.connectionstate())

  def lookupConnectionState(self, id):
    if id==ConnectionState.LoggedOut:
      return "LoggedOut"
    elif id==ConnectionState.LoggedIn:
      return "LoggedIn"
    elif id==ConnectionState.Disconnected:
      return "Disconnected"
    elif id==ConnectionState.Undefined:
      return "Undefined"
    elif id==ConnectionState.Offline:
      return 'Offline'
    else:
      return None


#########################################################################
## Callback Functions

  def logged_in(self, session, error_num):
    self.prev_error = error_num
    if error_num == ErrorType.Ok:
      Log.Debug("PlaybackService Login Success")
      self.status = PlaybackStatus.Ready
    else:
      Log.Debug("PlaybackService Login Failed error=%s"%(error_num))
      self.status = PlaybackStatus.AuthenticationFailed
    loginEvent.set()

  def logged_out(self, session):
    Log('PlaybackService Logged Out')
    logoutEvent.set()
    self.status = PlaybackStatus.LoggedOut

  def connection_error(self, session, error):
    Log('connection error: {0:d}'.format(error))

  def message_to_user(self, session, data):
    Log('message to user: {0}'.format(data))

  def log_message(self, session, data):
    Log("Spotify Callbacks: %s" %data, True)
    pass

  def streaming_error(self, session, error):
    Log('streaming error: {0:d}'.format(error))

  def play_token_lost(self, session):
    Log ('play_token_lost')
    self.endTrack()

  def end_of_track(self, session):
    Log ('end_of_track')
    self.endTrack()

  def notify_main_thread(self, session):
    # Log('notify_main_thread')
    self.mainloop.notify()

  def music_delivery(self, session, data, num_samples, sample_type, sample_rate, num_channels):
    # try:
      # Log('music_delivery num_samples=%s, sample_type=%s, sample_rate=%s, num_channels=%s' % (num_samples,sample_type,sample_rate,num_channels))
      return self.writeTrackData(data, num_samples, sample_type, sample_rate, num_channels)
    # except Exception as e:
    #   Log(e)
    #   self.endTrack()

  def start_playback(self, session):
    Log ('start_playback')
    
  def stop_playback(self, session):
    Log ('stop_playback')
  
  # def get_audio_buffer_stats(self, session):
  #   Log ('get_audio_buffer_stats')
  
  def offline_status_updated(self, session):
    Log ('offline_status_updated')
  
  def offline_error(self, session, error):
    Log ('offline_error')
  
  def credentials_blob_updated(self, session, blob):
    Log ('credentials_blob_updated')
  
  def connectionstate_updated(self, session):
    Log("connectionstate_updated")
    stateStr = self.GetStatus()
    Log ('connectionstate_updated state=%s'%(stateStr))