@@ -15,9 +15,7 @@ def update_local_rates():
15
15
return tree
16
16
17
17
18
- def convert_amount (amount , in_curr , currency_root , out_curr ):
19
- path = "{{http://www.ecb.int/vocabulary/2002-08-01/eurofxref}}Cube/.[@currency='{c}']"
20
-
18
+ def non_eur_convert (amount , in_curr , currency_root , out_curr , path ):
21
19
in_curr = currency_root .find (path .format (c = in_curr )).attrib
22
20
in_curr_rate = float (in_curr ['rate' ])
23
21
in_curr_currency = in_curr ['currency' ]
@@ -40,19 +38,41 @@ def convert_amount(amount, in_curr, currency_root, out_curr):
40
38
else :
41
39
print ('Cannot convert from {fr} to {to}' .format (fr = in_curr_currency , to = oc ), file = sys .stderr )
42
40
continue
43
- # a
41
+
42
+
43
+ def from_eur_convert (amount , in_curr , currency_root , out_curr , path ):
44
+ for oc in out_curr :
45
+ try :
46
+ tmp_out_curr = currency_root .find (path .format (c = oc )).attrib
47
+ # print(tmp_out_curr)
48
+ oc_rate = float (tmp_out_curr ['rate' ])
49
+ oc_currency = tmp_out_curr ['currency' ]
50
+ wanted = oc_rate * amount
51
+ # print("{am} {fr} = {res} {to}".format(am=amount, fr=in_curr_currency, res=wanted, to=oc_currency))
52
+ yield oc_currency , wanted
53
+ except (KeyError , AttributeError ):
54
+ if oc == 'EUR' :
55
+ # print("{am} {fr} = {res} {to}".format(am=amount, fr=in_curr_currency, res=wanted, to=oc))
56
+ yield in_curr , amount
57
+ else :
58
+ print ('Cannot convert from {fr} to {to}' .format (fr = in_curr , to = oc ), file = sys .stderr )
59
+ continue
60
+
61
+
44
62
def cmd_args ():
45
63
parser = argparse .ArgumentParser (description = 'Currency Converter, source file fom ECB' )
46
64
parser .add_argument ('amount' , metavar = '<amount>' , type = float ,
47
- help = 'Amount to be converted' )
65
+ help = 'Amount to be converted' )
48
66
parser .add_argument ('fr' , metavar = '<from>' ,
49
- help = 'Input currency' )
67
+ help = 'Input currency' )
50
68
51
69
parser .add_argument ('to' , metavar = '<to>' , nargs = '*' ,
52
- default = ['EUR' , 'USD' , 'JPY' , 'BGN' , 'CZK' , 'DKK' , 'GBP' , 'HUF' , 'PLN' , 'RON' , 'SEK' , 'CHF' , 'NOK' ,
53
- 'HRK' , 'RUB' , 'TRY' , 'AUD' , 'BRL' , 'CAD' , 'CNY' , 'HKD' , 'IDR' , 'ILS' , 'INR' , 'KRW' , 'MXN' ,
54
- 'MYR' , 'NZD' , 'PHP' , 'SGD' , 'THB' , 'ZAR' ],
55
- help = 'Output currency(ies), EUR, CZK...' )
70
+ default = ['EUR' , 'USD' , 'JPY' , 'BGN' , 'CZK' , 'DKK' , 'GBP' , 'HUF' , 'PLN' , 'RON' , 'SEK' , 'CHF' ,
71
+ 'NOK' ,
72
+ 'HRK' , 'RUB' , 'TRY' , 'AUD' , 'BRL' , 'CAD' , 'CNY' , 'HKD' , 'IDR' , 'ILS' , 'INR' , 'KRW' ,
73
+ 'MXN' ,
74
+ 'MYR' , 'NZD' , 'PHP' , 'SGD' , 'THB' , 'ZAR' ],
75
+ help = 'Output currency(ies), EUR, CZK...' )
56
76
57
77
parser .add_argument ('-json' ,
58
78
action = 'store_true' ,
@@ -63,7 +83,6 @@ def cmd_args():
63
83
64
84
65
85
def main ():
66
-
67
86
args = cmd_args ()
68
87
args .to = list (map (lambda x : x .strip (',;' ), args .to ))
69
88
@@ -78,17 +97,26 @@ def main():
78
97
79
98
now = dt .datetime .now ()
80
99
81
- if (currency_time != now .date ().isoformat ()) and now .hour > 16 : # updated today after 16:00
100
+ # update local XML in case of old info
101
+ if (currency_time != now .date ().isoformat ()) and now .hour > 16 and now .weekday () not in (5 , 6 ):
102
+ print (currency_time )
82
103
tree = update_local_rates ()
83
104
root = tree .getroot ()
84
105
85
106
cube = root [2 ][0 ]
86
107
87
- converted_dict = dict ((k , v ) for (k , v ) in convert_amount (10 , args .fr , cube , args .to ))
108
+ path = "{{http://www.ecb.int/vocabulary/2002-08-01/eurofxref}}Cube/.[@currency='{c}']"
109
+
110
+ if args .fr == 'EUR' :
111
+ res_gen = from_eur_convert (args .amount , args .fr , cube , args .to , path )
112
+ else :
113
+ res_gen = non_eur_convert (args .amount , args .fr , cube , args .to , path )
114
+
115
+ converted_dict = dict ((k , v ) for (k , v ) in res_gen )
88
116
89
- output = { "input" : { "amount" : args .amount , "currency" : args .fr },
90
- "output" : converted_dict
91
- }
117
+ output = {"input" : {"amount" : args .amount , "currency" : args .fr },
118
+ "output" : converted_dict
119
+ }
92
120
93
121
if args .json :
94
122
with open ('currency.json' , 'w' ) as oj :
@@ -98,6 +126,5 @@ def main():
98
126
print (json .dumps (output , sort_keys = True , indent = 4 ))
99
127
100
128
101
-
102
129
if __name__ == '__main__' :
103
130
main ()
0 commit comments