@@ -16,8 +16,10 @@ func help(err int) {
1616 "Usage: forex [options...]\n " +
1717 " [-base] <base> Base currency\n " +
1818 " [-quote] <quote> Quote currency\n " +
19- " -decimal <separator> Decimal separator\n " +
20- " -thousands <separator> Thousands seperator\n " +
19+ " -idecimal <separator> Input decimal separator\n " +
20+ " -ithousands <separator> Input thousands seperator\n " +
21+ " -odecimal <separator> Output decimal separator\n " +
22+ " -othousands <separator> Output thousands seperator\n " +
2123 " -rest <address:port> Start REST API on given socket\n " )
2224 os .Exit (err )
2325}
@@ -26,7 +28,7 @@ func help(err int) {
2628var pattern , _ = regexp .Compile ("<div><div class=\" BNeawe iBp4i AP7Wnd\" ><div><div class=\" BNeawe iBp4i AP7Wnd\" >.*</div></div></div></div></div><div class=\" nXE3Ob\" >" )
2729
2830// Query for pair
29- func query (rest bool , base * string , quote * string , decimal * string , thousands * string ) (* string , error ) {
31+ func query (rest bool , base * string , quote * string , idecimal * string , ithousands * string , odecimal * string , othousands * string ) (* string , error ) {
3032
3133 // URL encode white space
3234 * base = strings .ReplaceAll (strings .ReplaceAll (* base , " " , "+" ), " " , "+" )
@@ -54,16 +56,28 @@ func query(rest bool, base *string, quote *string, decimal *string, thousands *s
5456 } else {return nil , errors .New ("Pair not found" )}
5557 }
5658
57- // Extract rate from element
58- rawRate := strings .Split (strings .Split (string (match [76 :len (match )- 50 ]), " " )[0 ], "." )
59-
6059 // Set format defaults if not given
61- if decimal == nil {decimal = new (string )}
62- if * decimal == "" {* decimal = "." }
63- if thousands == nil {thousands = new (string )}
60+
61+ if idecimal == nil {idecimal = new (string )}
62+ if ithousands == nil {ithousands = new (string )}
63+ if odecimal == nil {odecimal = new (string )}
64+ if othousands == nil {othousands = new (string )}
65+
66+ if * idecimal == "" {
67+ if * ithousands == "." {* idecimal = ","
68+ } else {* idecimal = "." }
69+ }
70+ if * ithousands == "" {
71+ if * idecimal == "," {* ithousands = "."
72+ } else {* ithousands = "," }
73+ }
74+ if * odecimal == "" {* odecimal = * idecimal }
75+
76+ // Extract rate from element
77+ rawRate := strings .Split (strings .Split (string (match [76 :len (match )- 50 ]), " " )[0 ], * idecimal )
6478
6579 // Format rate and return
66- rate := strings .ReplaceAll (rawRate [0 ], "," , * thousands )+ * decimal + rawRate [1 ]
80+ rate := strings .ReplaceAll (rawRate [0 ], * ithousands , * othousands )+ * odecimal + rawRate [1 ]
6781 return & rate , nil
6882}
6983
@@ -73,8 +87,10 @@ func httpHandle(response http.ResponseWriter, request *http.Request) {
7387 // Get request queries
7488 base := request .URL .Query ().Get ("base" )
7589 quote := request .URL .Query ().Get ("quote" )
76- decimal := request .URL .Query ().Get ("decimal" )
77- thousands := request .URL .Query ().Get ("thousands" )
90+ idecimal := request .URL .Query ().Get ("idecimal" )
91+ ithousands := request .URL .Query ().Get ("ithousands" )
92+ odecimal := request .URL .Query ().Get ("odecimal" )
93+ othousands := request .URL .Query ().Get ("othousands" )
7894
7995 // Report error if base or quote not given
8096 if base == "" || quote == "" {
@@ -83,7 +99,7 @@ func httpHandle(response http.ResponseWriter, request *http.Request) {
8399 }
84100
85101 // Query Google for rate
86- rate , err := query (true , & base , & quote , & decimal , & thousands )
102+ rate , err := query (true , & base , & quote , & idecimal , & ithousands , & odecimal , & othousands )
87103 if err != nil {
88104 response .Write ([]byte (err .Error ()))
89105 return
@@ -102,8 +118,10 @@ func main() {
102118 var (
103119 base * string
104120 quote * string
105- decimal * string
106- thousands * string
121+ idecimal * string
122+ ithousands * string
123+ odecimal * string
124+ othousands * string
107125 rest * string
108126 )
109127
@@ -123,17 +141,29 @@ func main() {
123141 if len (os .Args ) == i {help (1 )}
124142 quote = & os .Args [i ]
125143 continue
126- case "decimal" :
144+ case "idecimal" :
145+ i ++
146+ if idecimal != nil {help (1 )}
147+ if len (os .Args ) == i {help (1 )}
148+ idecimal = & os .Args [i ]
149+ continue
150+ case "ithousands" :
151+ i ++
152+ if ithousands != nil {help (1 )}
153+ if len (os .Args ) == i {help (1 )}
154+ ithousands = & os .Args [i ]
155+ continue
156+ case "odecimal" :
127157 i ++
128- if decimal != nil {help (1 )}
158+ if odecimal != nil {help (1 )}
129159 if len (os .Args ) == i {help (1 )}
130- decimal = & os .Args [i ]
160+ odecimal = & os .Args [i ]
131161 continue
132- case "thousands " :
162+ case "othousands " :
133163 i ++
134- if thousands != nil {help (1 )}
164+ if othousands != nil {help (1 )}
135165 if len (os .Args ) == i {help (1 )}
136- thousands = & os .Args [i ]
166+ othousands = & os .Args [i ]
137167 continue
138168 case "rest" :
139169 i ++
@@ -157,7 +187,7 @@ func main() {
157187 if strings .HasPrefix (* rest , ":" ) && strings .Count (* rest , ":" ) == 1 {
158188 restAddr = "localhost" + * rest
159189 } else {restAddr = * rest }
160- os .Stdout .WriteString ("Listening on " + restAddr + "\n Example: " + restAddr + "/api?base=eur"e=usd&decimal=,&thousands =.\n " )
190+ os .Stdout .WriteString ("Listening on " + restAddr + "\n Example: " + restAddr + "/api?base=eur"e=usd&idecimal=.&ithousands=,&odecimal=,&othousands =.\n " )
161191
162192 http .ListenAndServe (* rest , nil )
163193 os .Exit (0 )
@@ -166,7 +196,7 @@ func main() {
166196 // Display help and exit if no base or quote given
167197 if base == nil || quote == nil {help (2 )
168198 } else {
169- rate , err := query (false , base , quote , decimal , thousands )
199+ rate , err := query (false , base , quote , idecimal , ithousands , odecimal , othousands )
170200 if err != nil {
171201 os .Stdout .WriteString (err .Error ())
172202 os .Exit (- 1 )
0 commit comments