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. Option Explicit
  2.  
  3. Dim arrWebPageTitle
  4. Dim blnTd
  5. Dim colMatches, objRE
  6. Dim strAuthor, strBookTitle, strISBN, strWebPageTitle
  7.  
  8.  
  9. With WScript.Arguments
  10. 	If .UnNamed.Count <> 1 Then Syntax
  11. 	If .Named.Count    > 1 Then Syntax
  12. 	If .Named.Count    = 1 And Not .Named.Exists( "TD" ) Then Syntax
  13. 	blnTd = ( .Named.Count = 1 )
  14. End With
  15.  
  16. strISBN         = WScript.Arguments.UnNamed( 0 )
  17. strBookTitle    = "Not found"
  18. strAuthor       = "Unknown"
  19. strWebPageTitle = TitleFromHTML( "https://www.amazon.com/dp/" & strISBN & "/" )
  20. strWebPageTitle = Replace( strWebPageTitle, "Amazon.com: ", "" )
  21. strWebPageTitle = Replace( strWebPageTitle, ": Books", "" )
  22. strWebPageTitle = Trim( strWebPageTitle )
  23. arrWebPageTitle = Split( strWebPageTitle, ":" )
  24. If UBound( arrWebPageTitle ) > 0 Then
  25. 	strBookTitle   = Trim( arrWebPageTitle(0) )
  26. 	strAuthor      = Trim( arrWebPageTitle(1) )
  27. 	Set objRE      = New RegExp
  28. 	objRE.Pattern  = "^[^\(]+"
  29. 	Set colMatches = objRE.Execute( strBookTitle )
  30. 	If colMatches.Count = 1 Then strBookTitle = colMatches(0).Value
  31. 	Set colMatches = Nothing
  32. 	Set objRE      = Nothing
  33. End If
  34.  
  35. If blnTd Then
  36. 	WScript.Echo strISBN & vbTab & strBookTitle & vbTab & strAuthor & vbCrLf
  37. Else
  38. 	WScript.Echo vbCrLf & "Title  : " & strBookTitle & vbCrLf & "Author : " & strAuthor & vbCrLf & "ISBN   : " & strISBN
  39. End If
  40.  
  41.  
  42. Sub IETerminate( )
  43. 	Dim colItems, objItem, objWMIService
  44. 	On Error Resume Next
  45. 	Set objWMIService = GetObject( "winmgmts://./root/CIMV2" )
  46. 	Set colItems      = objWMIService.ExecQuery( "SELECT * FROM Win32_Process WHERE Name = 'iexplore.exe'" )
  47. 	If colItems.Count > 0 Then
  48. 		For Each objItem In colItems
  49. 			objItem.Terminate
  50. 		Next
  51. 	End If
  52. 	Set colItems      = Nothing
  53. 	Set objWMIService = Nothing
  54. 	On Error GoTo 0
  55. End Sub
  56.  
  57.  
  58. Function IsIEActive( )
  59. 	Dim blnActive, colItems, objWMIService
  60. 	blnActive = False
  61. 	On Error Resume Next
  62. 	Set objWMIService = GetObject( "winmgmts://./root/CIMV2" )
  63. 	Set colItems      = objWMIService.ExecQuery( "SELECT * FROM Win32_Process WHERE Name = 'iexplore.exe'" )
  64. 	blnActive = ( colItems.Count > 0 )
  65. 	Set colItems      = Nothing
  66. 	Set objWMIService = Nothing
  67. 	IsIEActive = blnActive
  68. 	On Error GoTo 0
  69. End Function
  70.  
  71.  
  72. Function TitleFromHTML( strURL )
  73. 	Dim blnIEActive, objIE
  74. 	blnIEActive = IsIEActive ' Check if this will be the only IE running
  75. 	On Error Resume Next
  76. 	Set objIE = Nothing
  77. 	Set objIE = CreateObject( "InternetExplorer.Application" )
  78. 	If Err.number Then
  79. 		Err.Clear
  80. 		Set objIE = CreateObject( "InternetExplorer.Application" )
  81. 	End If
  82. 	If Err.number Then
  83. 		TitleFromHTML = "Not found by Unknown: Unknown: Amazon.com: Books"
  84. 	Else
  85. 	 	objIE.Navigate strURL
  86. 		Do Until objIE.ReadyState = 4
  87. 			WScript.Sleep 1
  88. 		Loop
  89. 		TitleFromHTML = objIE.Document.Title
  90. 		objIE.Quit
  91. 	End If
  92. 	Set objIE = Nothing
  93. 	On Error GoTo 0
  94. 	If Not blnIEActive Then IETerminate ' If this was the only IE running, terminate all IE processes
  95. End Function
  96.  
  97.  
  98. Sub Syntax
  99. 	Dim strMsg
  100. 	strMsg = strMsg _
  101. 	       & vbCrLf _
  102. 	       & "BookFind.vbs,  Version 1.12" _
  103. 	       & vbCrLf _
  104. 	       & "Display book title and author name for the specified ISBN number." _
  105. 	       & vbCrLf & vbCrLf _
  106. 	       & "Usage:  CSCRIPT  //NOLOGO  BOOKFIND.VBS  isbn  [ /TD ]" _
  107. 	       & vbCrLf & vbCrLf _
  108. 	       & "Where:  ""isbn"" is the ISBN (or ASIN) of the book to search for" _
  109. 	       & vbCrLf _
  110. 	       & "        /TD    changes the output format to tab delimited" _
  111. 	       & vbCrLf & vbCrLf _
  112. 	       & "Note:   This script uses Amazon's web site to look up author and title."  _
  113. 	       & vbCrLf _
  114. 	       & "        To be precise, the data is extracted from the title of the page"  _
  115. 	       & vbCrLf _
  116. 	       & "        with URL ""https://www.amazon.com/dp/"" followed by the ISBN." _
  117. 	       & vbCrLf _
  118. 	       & "        That means this script will fail if Amazon changes the URLs." _
  119. 	       & vbCrLf & vbCrLf _
  120. 	       & "Written by Rob van der Woude" _
  121. 	       & vbCrLf _
  122. 	       & "https://www.robvanderwoude.com"
  123. 	Wscript.Echo( strMsg )
  124. 	Wscript.Quit( 1 )
  125. End Sub
  126.  

page last modified: 2024-04-16; loaded in 0.0087 seconds