import os import json import requests from decouple import config class CaesarFinance: def __init__(self) -> None: self.CLIENT_ID = config('CLIENT_ID') self.CLIENT_SECRET = config('CLIENT_SECRET') self.access_token = None def create_access_code(self): print(f"https://auth.monzo.com/?client_id={self.CLIENT_ID}&redirect_uri=https://palondomus-caesarai.hf.space&response_type=code&state=state_token") val = input("Have you created the access code? (y), (n)") if val.lower() == "y": return True elif val.lower() == "n": return False else: return False def get_access_token(self,code): form_data = {'grant_type': 'authorization_code','client_id':self.CLIENT_ID,'client_secret':self.CLIENT_SECRET, "redirect_uri":"https://palondomus-caesarai.hf.space", "code":code } server = requests.post('https://api.monzo.com/oauth2/token', data=form_data) output = server.json()["access_token"] return output def whoami(self): response = requests.get("https://api.monzo.com/ping/whoami",headers={'Authorization': 'Bearer ' + self.access_token}) return response.json() def get_accounts(self): response = requests.get("https://api.monzo.com/accounts",headers={'Authorization': 'Bearer ' + self.access_token}) return response.json() def phone_auth(self): phone_auth_val = input("Authenticated on phone? (y),(n)") if phone_auth_val.lower() == "y": return True elif phone_auth_val.lower() == "n": return False else: return False def get_balance(self,account_id): response = requests.get("https://api.monzo.com/balance",params={"account_id":account_id},headers={'Authorization': 'Bearer ' + self.access_token}) return response.json() # get balance def get_transactions(self,account_id): response = requests.get("https://api.monzo.com/transactions",params={"account_id":account_id},headers={'Authorization': 'Bearer ' + self.access_token}) return response.json() if __name__ == "__main__": caesarfinance = CaesarFinance() access_code_bool = False while access_code_bool == False: access_code_bool = caesarfinance.create_access_code() #code = caesarfinance.fetch_access_code() access_token_bool = False while access_token_bool == False: code = input("Insert access token:") if "eyJhb" in code: access_token_bool = True elif "eyJhb" not in code: access_token_bool = False access_token = caesarfinance.get_access_token(code) caesarfinance.access_token = access_token access_phone_auth = False while access_phone_auth == False: access_phone_auth = caesarfinance.phone_auth() accountinfo = caesarfinance.get_accounts() personal = accountinfo["accounts"][0]["id"] business = accountinfo["accounts"][-1]["id"] monzo_authorized = True if "accounts" in accountinfo else False while monzo_authorized == True: # get the command from prompt cfin = "caesar" cwd = os.getcwd() command = input(f" {cwd} $> ") if command == f"{cfin} --help": with open(f"caesarfinhelp.txt","r") as f: helpinfo = f.read() print(helpinfo) if command == f"{cfin} accounts" or command == f"{cfin} -a": accountinfo = caesarfinance.get_accounts() print(accountinfo) if command == f"{cfin} accounts -b personal": transactioninfo = caesarfinance.get_balance(personal) print(transactioninfo) if command == f"{cfin} accounts -b business": transactioninfo = caesarfinance.get_balance(business) print(transactioninfo) if command == f"{cfin} accounts -t personal": transactioninfo = caesarfinance.get_transactions(personal) if "--save" in command: with open("caesarpersonaltransactions.json","w+") as f: json.dump(transactioninfo,f) print(transactioninfo) if command == f"{cfin} accounts -t business": transactioninfo = caesarfinance.get_transactions(business) if "--save" in command: with open("caesarbusinesstransactions.json","w+") as f: json.dump(transactioninfo,f) print(transactioninfo) if not command.strip(): # empty command continue if command.lower() == "exit": # if the command is exit, just break out of the loop break #caesarfinance.get_access_code()