Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for yahoofx.vbs

(view source code of yahoofx.vbs as plain text)

  1. Option Explicit
  2.  
  3. Dim dblAmount, dblConvert, dblExch, intArgs, strFromCurr, strToCurr
  4.  
  5. ' Initial values
  6. dblAmount = 0
  7. intArgs   = 0
  8.  
  9. ' Check command line arguments
  10. If WScript.Arguments.Count <> 3 Then Syntax ""
  11. If WScript.Arguments.Named.Exists( "Amount" ) Then
  12. 	dblAmount = WScript.Arguments.Named( "Amount" )
  13. 	intArgs = intArgs + 1
  14. End If
  15. If WScript.Arguments.Named.Exists( "A" ) Then
  16. 	dblAmount = WScript.Arguments.Named( "A" )
  17. 	intArgs = intArgs + 1
  18. End If
  19. If WScript.Arguments.Named.Exists( "From" ) Then
  20. 	strFromCurr = WScript.Arguments.Named( "From" )
  21. 	intArgs = intArgs + 1
  22. End If
  23. If WScript.Arguments.Named.Exists( "F" ) Then
  24. 	strFromCurr = WScript.Arguments.Named( "F" )
  25. 	intArgs = intArgs + 1
  26. End If
  27. If WScript.Arguments.Named.Exists( "To" ) Then
  28. 	strToCurr = WScript.Arguments.Named( "To" )
  29. 	intArgs = intArgs + 1
  30. End If
  31. If WScript.Arguments.Named.Exists( "T" ) Then
  32. 	strToCurr = WScript.Arguments.Named( "T" )
  33. 	intArgs = intArgs + 1
  34. End If
  35. If dblAmount = 0 And WScript.Arguments.Unnamed.Count = 1 Then
  36. 	dblAmount = WScript.Arguments.Unnamed(0)
  37. 	intArgs = intArgs + 1
  38. End If
  39. If intArgs <> 3 Then Syntax ""
  40.  
  41. ' Validate command line arguments
  42. If Not IsNumeric( dblAmount ) Then
  43. 	Syntax dblAmount & " is not a valid amount"
  44. End If
  45. If dblAmount <= 0 Then
  46. 	Syntax "Amount must be greater than 0"
  47. End If
  48. If Len( strFromCurr ) <> 3 Then
  49. 	Syntax strFromCurr & " is not a valid ISO 4217 currency abbreviation." & vbCrLf _
  50. 	     & "Look up valid currency codes at http://www.oanda.com/site/help/iso_code.shtml"
  51. End If
  52. If Len( strToCurr ) <> 3 Then
  53. 	Syntax strToCurr & " is not a valid ISO 4217 currency abbreviation." & vbCrLf _
  54. 	     & "Look up valid currency codes at http://www.oanda.com/site/help/iso_code.shtml"
  55. End If
  56.  
  57. ' Retrieve the exchange rate for these currencies
  58. dblConvert  = YahooTrade( strFromCurr, strToCurr )
  59. If dblConvert = 0 Then
  60. 	Syntax "Error retrieving exchange rate"
  61. Else
  62. 	' Format the screen output
  63. 	dblExch = FormatNumber( dblConvert * dblAmount, 2, True, False, False )
  64. 	WScript.Echo strFromCurr & " " & dblAmount & " = " & strToCurr & " " & dblExch
  65. End If
  66.  
  67.  
  68. Sub Syntax( errMsg )
  69. 	Dim StdIn, strMsg
  70. 	If errMsg <> "" Then WScript.Echo "Error: " & errMsg
  71. 	strMsg = "YahooFX.vbs,  Version 1.00" & vbCrLf _
  72. 	       & "Calculate exchange rates" & vbCrLf & vbCrLf _
  73. 	       & "Usage:   YAHOOFX.VBS [/Amount:]amount /From:icc /To:icc" _
  74. 	       & vbCrLf & vbCrLf _
  75. 	       & "Where:   /A:amount   The amount that has to be converted" _
  76. 	       & vbCrLf _
  77. 	       & "         /F:icc      ISO 4217 currency code for amount" _
  78. 	       & vbCrLf _
  79. 	       & "         /T:icc      ISO 4217 currency code to convert to" _
  80. 	       & vbCrLf & vbCrLf _
  81. 	       & "Notes:   [1] Switches may be abbreviated, " _
  82. 	       & "e.g. /A and /AMOUNT are identical" & vbCrLf _
  83. 	       & "         [2] Look up valid ISO 4217 currency codes at" _
  84. 	       & vbCrLf _
  85. 	       & "             http://www.oanda.com/site/help/iso_code.shtml" _
  86. 	       & vbCrLf _
  87. 	       & "         [3] This script uses Yahoo's currency pages " _
  88. 	       & "(http://finance.yahoo.com)" & vbCrLf _
  89. 	       & "             to retrieve the current exchange rate " _
  90. 	       & "for our two currencies, so" & vbCrLf _
  91. 	       & "             this script will be broken as soon as " _
  92. 	       & "Yahoo changes these pages." & vbCrLf _
  93. 	       & "         [4] The author of this script cannot " _
  94. 	       & "be held responsible for any" & vbCrLf _
  95. 	       & "             damage, direct nor consequential, " _
  96. 	       & "caused by the use of or" & vbCrLf _
  97. 	       & "             inability to use this script. " _
  98. 	       & "Do not make any financial decisions" & vbCrLf _
  99. 	       & "             based on the output of this script. " _
  100. 	       & "Always consult a ""second" & vbCrLf _
  101. 	       & "             source"" before making any decision." _
  102. 	       & vbCrLf _
  103. 	       & "             Use this script entirely at your own risk." _
  104. 	       & vbCrLf & vbCrLf _
  105. 	       & "Examples:" & vbCrLf _
  106. 	       & "    Convert 2000 US Dollars to Euros:" & vbCrLf _
  107. 	       & "        YAHOOFX.VBS /AMOUNT:2000 /FROM:USD /TO:EUR" & vbCrLf _
  108.            & "    Convert 1120 Danish Krones to Indian Rupees:" & vbCrLf _
  109. 	       & "        YAHOOFX.VBS 1120 /F:DKK /T:INR" & vbCrLf & vbCrLf _
  110. 	       & "Written by Rob van der Woude" & vbCrLf _
  111. 	       & "http://www.robvanderwoude.com"
  112. 	WScript.Echo strMsg
  113. 	WScript.Quit 1
  114. End Sub
  115.  
  116.  
  117. Function YahooTrade( myFromCurr, myToCurr )
  118. ' This function retrieves the exchange rate
  119. ' for any two currencies from finance.yahoo.com
  120. '
  121. ' Arguments:
  122. ' myFromCurr [string]  ISO 4217 3 letter code for the currency to convert from
  123. ' myToCurr   [string]  ISO 4217 3 letter code for the currency to convert to
  124. '
  125. ' Look up currency codes at http://www.oanda.com/site/help/iso_code.shtml
  126. '
  127. ' Returns:
  128. ' Conversion rate as number
  129. '
  130. ' Disclaimer:
  131. ' This script uses http://finance.yahoo.com to retrieve exchange rates.
  132. ' This script will break when Yahoo changes its web page layout or content.
  133. ' The author of this script cannot be held responsible for any damage, direct
  134. ' nor consequential, caused by the use of or inability to use this script.
  135. ' Do not make any financial decisions based on the output of this script.
  136. ' Always use a "second source" before making any decision.
  137. ' Use this script entirely at your own risk.
  138. '
  139. ' Written by Rob van der Woude
  140. ' http://www.robvanderwoude.com
  141. 	Dim colMatches, intLastSubMatch, objHTTP, objRE, strConversion
  142. 	Dim strDecimal, strMyAmount, strResponse, strURL, strUserAgent
  143.  
  144. 	' Get the locally used decimal delimiter
  145. 	strDecimal  = Replace( FormatNumber( 0, 1, True ), "0", "" )
  146.  
  147. 	' Retrieve Yahoo's web page containing the our currencies' exchange rate
  148. 	Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )
  149. 	strURL = "http://finance.yahoo.com/q?s=" _
  150. 	       & UCase( myFromCurr & myToCurr ) & "=X"
  151. 	objHTTP.Open "GET", strURL, False
  152. 	strUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows 98)"
  153. 	objHTTP.SetRequestHeader "UserAgent", strUserAgent
  154. 	objHTTP.Send
  155. 	strResponse = objHTTP.ResponseText
  156. 	Set objHTTP = Nothing
  157.  
  158. 	' Extract and return the exchange rate from
  159. 	' the web page; in case of error return 0
  160. 	Set objRE        = New RegExp
  161. 	objRE.Global     = False
  162. 	objRE.IgnoreCase = True
  163. 	objRE.Pattern    = ">Last Trade:(<[^>]+>)+([.0-9]+)<[^>]+>"
  164. 	Set colMatches   = objRE.Execute( strResponse )
  165. 	If colMatches.Count = 1 Then
  166. 		intLastSubMatch = colMatches.Item(0).Submatches.Count - 1
  167. 		strConversion   = colMatches.Item(0).Submatches( intLastSubMatch )
  168. 		If IsNumeric( strConversion ) Then
  169. 			' Convert the match from string to number,
  170. 			' using the local decimal delimiter
  171. 			strConversion = CDbl( Replace( strConversion, ".", strDecimal ) )
  172. 			YahooTrade = strConversion
  173. 		Else
  174. 			YahooTrade = 0
  175. 		End If
  176. 	Else
  177. 		YahooTrade = 0
  178. 	End If
  179. 	Set colMatches = Nothing
  180. 	Set objRE      = Nothing
  181. End Function
  182.  

page last modified: 2024-02-26; loaded in 0.0310 seconds