Program XML/HTTP with VBScript

Program XML/HTTP with VBScript:

' amazon_price.vbs
' This VBScript prompts for an ASIN, and returns the price.
' Usage: double-click amazon_price.vbs, enter ASIN.

Sub AmazonQuery(ASIN_In)
  Dim SelectedText
  Dim MSXML
  Dim XMLURL
  Dim Loaded

  ' Make sure that some text is selected
  If ((Len(ASIN_In) = 0) Or (ASIN_In < " ")) Then
     WScript.Echo "Please enter an ASIN."
     Exit Sub
  End If

  ' Set Associate ID and Developer Token
  AffiliateTag = "insert associate tag"
  DeveloperToken = "insert developer token"

  ' Create an instance of the MSXML Parser
  Set MSXML = CreateObject("MSXML.DOMDocument")

  ' Set MSXML Options
  MSXML.Async = False
  MSXML.preserveWhiteSpace = False
  MSXML.validateOnParse = True
  MSXML.resolveExternals = False

  ' Form the request URL
  XMLURL = "http://xml.amazon.com/onca/xml3" + _
     "?t=" + AffiliateTag + _
     "&dev-t=" + DeveloperToken + _
     "&page=1" + _
     "&f=xml" + _
     "&mode=books" + _
     "&type=lite" + _
     "&AsinSearch=" + ASIN_In

  ' Issue the request and wait for the response
  Loaded = MSXML.Load(XMLURL)

  ' If the request is loaded successfully, continue
  If (Loaded) Then
     ' Look for the ErrorMsg tag
     Set XMLError = MSXML.SelectNodes("//ErrorMsg")

     ' If it exists, display the message and exit
     If XMLError.length > 0 Then
        WScript.Echo MSXML.SelectSingleNode("//ErrorMsg").text
        Exit Sub
     End If

     ' If there's no error, use XPath to get the product name
     ' and the price
     WScript.Echo "The price of " + _
       MSXML.SelectSingleNode("ProductInfo/Details/ProductName").text + _
          " is " + _
       MSXML.SelectSingleNode("ProductInfo/Details/OurPrice").text
  Else
     WScript.Echo "The service is not available."
  End If

End Sub

strASIN = InputBox("Please enter a product ASIN.")

AmazonQuery(strASIN)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: