def recover():

    while True:

        time.sleep(5)

        lines = []
        with open('./recover_list.txt', 'r') as f:
            lines = f.readlines()

        if len(lines) <= 0:
            continue

        print('recovering start...')

        credentials = lines[0].strip()
        group_name = util.extract_grouping_name(credentials)
        group_alphabet = group_name[:-1]

        group_info = None
        for g in datas.credentials_list:
            if group_alphabet == g.get_group_alphabet():
                group_info = g
                break
        
        origin_cre_info = None
        recover_cre_info = None
        if group_info:
    
            for cre in group_info.credentials_list:
                print(cre.get_credentials_name())
                if cre.get_state() == datas.CredentialsInfo.STATE_USABLE:
                    origin_cre_info = cre
                if cre.get_credentials_name() == credentials:
                    recover_cre_info = cre

        if (origin_cre_info == None) or (recover_cre_info == None):
            continue

        store = credentials_mgr.get_storage(origin_cre_info.get_user_id())
        service_o = credentials_mgr.get_service(store)
        store = credentials_mgr.get_storage(recover_cre_info.get_user_id())
        service_r = credentials_mgr.get_service(store)
        
        all_origin_files = credentials_mgr.retrieve_all_files(service_o)
        recover_files = credentials_mgr.retrieve_all_files(service_r)

        origin_jigsaw_folder_id = ''
        for file in all_origin_files:
            if file['title'] == 'jigsaw':
                origin_jigsaw_folder_id = file['id']
                break

        origin_children_folder_dic = credentials_mgr.get_children_folder(all_origin_files, origin_jigsaw_folder_id)

        recover_jigsaw_folder_info = None        
        for file in recover_files:
            if file['title'] == 'jigsaw':
                recover_jigsaw_folder_info = file
                break
        if recover_jigsaw_folder_info == None:
            recover_jigsaw_folder_info = credentials_mgr.create_public_folder(service_r, 'jigsaw')

        recover_children_folder_dic = {}
        for folder_name in origin_children_folder_dic.values():
            folder_info = credentials_mgr.create_public_folder(service_r, folder_name, recover_jigsaw_folder_info['id'])
            recover_children_folder_dic[folder_info['title']] = folder_info['id']

        for file in all_origin_files:
        
           parent_folder_id = file['parents'][0]['id']
           if parent_folder_id in origin_children_folder_dic:
                folder_name = origin_children_folder_dic[parent_folder_id]
                file_name = folder_name + '_' + file['title']
                file_path_name = './recover_files/'+ file_name
                #download
                credentials_mgr.download_file(service_o, file['id'],file_path_name ) 
                #upload
                credentials_mgr.upload_file(service_r, recover_children_folder_dic[folder_name], file_name, file['mimeType'], file_path_name)

        recover_cre_info.set_usable_state()
        group_info.compute_group_state()        

        lines.pop(0)
        with open('./recover_list.txt', 'w') as f:
            f.write('')
            for str in lines:
                f.write(str)

        git_manager.recover_add()
def redirect_url():

    group_info = None
    cre_obj = None
    store = None

    try:
        error = request.args.get('error', None)
        if error != None:
            return error

        id = request.args.get('state', None)
        if id == None:
            return 'No Id'

        store, group_info, cre_obj  = credentials_mgr.make_storage(id)

        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE,
                                   scope=SCOPES,
                                   redirect_uri='%s/redirect_url'% SERVER_URL.strip())
        auth_code = request.args.get('code',None)
        credentials = flow.step2_exchange(auth_code)

        store.put(credentials)

        service = credentials_mgr.get_service(store)
        credentials_mgr.delete_all_files(service) # test code
        folder = credentials_mgr.create_public_folder(service, 'jigsaw')

        cre_dic = json.loads(credentials.to_json())
        cre_dic['jigsaw_folder_id'] = folder['id']
        cre_str = json.dumps(cre_dic)

        f = open(datas.credentials_path + '%s' % datas.credential_dic[id],'w') 
        f.write(cre_str)
        f.close()

    except Exception as error:
        print(error)
        try:
            cre_obj.set_recovery_wait_state()
            cre_obj.set_id_credentials_name('','')

            end_group_info = datas.credentials_list[-1]
            if group_info.get_group_alphabet() == end_group_info.group_alphabet_:
                state = datas.CredentialsInfo.STATE_RECOVERY_WAIT
                for cre in end_group_info.credentials_list:
                    if cre.get_state() == datas.CredentialsInfo.STATE_USABLE:
                        state = cre.get_state
                        break
                if state == datas.CredentialsInfo.STATE_USABLE:
                    datas.credentials_list.pop()
        except Exception as e:
            pass

        try:
            os.remove(datas.credentials_path + datas.credential_dic[id])
        except:
            pass
        try:
            del datas.credential_dic[id]
        except:
            pass

        return render_template('failed_donation.html')

    print('id : ' + id + ' credentials name : ' + store._filename.split('/')[-1])
    cre_obj.set_id_credentials_name(id, store._filename.split('/')[-1])
    if cre_obj.get_state() == datas.CredentialsInfo.STATE_RECOVERY_WAIT:
        fp = open('./recover_list.txt', 'a')
        fp.write(store._filename.rsplit('/',1)[-1] + '\n')
        fp.close()
        cre_obj.set_recovering_state()
        git_manager.recover_add()

    '''
    log = 'donate_' + id + '\n'
    f = open('./log.txt', 'a')
    f.write(log)
    f.close()
    datas.log_list.append(log)
    print(datas.log_list)
    '''
    
    group_info.compute_group_state()
    return render_template('donation_result.html')