35 lines
1.0 KiB
Python
35 lines
1.0 KiB
Python
|
|
import re
|
||
|
|
import json
|
||
|
|
import csv
|
||
|
|
import sys
|
||
|
|
import requests
|
||
|
|
from bs4 import BeautifulSoup
|
||
|
|
|
||
|
|
def get_price(market, ticker):
|
||
|
|
url = f"https://web.freetrade.io/universe/{market}/{ticker}"
|
||
|
|
response = requests.get(url, headers={
|
||
|
|
"User-Agent": "Mozilla/5.0"
|
||
|
|
}, timeout=20)
|
||
|
|
text = BeautifulSoup(response.text, "html.parser").get_text(" ", strip=True)
|
||
|
|
m = re.search(r"Latest price\s*:\s*[£$]([0-9,.]+)", text)
|
||
|
|
if m:
|
||
|
|
return float(m.group(1).replace(",", ""))
|
||
|
|
return None
|
||
|
|
|
||
|
|
holdings_file = sys.argv[1] if len(sys.argv) > 1 else "holdings.csv"
|
||
|
|
|
||
|
|
results = []
|
||
|
|
with open(holdings_file) as f:
|
||
|
|
reader = csv.reader(f)
|
||
|
|
for row in reader:
|
||
|
|
ticker, market, shares = row[0].strip(), row[1].strip(), int(row[2].strip())
|
||
|
|
price = get_price(market, ticker)
|
||
|
|
results.append({
|
||
|
|
"ticker": ticker,
|
||
|
|
"market": market,
|
||
|
|
"shares": shares,
|
||
|
|
"price": price,
|
||
|
|
"total_value": round(price * shares, 2) if price is not None else None
|
||
|
|
})
|
||
|
|
|
||
|
|
print(json.dumps(results, indent=2))
|