Sample AMG Search script: Difference between revisions
		
		
		
		Jump to navigation
		Jump to search
		
| m (added link to search scripts documentation) |  (Updated source code to reflect some changes on AMG servers) | ||
| Line 52: | Line 52: | ||
|    ' The following HTML is taken from AMG web pages - so that we don't have to load the search page and can post the query directly |    ' The following HTML is taken from AMG web pages - so that we don't have to load the search page and can post the query directly | ||
|    html = "<form name=""search"" action=""http:// |   ' The following line will probably need some better way of retrieving server name from AMG. Using www.allmusicguide.com doesn't work at this moment. | ||
|    html = "<form name=""search"" action=""http://wm08.allmusic.com/cg/amg.dll"" method=""post"">" | |||
|    html = html & "<input type=""hidden"" name=""P"" value=""amg"" />" |    html = html & "<input type=""hidden"" name=""P"" value=""amg"" />" | ||
|    html = html & "<p><input type=""text"" name=""sql"" id=""search_txt"" />" |    html = html & "<p><input type=""text"" name=""sql"" id=""search_txt"" />" | ||
| Line 107: | Line 108: | ||
|    Set Doc2 = WB2Intf.Document |    Set Doc2 = WB2Intf.Document | ||
|    Set DivResults = Doc2. | |||
|    Set DivResults = Doc2.body | |||
|    Set Results = SDB.NewStringList |    Set Results = SDB.NewStringList | ||
|    Set FoundLinks = SDB.NewStringList |    Set FoundLinks = SDB.NewStringList | ||
|    If IsObject(DivResults) Then |    If IsObject(DivResults) And Not IsNull(DivResults) Then | ||
|      Set AColl = DivResults.getElementsByTagName("A") |      Set AColl = DivResults.getElementsByTagName("A") | ||
|      For Each itm In AColl |      For Each itm In AColl | ||
|        Set Imgs = itm.getElementsByTagName("img") |        Set Imgs = itm.getElementsByTagName("img") | ||
|        If  |       pos = InStr( CStr(itm.href), ".com/") | ||
|        If pos>0 Then | |||
|         href = Mid( itm.href, pos) | |||
|         If left(href,Len(".com/cg/amg.dll?p=amg&sql=10:")) = ".com/cg/amg.dll?p=amg&sql=10:" And Imgs.length=0 Then | |||
|           Results.Add itm.innerText | |||
|           FoundLinks.Add itm.href | |||
|         End If | |||
|        End If |        End If | ||
|      Next |      Next | ||
| Line 157: | Line 162: | ||
|      For Each itm In AColl |      For Each itm In AColl | ||
|        Set Imgs = itm.getElementsByTagName("img") |        Set Imgs = itm.getElementsByTagName("img") | ||
|        If  |       pos = InStr( CStr(itm.href), ".com/") | ||
|        If pos>0 Then | |||
|         href = Mid( itm.href, pos) | |||
|         If left(href,Len(".com/cg/amg.dll?p=amg&sql=33:")) = ".com/cg/amg.dll?p=amg&sql=33:" And Imgs.length=0 Then | |||
|           Tracks.Add itm.innerText | |||
|         End If | |||
|        End If |        End If | ||
|        If itm.className="subtitle" Then |        If itm.className="subtitle" Then | ||
Revision as of 17:28, 30 August 2007
This script demonstrates how to plug-in into MediaMonkey Web Search dialog. It searches AMG web a lets user tag some basic fields. Don't forget that it's rather a sample of what Search scripts can do than fully working script.
It currently searches AllMusicGuide's web for the given album, presents user results found and lets tag some basic fields:
- Album
- Artist
- Track titles
- Album art
There's a lot that could be improved, namely:
- Error handling - currently many possible problems aren't solved
- More fields could be tagged
- Results could be formatted using custom HTML page (not the downloaded one)
- Many others...
It's quite well documented, and so it can help in creation of your own Search scripts.
' Sample AMG Search script
'
' This script demonstrates how to plug-in into MediaMonkey Web Search dialog. You should save it to Scripts folder as
' SearchAMG.vbs. It has to be in Scripts.ini file, where entries can be as follows:
'
' [SearchAMG]
' FileName=SearchAMG.vbs
' ProcName=SearchAMG
' Order=10
' DisplayName=Search All Music Guide
' Language=VBScript
' ScriptType=3
Dim WB, WB2
Dim FoundLinks
Dim Tmr
' MediaMonkey calls this method whenever a search is started using this script
Sub StartSearch( Panel, SearchTerm, SearchArtist, SearchAlbum)
  Set UI = SDB.UI
  ' This is a web browser that we use to present results to the user
  Set WB = UI.NewActiveX( Panel, "Shell.Explorer")
  WB.Common.Align = 5      ' Fill whole client rectangle
  WB.Common.ControlName = "WB"
  ' This is a hidden browser that we use to find results (a better solution can be used, but this seems to be the easiest...)
  Set WB2 = UI.NewActiveX( Panel, "Shell.Explorer")
  WB2.Common.ControlName = "WB2"
  WB.Common.BringToFront
  ' The following HTML is taken from AMG web pages - so that we don't have to load the search page and can post the query directly
  ' The following line will probably need some better way of retrieving server name from AMG. Using www.allmusicguide.com doesn't work at this moment.
  html = "<form name=""search"" action=""http://wm08.allmusic.com/cg/amg.dll"" method=""post"">"
  html = html & "<input type=""hidden"" name=""P"" value=""amg"" />"
  html = html & "<p><input type=""text"" name=""sql"" id=""search_txt"" />"
  html = html & "<input type=""image"" src=""/i/pages/wide/go.gif"" id=""search_button"" /></p>"
  html = html & "<p><select name=""opt1"" id=""search_opt"">"
  html = html & "  <option value=""1"" selected=""selected"">Artist/Group</option>"
  html = html & "  <option value=""2"">Album</option>"
  html = html & "  <option value=""3"">Song</option>"
  html = html & "  <option value=""55"">Classical Work</option>"
  html = html & "</select></p>"
  html = html & "</form>"
  Set WB2Intf = WB2.Interf
  WB2Intf.Visible = false
  WB2.SetHTMLDocument html
  Set Doc2 = WB2Intf.Document
  Set SrchTxt = Doc2.getElementById("search_txt")
  If SearchAlbum<>"" Then
    SrchTxt.Value = SearchAlbum
  Else
    SrchTxt.Value = SearchTerm
  End If
  Set SrchType = Doc2.getElementById("search_opt")
  SrchType.selectedIndex = 1  ' Search for an album
  Set SrchButton = Doc2.getElementById("search_button")
  SrchButton.Click
  Set Tmr = SDB.CreateTimer( 40)
  Script.RegisterEvent Tmr, "OnTimer", "ContinueSearch"
