File size: 4,852 Bytes
a350783
 
130fc9d
 
 
 
 
 
 
 
3c86687
130fc9d
3c86687
130fc9d
 
 
 
 
 
 
 
3c86687
130fc9d
77b3c51
130fc9d
 
 
77b3c51
 
 
3c86687
 
 
 
 
 
 
 
 
 
 
 
 
 
a350783
 
 
 
 
 
 
 
3c86687
 
130fc9d
 
 
 
 
 
 
d271195
3c86687
 
 
 
 
 
 
 
 
 
 
 
 
 
a350783
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130fc9d
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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"<caesarmonzo> {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()