예제 #1
0
파일: main.py 프로젝트: Zauberstuhl/spotimc
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()
예제 #2
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)
예제 #3
0
파일: __init__.py 프로젝트: keydon/spotimc
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()
예제 #4
0
파일: __init__.py 프로젝트: andrewl/spotimc
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()
예제 #5
0
파일: main.py 프로젝트: disrupted/spotimc
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 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)