End Sub
' We use this procedure as a callback using Timer, so that we can present results as soon as they are downloaded
Sub ContinueSearch( Timer)
  Script.UnregisterEvents Tmr
  Set Tmr = Nothing
  Set WB2Intf = WB2.Interf
  If Len(WB2Intf.LocationURL)<10 Then        ' A trick - wait until navigation to the search results page starts
    Set Tmr = SDB.CreateTimer( 40)
    Script.RegisterEvent Tmr, "OnTimer", "ContinueSearch"
    Exit Sub
  End If
  If (WB2Intf.ReadyState=1) Or WB2Intf.Busy Then
    Set Tmr = SDB.CreateTimer( 40)
    Script.RegisterEvent Tmr, "OnTimer", "ContinueSearch"
    Exit Sub
  End If
  Set Doc2 = WB2Intf.Document
  Set DivResults = Doc2.body
  Set Results = SDB.NewStringList
  Set FoundLinks = SDB.NewStringList
  If IsObject(DivResults) And Not IsNull(DivResults) Then
    Set AColl = DivResults.getElementsByTagName("A")
    For Each itm In AColl
      Set Imgs = itm.getElementsByTagName("img")
      pos = InStr( CStr(itm.href), ".com/")
      If pos>0 Then
        href = Mid( itm.href, pos)
        If left(href,Len(".com/cg/amg.dll?p=amg&sql=10:")) = ".com/cg/amg.dll?p=amg&sql=10:" And Imgs.length=0 Then
          Results.Add itm.innerText
          FoundLinks.Add itm.href
        End If
      End If
    Next
    SDB.Tools.WebSearch.SetSearchResults Results
    If Results.Count>0 Then
      SDB.Tools.WebSearch.ResultIndex = 0
    End If
  Else
    WB.SetHTMLDocument = Doc2.documentElement.innerHTML
  End If
