def download_file(self, name, path_to_file): """ Download the corresponding file if exists and store it in the path """ log('Onedrive: Download: ' + name) root_folder = self.client.item(drive='me', id='root').children.get() id_of_file = root_folder[0].id self.client.item(drive='me', id=id_of_file).download(path_to_file)
def check_bankin_account(self): """ Check the validity of the user's email""" log('check user') response = requests.get(self.settings_url, headers=self.headers) if response.status_code != 200: raise PostGetErrors(response.status_code, "error raised on checking the user's login") return response.json()['email_valid']
def store_balance(): """ Main script to login, refresh the balance, save it in an excel file, logout""" # Check whether the setup is done if not os.path.exists(pathfiles.get_account_folder): log('Please run <python setup_oath.py> to setup your Onedrive and Bankin oauth configs') exit(0) # Read options options = conf.parse_setup_options(pathfiles.setup_options) # Open the login file and retrieve the personal data to login to Bankin account and Onedrive bankin_param = conf.parse_bankin_params(pathfiles.bankin_oauth) # Get the password in the console password = getpass.getpass('Type your bankin password: '******'email'], password, bankin_param['client_id'], bankin_param['client_secret']) # Save the path in temp folder excel_path = pathfiles.data_temp_file if options['save'] == 'onedrive': onedrive_param = conf.parse_onedrive_params(pathfiles.onedrive_oauth) onedrive_interface = OnedriveInterface(onedrive_param['client_id'], onedrive_param['client_secret'], onedrive_param['onedrive_uri']) # Authenticate to onedrive if not onedrive_interface.authenticate(): exit(0) # Download the file onedrive_interface.download_file(pathfiles.account_filename, pathfiles.data_temp_file) else: if options['local_path'] != 'none': excel_path = options['local_path'] try: if bankin_interface.authenticate(): if bankin_interface.refresh_items(): data = bankin_interface.get_items_balance() # Get the latest balance of all the bankin accounts bankin_interface.logout() excel_interface = ExcelInterface(excel_path, pathfiles.account_filename) excel_interface.save_in_excel(data) if options['save'] == 'onedrive': onedrive_interface.upload_file(pathfiles.account_filename, pathfiles.data_temp_file) if options['send'] == 'email': print('wip') except PostGetErrors as error: print('error: ' + error.message)
def authenticate(self): """ Authenticate the user""" log('Authenticate') response = requests.post(self.authenticate_url, headers=self.headers, params=self.params) if response.status_code != 200: raise PostGetErrors(response.status_code, 'error raised on authenticate') self.headers.update( {'Authorization': 'Bearer ' + response.json()['access_token']}) return self.check_bankin_account()
def authenticate(self): """ Authenticate to onedrive and return is success or not """ log('Onedrive: Authenticate') auth_url = self.client.auth_provider.get_auth_url(self.redirect_uri) # Block thread until we have the code code = GetAuthCodeServer.get_auth_code(auth_url, self.redirect_uri) # Finally, authenticate! try: self.client.auth_provider.authenticate(code, self.redirect_uri, self.client_secret) except onedrivesdk.error.ErrorCode as error: log(error) return False return True
def refresh_items(self): """ Refresh all the items""" log('Refresh bank accounts') # Store the url url = self.items_url + '/refresh' # Ask for item refresh response = requests.post(url, headers=self.headers) if response.status_code != 200 and response.status_code != 202: raise PostGetErrors(response.status_code, 'error raised on refreshing') refreshed = False while not refreshed: # Check if the item is refreshed response = requests.get(url + '/status', headers=self.headers) # While the item is not refreshed check for its status every second for 20 seconds refresh_response = json.loads( response.content.decode('utf-8'))['resources'] refreshed = True for response in refresh_response: if response['status'] == 'finished': continue if response['status'] != 'finished': refreshed = False if response['status'] == 'finished_error': print("Error on refresh") return True print('Retrieving data from banks; timeout after ' + str(self.timeout) + 's') time.sleep(1) self.timeout -= 1 if self.timeout <= 0: print( 'Retrieving data from banks; Timeout ! Please try to connect to your bankin account' ' on the web and understand the issue') return False log('Bank accounts updated') return True
def create_folders(): # If the get_account folders exists then create folder if not os.path.exists(pathfiles.temp_folder): log('Create folders temps_file in ' + pathfiles.temp_folder) os.makedirs(pathfiles.temp_folder) if not os.path.exists(pathfiles.config_folder): log('Create folders configs in ' + pathfiles.config_folder) os.makedirs(pathfiles.config_folder) # Create empty yaml # Onedrive oauth onedrive_oauth = {'client_id': 'none', 'client_secret': 'none', 'onedrive_uri': 'none'} with open(pathfiles.onedrive_oauth, 'w') as file: yaml.dump(onedrive_oauth, file) # Bankin oauth bankin_oauth = {'email': 'none', 'client_id': 'none', 'client_secret': 'none'} with open(pathfiles.bankin_oauth, 'w') as file: yaml.dump(bankin_oauth, file) # Options options = {'send': 'none', 'save': 'local', 'local_path': 'none'} yaml.dump(options, file)
def setup_onedrive(): log('Setup Onedrive Oauth') client_id = str(input('input onedrive client_id: ')) client_secret = str(input('input onedrive client_secret: ')) onedrive_uri = str(input('input onedrive onedrive_uri: ')) onedrive_oauth = {'client_id': client_id, 'client_secret': client_secret, 'onedrive_uri': onedrive_uri} # Store in the corresponding yaml with open(pathfiles.onedrive_oauth, 'w') as file: yaml.dump(onedrive_oauth, file) # Test the connection onedrive_interface = OnedriveInterface(client_id, client_secret, onedrive_uri) if not onedrive_interface.authenticate(): log('Onedrive Oauth unsuccessful') if input('Do you want to try again ? (y/n)') == 'y': setup_bankin() else: log('Onedrive Oauth Successful')
def setup_options(option, value): log('Setup Options') # Read option Yaml or create options = conf.parse_setup_options(pathfiles.setup_options) # Replace the option options[option] = value log('Set '+option+' to: ' + value) # Ask where to save in local if value == 'local': if input('Do you want to change the path ? (current local path: ' + options['local_path'] + ') yes/no: ') \ != 'no': path_to_save_local = input('Where do you want to save in local: ') options['local_path'] = path_to_save_local log('Set local_path to: ' + path_to_save_local) print(options) # Save the option in the corresponding yaml with open(pathfiles.setup_options, 'w') as file: yaml.dump(options, file)
def setup_bankin(): log('Setup Bankin Oauth') email = str(input('input Bankin email: ')) client_id = str(input('input Bankin client_id: ')) client_secret = str(input('input Bankin client_secret: ')) bankin_oauth = {'email': email, 'client_id': client_id, 'client_secret': client_secret} # Store in the corresponding yaml with open(pathfiles.bankin_oauth, 'w') as file: yaml.dump(bankin_oauth, file) # Test the connection # Get the password in the console password = getpass.getpass('Type your bankin password: '******'Bankin Oauth Successful') except PostGetErrors: log('Bankin Oauth unsuccessful') if input('Do you want to try again ? (y/n)') == 'y': setup_bankin()
def upload_file(self, name, path_to_file): """ Upload the file corresponding file to the path """ log('Onedrive: Upload: ' + name) return self.client.item(drive='me', id='root').children[name].upload(path_to_file)
def logout(self): """ Logout the user""" log('logout user') response = requests.post(self.logout_url, headers=self.headers) if response.status_code != 200: raise PostGetErrors(response.status_code, 'error raised on logout')
def get_items_balance(self): """ Get all the items balance and store them in a useful DataFrame""" log('Retrieved items balance') items = self.get_items_response_json() return items