예제 #1
0
파일: auth.py 프로젝트: Samurais/LPan
def auth(client, config):
    if client.is_authed():
        return True

    if config.has_section('authorize'):
        oauth_token = config.get('authorize', 'oauth_token')
        oauth_token_secret = config.get('authorize', 'oauth_token_secret')
        user_id = config.getint('authorize', 'user_id')
        print 'read token, oauth_token = %s' % oauth_token
        print 'oauth_token_secret = %s' % oauth_token_secret
        print 'user_id = %d' % user_id
        client.set_auth(oauth_token, oauth_token_secret, user_id)
        
    if not client.is_authed():
        res = client.auth(authorize)
        if res:
            config.add_section('authorize')
            config.set('authorize', 'oauth_token', client._oauth_token)
            config.set('authorize', 'oauth_token_secret', client._oauth_token_secret)
            config.set('authorize', 'user_id', client._user_id)
            fp = open(get_cfg_path(), 'w')
            config.write(fp)
        elif res == False:
            print('authorize failed, try again')
            auth(client)
        else:
            print('exit authorizing')

    return client.is_authed()
예제 #2
0
파일: main.py 프로젝트: Samurais/LPan
def main():
    # load config
    config = load_config()
    _consumer_key = ""
    _consumer_secret = ""
    
    if config.has_section('client'):
        _consumer_key = config.get('client', '_consumer_key')
        _consumer_secret = config.get('client', '_consumer_secret')

    # init client
    client = Client(_consumer_key, _consumer_secret)
    
    # auth before use
    auth(client, config)

    if client.is_authed():
        global local_path
        global g_ind
        if config.has_section('sync'):
            local_path = config.get('sync', 'local_path')
        if not local_path:
            home_dir = os.path.expanduser('~')
            default_dir = os.path.join(home_dir, "kuaipan")
            is_create_default = False
            if not os.path.exists(default_dir):
                is_create_default = True
                os.mkdir(default_dir)
            
            dialog = gtk.FileChooserDialog(title="请选择快盘文件同步到本地存放的文件夹", parent=None,
                action=gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
                buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN,gtk.RESPONSE_OK))
            dialog.set_current_folder(default_dir)

            filter = gtk.FileFilter()
            filter.set_name("所有文件夹")
            filter.add_pattern("*")
            dialog.add_filter(filter)            
                
            response = dialog.run()
            if response == gtk.RESPONSE_OK:
                print dialog.get_filename(), 'selected'
                local_path = dialog.get_filename()
                if is_create_default and local_path != default_dir:
                    os.rmdir(default_dir)
                config.add_section('sync')
                config.set('sync', 'local_path', local_path)
                fp = open(get_cfg_path(), 'w')
                config.write(fp)                    
            else:
                print 'Closed, no files selected'
            dialog.destroy()

        if not local_path:
            return

        ac_info = client.get_account_info()
        print('is authed')
        print(ac_info)
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        g_ind = init_indicator(ac_info, client)

        # start sync thread
        start_sync(client, local_path, g_ind)
        
        # start monitor thread
        start_monitor(client, local_path, g_ind)
        
        # start run application
        gtk.main()
    else:
        print('not authed')