-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathukindexprices.py
executable file
·81 lines (62 loc) · 2.03 KB
/
ukindexprices.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python
import datetime
from selenium import webdriver
from time import sleep
'''
Index fund scraper.
This python script will take a list of funds and look up their price from
iii.co.uk, then write them to a pricesdb file for ledger-cli (or beancount)'s usage.
Run cron daily/weekly/monthly
python ukfundprices.py
'''
__author__ = "Alex Johnstone <alexjj@gmail.com>"
# List of funds to look up.
funds = ('FIAAGJ',
'VVDVWE',
'VVFUSI',
'MYKAAS',
'VIUKGO',
'VIGSCA',
'VVUILG',
'VVLSRE')
# FIAAGY and MYKAAS prices are in p
penny_funds = ('FIAAGY', 'MYKAAS')
base_url = 'http://www.iii.co.uk/investment/detail?code=mex:'
end_url = '&it=ukut'
pricedb_file = '/home/alex/finance/prices.beancount'
# ledger or beancount
program = 'beancount'
#program = 'ledger'
# Make the ledger string
def make_ledger_str(fund, price):
now = datetime.datetime.today()
if program == 'ledger':
timestamp = now.strftime("%Y/%m/%d %H:%M:%S")
string = "P {} {} {} GBP".format(timestamp, fund, price)
elif program == 'beancount':
timestamp = now.strftime("%Y-%m-%d")
string = "{} price {} {} GBP".format(timestamp, fund, price)
else:
print("Only ledger or beancount")
quit()
return string
def get_prices():
price_list = []
for fund in funds:
browser = webdriver.PhantomJS()
browser.get(base_url + fund + end_url)
price = browser.find_element_by_class_name('price').text
if fund in penny_funds:
price = float(price) / 100
string = make_ledger_str(fund, price)
price_list.append(string)
browser.quit()
sleep(1) # Kept getting connection refused
return price_list
def write_prices(price_list):
with open(pricedb_file, 'a') as text_file:
for string in price_list:
print(string, file=text_file)
if __name__ == "__main__":
price_list = get_prices()
write_prices(price_list)