-
Notifications
You must be signed in to change notification settings - Fork 2
/
moneywiz_url_parser.py
58 lines (44 loc) · 1.89 KB
/
moneywiz_url_parser.py
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
# https://help.wiz.money/en/articles/4525440-automate-transaction-management-with-url-schemas
# This is moneywiz wrapper module
# It contains all functions for working with MoneyWiz
# It is used in main.py
#
from datetime import datetime
import urllib.parse
# Function for getting MoneyWiz URL for expense or income operations
def get_moneywiz_url(account, amount, currency, date, payee="", description="", memo="", tags="", save=False):
# Set operation type
if amount < 0:
operation = "expense"
# Make amount positive for MoneyWiz
amount = abs(amount)
else:
operation = "income"
# Fix account name. It should not contain spaces
account = account.replace(" ", "")
# Check amount. It should use dot as decimal delimiter
if "." not in str(amount):
raise ValueError("Amount should use dot as decimal delimiter")
# Currency code should be in uppercase
currency = currency.upper()
# Format date as "yyyy-MM-dd HH:mm:ss" for MoneyWiz
date_obj = datetime.strptime(date, "%d/%m/%y %H:%M")
formatted_date = date_obj.strftime("%Y-%m-%d %H:%M:%S")
# Create a list to store URL components
url_components = [
f"moneywiz://{operation}?account={account}&amount={amount}¤cy={currency}&date={formatted_date}"
]
# Add optional parameters
optional_params = {
"payee": urllib.parse.quote(payee.strip()),
"description": urllib.parse.quote(description.strip()),
"memo": urllib.parse.quote(memo.strip()),
"tags": urllib.parse.quote(tags.strip()),
}
url_components.extend([f"&{key}={value}" for key, value in optional_params.items() if value])
# Append 'save=true' if save parameter is True
if save:
url_components.append("&save=true")
# Join URL components using str.join()
moneywiz_url = "".join(url_components).replace(' ', '%20')
return moneywiz_url