def main(addon_dir): #Check needed directories first data_dir, cache_dir, settings_dir = check_dirs() #Instantiate the settings obj settings_obj = SettingsManager() #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() 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() #Start main window and enter it's main loop mainwin = windows.MainWindow("main-window.xml", addon_dir, "DefaultSkin") mainwin.initialize(sess, proxy_runner) mainwin.doModal() #Deinit sequence player = xbmc.Player() player.stop() proxy_runner.stop() #Logout sess.logout() logout_event.wait(10) #Stop main loop ml_runner.stop() return sess
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()
def start_proxy_runner(self): self.proxy_runner = ProxyRunner(self.session, self.buffer_manager, host='127.0.0.1', allow_ranges=False) self.proxy_runner.start() self.proxy_info = ProxyInfo(self.proxy_runner) return self.proxy_info
class Server: def __init__(self): self.settings = Settings() self.authenticator = Authenticator() self.url_gen = UrlGenerator() self.model_factory = ModelFactory(self.url_gen) self.session = None self.cache_storage = CacheStorage(self.settings) self.server_is_up = False def start(self): if not self.server_is_up: self.set_up_session() self.runner = self.start_main_loop() self.start_proxy_runner() self.set_up_model_factory(self.session, self.proxy_info) self.server_is_up = True self.install_shutdown_watcher() def stop(self): if self.server_is_up: self.session.logout() self.runner.stop() self.proxy_runner.stop() self.server.shutdown() self.server.server_close() self.clean_up() self.server_is_up = False def is_active(self): return self.server_is_up def reset_settings(self): self.settings = Settings() def clean_up(self): self.session = None self.model_factory.clean_up() self.proxy_runner = None self.proxy_info = None self.authenticator.clean_up() self.main_loop = None self.buffer_manager = None self.cache_storage = None def start_main_loop(self): runner = MainLoopThread(self.main_loop, self.session) runner.start() return runner def start_proxy_runner(self): self.proxy_runner = ProxyRunner(self.session, self.buffer_manager, host='127.0.0.1', allow_ranges=False) self.proxy_runner.start() self.proxy_info = ProxyInfo(self.proxy_runner) return self.proxy_info def start_rpc_server(self): self.server = SimpleXMLRPCServer(("127.0.0.1", self.settings.internal_server_port)) self.server.register_instance(LocalService(self)) self.server.serve_forever() def install_shutdown_watcher(self): ShutdownWatcher(self).start() 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 set_up_authenticator(self, session): self.authenticator.set_session(session) def set_up_playlistcontainer_callbacks(self, session): container = session.playlistcontainer() container.add_callbacks(PlaylistCallbacks(self.cache_storage)) def set_up_model_factory(self, session, proxy_info): self.url_gen.set_session(session) self.url_gen.set_proxy_info(proxy_info) def log_in(self): # Running login twice ends up in offline mode ... if self.authenticator.connection_state() == ConnectionState.LoggedIn: return state = self.authenticator.login(self.settings.username, self.settings.password) # TODO This will not be called if the login was performed in SessionGuard # if state == ConnectionState.LoggedIn: # self.set_up_playlistcontainer_callbacks(self.session) def log_out(self): return self.authenticator.logout() def get_authenticator(self): return self.authenticator def get_model_factory(self): return self.model_factory def get_cache_storage(self): return self.cache_storage
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)
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()
def main(addon_dir): #Initialize app var storage app = Application() logout_event = Event() app.set_var('logout_event', logout_event) app.set_var('exit_requested', False) #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 continue normal operation else: #TODO: Wrap this inside a function ip_list = ['127.0.0.1', xbmc.getInfoLabel('Network.IPAddress')] proxy_runner = ProxyRunner(sess, buf, host='0.0.0.0', allowed_ips=ip_list) 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, app) app.set_var('main_window', mainwin) 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 app.remove_var('main_window') gc.collect() #Logout if sess.user() is not None: sess.logout() logout_event.wait(10) #Stop main loop ml_runner.stop()
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) proxy_runner.set_stream_end_callback(preloader_cb) user_agent = try_decode('Spotify/{0} (XBMC/{1})'.format(ADDON_VERSION, xbmc.getInfoLabel("System.BuildVersion"))) playtoken = proxy_runner.get_user_token(user_agent) header_dict = { 'User-Agent': user_agent, 'X-Spotify-Token': playtoken } 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)') proxy_runner.clear_stream_end_callback() proxy_runner.stop() buf.cleanup() #Clear some vars and collect garbage proxy_runner = None preloader_cb = None #Logout 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) != '': logMsg("ERROR in backgroundservice! " + str(ex)) 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)