End Sub
' This procedure is called by MediaMonkey when user selects some of search results
Sub ShowResult( ResultID)
  If ResultID>=0 And ResultID<FoundLinks.Count Then
    SDB.Tools.WebSearch.ClearTracksData   ' Tell MM to disregard any previously set tracks' data
    WB.SetHTMLDocument ""                 ' To prevent usage of this data
    WB.Interf.Navigate FoundLinks.Item(ResultID)
    Set Tmr = SDB.CreateTimer( 500)
    Script.RegisterEvent Tmr, "OnTimer", "ResultFullyLoaded"
  End If
End Sub
' This is a callback handled by a timer, so that we can respond as soon as album results are loaded
Sub ResultFullyLoaded( Timer)
  Script.UnregisterEvents Tmr
  Set Tmr = Nothing
  Set Tracks = SDB.NewStringList
  Set Doc = WB.Interf.Document
  If IsObject(Doc) Then
    ' Get track titles
    Set AColl = Doc.getElementsByTagName("A")
    For Each itm In AColl
      Set Imgs = itm.getElementsByTagName("img")
      pos = InStr( CStr(itm.href), ".com/")
      If pos>0 Then
        href = Mid( itm.href, pos)
        If left(href,Len(".com/cg/amg.dll?p=amg&sql=33:")) = ".com/cg/amg.dll?p=amg&sql=33:" And Imgs.length=0 Then
          Tracks.Add itm.innerText
        End If
      End If
      If itm.className="subtitle" Then
        ArtistTitle = itm.innerText
      End If
    Next
    ' Get album title
    Set SpanColl = Doc.getElementsByTagName("Span")
    For Each itm in SpanColl
      If itm.className="title" Then
        AlbumTitle = itm.innerText
      End If
    Next
    ' Get Album art URL
    Set ImgColl = Doc.getElementsByTagName("Img")
    For Each itm in ImgColl
      If left(itm.src,Len("http://image.allmusic.com/")) = "http://image.allmusic.com/" Then
        SDB.Tools.WebSearch.AlbumArtURL = itm.src
      End If
    Next
  End If
  If Tracks.Count=0 Then
    ' Nothing found yet, wait some more time
    Set Tmr = SDB.CreateTimer( 500)
    Script.RegisterEvent Tmr, "OnTimer", "ResultFullyLoaded"
  Else
    ' Some results were found, notify MediaMonkey
    SDB.Tools.WebSearch.SmartUpdateTracks Tracks
    For i=0 To SDB.Tools.WebSearch.NewTracks.Count-1
      SDB.Tools.WebSearch.NewTracks.Item(i).ArtistName = ArtistTitle
      SDB.Tools.WebSearch.NewTracks.Item(i).AlbumName = AlbumTitle
    Next
    SDB.Tools.WebSearch.RefreshViews   ' Tell MM that we have made some changes
  End If
End Sub
' This does the final clean up, so that our script doesn't leave any unwanted traces
Sub FinishSearch( Panel)
  ' Correctly terminate all the actions we have started
  WB.Common.DestroyControl
  WB2.Common.DestroyControl
  Set WB = Nothing
  Set WB2 = Nothing
  If IsObject(Tmr) Then
    Script.UnregisterEvents Tmr
    Set Tmr = Nothing
  End If
End Sub