Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for bookfind.vbs

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

  1. If WScript.Arguments.UnNamed.Count <> 1 Then Syntax
  2. If WScript.Arguments.Named.Count    > 1 Then Syntax
  3. blnTd = False
  4. If WScript.Arguments.Named.Count    = 1 Then
  5. 	If UCase( WScript.Arguments.Named( 0 ) ) = "/TD" Then
  6. 		blnTd = True
  7. 	Else
  8. 		Syntax
  9. 	End If
  10. End If
  11.  
  12. strISBN    = WScript.Arguments.UnNamed( 0 )
  13. strPgTitle = TitleFromHTML( "http://www.amazon.com/gp/product/" & strISBN & "/" )
  14. strPattern = "Amazon.com: (.*): Books: (.*)$"
  15. strTitle   = RegExpVal( strPattern, strPgTitle, 0 )
  16. strAuthor  = RegExpVal( strPattern, strPgTitle, 1 )
  17. If blnTd Then
  18. 	strMsg = strISBN & vbTab & strTitle & vbTab & strAuthor & vbCrLf
  19. Else
  20. 	strMsg = vbCrLf & "Title  : " & strTitle  _
  21. 	       & vbCrLf & "Author : " & strAuthor _
  22. 	       & vbCrLf & "ISBN   : " & strISBN
  23. End If
  24. WScript.Echo strMsg
  25.  
  26.  
  27. Function RegExpVal( strPattern, strString, idx )
  28. 	On Error Resume Next
  29. 	Dim regEx, Match, Matches, RetStr
  30. 	Set regEx        = New RegExp
  31. 	regEx.Pattern    = strPattern
  32. 	regEx.IgnoreCase = True
  33. 	regEx.Global     = True
  34. 	Set Matches      = regEx.Execute( strString )
  35. 	RegExpVal        = Matches( 0 ).SubMatches( idx )
  36. End Function
  37.  
  38.  
  39. Function TitleFromHTML( strURL )
  40. 	Set ie = CreateObject( "InternetExplorer.Application" )
  41.  	ie.Navigate strURL
  42. 	Do Until ie.ReadyState = 4
  43. 		WScript.Sleep 10
  44. 	Loop
  45. 	TitleFromHTML = ie.Document.Title
  46. 	ie.Quit
  47. End Function
  48.  
  49.  
  50. Sub Syntax
  51. 	strMsg = strMsg & vbCrLf & "BookFind.vbs,  Version 1.11"                            & vbCrLf _
  52. 	       & "Display book title and author name for the specified ISBN number."        & vbCrLf & vbCrLf _
  53. 	       & "Usage:  CSCRIPT  //NOLOGO  BOOKFIND.VBS  isbn  [ /TD ]"                   & vbCrLf & vbCrLf _
  54. 	       & "Where:  ""isbn"" is the ISBN (or ASIN) of the book to search for"         & vbCrLf _
  55. 	       & "        /TD    changes the output format to tab delimited"                & vbCrLf & vbCrLf _
  56. 	       & "Note:   This script uses Amazon's web site to look up author and title."  & vbCrLf _
  57. 	       & "        To be precise, the data is extracted from the title of the page"  & vbCrLf _
  58. 	       & "        with URL http://www.amazon.com/gp/product/ followed by the ISBN." & vbCrLf _
  59. 	       & "        That means this script will fail when Amazon changes the URLs."   & vbCrLf & vbCrLf _
  60. 	       & "Written by Rob van der Woude"                                             & vbCrLf _
  61. 	       & "http://www.robvanderwoude.com"
  62. 	Wscript.Echo( strMsg )
  63. 	Wscript.Quit( 1 )
  64. End Sub
  65.  

page last uploaded: 2016-12-15, 11:20