Created
January 18, 2023 16:25
-
-
Save NickBouwhuis/abd89f2cd4a9cdaa506b68b77e2a2147 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import forex_python.converter | |
# Define variables | |
stocks = {"AAPL": 100, "TSLA": 100, "MSFT": 100} | |
finnhub_token = 'xxx' | |
lunchmoney_token = 'xxx' | |
lunchmoney_assetid = 1234 | |
# Initialize the total worth in EUR. | |
# You can add cash that's not invested but still in your account here | |
total_worth_eur = 0 | |
# Create a converter object | |
converter = forex_python.converter.CurrencyRates() | |
# Iterate through the symbols and multipliers | |
for symbol, multiplier in stocks.items(): | |
# Make the GET request to the API | |
response = requests.get(f'https://finnhub.io/api/v1/quote?symbol={symbol}&token={finnhub_token}') | |
# Get the current price in USD | |
current_price_usd = response.json()['c'] | |
# Multiply the current price by the multiplier | |
current_price_usd *= multiplier | |
# Convert the value from USD to EUR | |
current_price_eur = converter.convert('USD', 'EUR', current_price_usd) | |
# Add the converted value to the total worth in EUR | |
total_worth_eur += current_price_eur | |
# Prepare the JSON payload with the balance key | |
payload = {'balance': total_worth_eur} | |
# Prepare the headers with the API token | |
headers = {'Authorization': f'Bearer {lunchmoney_token}'} | |
# Make a PUT request to the Lunchmoney API | |
response = requests.put(f'https://dev.lunchmoney.app/v1/assets/{lunchmoney_assetid}', json=payload, headers=headers) | |
# Check the response status code | |
if response.status_code == 200: | |
print(f'Succesfully updated asset {lunchmoney_assetid} to {total_worth_eur} EUR') | |
else: | |
print(f'An error occurred: {response.text}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment