-
Notifications
You must be signed in to change notification settings - Fork 0
/
currencyCAD.py
166 lines (137 loc) · 4.7 KB
/
currencyCAD.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import sys
if sys.version[0] != '3':
print ('You must run currencyCAD with python3')
sys.exit(0)
from urllib.request import urlopen
import pifacecad.tools
import pifacecad
from time import sleep
from pifacecad.tools.question import LCDQuestion
cad = pifacecad.PiFaceCAD()
currenciesUrl = "https://www.google.com/finance/converter"
currencies = urlopen(currenciesUrl)
currencies = str(currencies.read())
currencies = currencies.split('<select name=from value="">\\n')[1]
currencies = currencies.split('</select>')[0]
currencies = currencies.replace('<option value="',',')
currencies = currencies.replace('">',',')
currencies = currencies.replace('</option>\\n',',')
currencies = currencies.split(',')
currencies.remove('')
availableCurrecies = []
for x in currencies:
if len(x) == 3:
availableCurrecies.append(x)
# print(availableCurrecies)
# fromCurrency = availableCurrecies[int(input("Enter from Currency:"))]
# toCurrency = "GBP"
# amount = 2
cad.lcd.cursor_off()
cad.lcd.backlight_on()
question = LCDQuestion(question="Load presets", answers=["Yes","No"])
answer = question.ask()
if not answer:
f = open('currencyPairs','r')
currencyPairs =[]
for line in f:
line = line.replace("\n","")
pair = line.split(" ")
currencyPairs.append(pair)
f.close()
# currencyPairs = [["GBP","JPY"],["USD","GBP"],["USD","JPY"]]
availableCurrecies = []
for pair in currencyPairs:
availableCurrecies.append(pair[0] + ">" + pair[1])
while cad.switches[4].value != 1:
scanner = pifacecad.tools.LCDScanf(format="%4i.%2i %m%r", custom_values=availableCurrecies)
request = scanner.scan()
# print(request)
while request[0] ==0 and request[1] ==0:
cad.lcd.clear()
cad.lcd.write("Must give an")
cad.lcd.set_cursor(0,1)
cad.lcd.write("amount!")
sleep(2)
cad.lcd.clear()
scanner = pifacecad.tools.LCDScanf(format="%4i.%2i %m%r", custom_values=availableCurrecies)
request = scanner.scan()
amount = (request[0]) + (request[1]*.1)
fromCurrency = currencyPairs[availableCurrecies.index(request[2])][0]
toCurrency = currencyPairs[availableCurrecies.index(request[2])][1]
url = "https://www.google.com/finance/converter?a={amount}&from={fromCurrency}&to={toCurrency}".format(amount=str(amount),fromCurrency=fromCurrency, toCurrency=toCurrency)
response = urlopen(url)
html = str(response.read())
html = html.split("= <span class=bld>")[1]
result = float(html.split(" " +toCurrency)[0])
cad.lcd.set_cursor(0,0)
cad.lcd.set_cursor(0,1)
cad.lcd.cursor_off()
cad.lcd.write(format(result, '.2f'))
print(format(result,'.2f'))
while cad.switches[0].value != 1:
if cad.switches[4].value:
break
sleep(.2)
cad.lcd.clear()
else:
while cad.switches[4].value != 1:
scanner = pifacecad.tools.LCDScanf(format="%4i.%2i %m>%m%r", custom_values=availableCurrecies)
request = scanner.scan()
# print(request)
while request[2] == request[3] or (request[0] ==0 and request[1] ==0):
cad.lcd.clear()
if request[2] == request[3]:
cad.lcd.write("Use different")
cad.lcd.set_cursor(0,1)
cad.lcd.write("currencies!")
sleep(2)
elif request[0]==0 and request[1]==0:
cad.lcd.write("Must give an")
cad.lcd.set_cursor(0,1)
cad.lcd.write("amount!")
sleep(2)
cad.lcd.clear()
scanner = pifacecad.tools.LCDScanf(format="%4i.%2i %m>%m%r", custom_values=availableCurrecies)
request = scanner.scan()
amount = (request[0]) + (request[1]*.1)
fromCurrency = request[2]
toCurrency = request[3]
url = "https://www.google.com/finance/converter?a={amount}&from={fromCurrency}&to={toCurrency}".format(amount=str(amount),fromCurrency=fromCurrency, toCurrency=toCurrency)
response = urlopen(url)
html = str(response.read())
try:
html = html.split("= <span class=bld>")[1]
except IndexError:
print("For some reason, Google finance converter does not convert between those currencies")
cad.lcd.clear()
cad.lcd.write("Google cant conv\nthose currencies")
sleep(2)
sys.exit()
result = float(html.split(" " +toCurrency)[0])
cad.lcd.set_cursor(0,0)
cad.lcd.set_cursor(0,1)
cad.lcd.cursor_off()
cad.lcd.write(format(result, '.2f'))
print(format(result,'.2f'))
written = False
while cad.switches[0].value != 1:
if cad.switches[4].value:
break
sleep(.2)
if cad.switches[1].value == 1 and not written:
written = True
f = open('currencyPairs','r')
pairs =[]
for line in f:
line = line.replace("\n","")
pair = line.split(" ")
pairs.append(pair)
f.close()
currentPair = [fromCurrency, toCurrency]
if currentPair not in pairs:
f = open('currencyPairs','a')
f.write(fromCurrency + " " + toCurrency + "\n")
cad.lcd.set_cursor(0,1)
print("Pair saved")
cad.lcd.write("Pair saved")
cad.lcd.clear()