Skip to content

Instantly share code, notes, and snippets.

@rornor
Last active December 10, 2015 07:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rornor/9b835d41a4257cab25c9 to your computer and use it in GitHub Desktop.
foobar2000, Biography View script that can be used to display artist biography and album review according allmusic.com
Cache = 1 ' set to 0 to turn caching off
Set Arg = WScript.Arguments
If Cache Then Set oXml = CreateObject("MSXML2.DOMDocument.6.0")
If Arg.Count <> 3 Then
WScript.Echo "Usage: cscript //nologo allmusic.vbs ""%album artist%"" ""%album%"" review|bio"
WScript.Quit()
Else
If Arg(0) <> "?" And Arg(1) <> "?" Then
id = md5(Arg(0) & Arg(1))
If Not CacheCheck Then
If Arg(2) = "skip" Then
CacheUpdate("")
Else
Dim albumLink, artistLink
If Len(Arg(1)) < 3 Then query = Arg(1) & " " & Arg(0) : Else query = Arg(1) End If
content = search(arg(2))
If content <> "" Then
WScript.Echo content
If Cache Then CacheUpdate content
End If
End If
End If
End If
End If
Function search(result)
Set html = CreateObject("HtmlFile")
html.write Request("http://www.allmusic.com/search/albums/" & Escape(query))
For Each row In html.getElementsByTagName("h4")
Set album_search = row.nextSibling
albumLink = album_search.firstChild.getAttribute("href")
If Match(album_search.innerText, Arg(1)) Then
If Match(album_search.nextSibling.innerText, Arg(0)) Then
With CreateObject("HtmlFile")
.write Request(albumLink)
Set div = .getElementsByTagName("p")(1).parentNode
review = div.innerText
reviewAuthor = div.previousSibling.innerText
artistLink = .getElementsByTagName("h3")(0).firstChild.getAttribute("href")
End With
If result = "bio" Then
With CreateObject("HtmlFile")
.write Request(artistLink & "\biography")
Set div = .getElementsByTagName("p")(2).parentNode
biography = div.innerText
biographyAuthor = div.previousSibling.innerText
search = biography & vbCrLf & vbCrLf & biographyAuthor
End With
Else
search = review & vbCrLf & vbCrLf & reviewAuthor
End If
Exit For
End If
End If
Next
End Function
Function CacheCheck
If Cache Then
Set oFS = CreateObject("Scripting.FileSystemObject")
If Not oFS.FileExists("foo_allmusic.xml") Then
oXml.loadXML "<?xml version='1.0' encoding='UTF-8'?><Items></Items>"
oXml.save "foo_allmusic.xml"
Else
oXml.load "foo_allmusic.xml"
Set nod = oXml.selectSingleNode("Items/Item[@Id='" & id & "']/" & Arg(2))
If Not nod Is Nothing Then
If Arg(2) <> "skip" Then WScript.Echo nod.text
CacheCheck = True
Else
Set nod = oXml.selectSingleNode("Items/Item[@Id='" & id & "']/skip")
If Not nod Is Nothing Then CacheCheck = True
End If
End If
End If
End Function
Sub CacheUpdate(t)
oXml.load "foo_allmusic.xml"
Set root = oXml.selectSingleNode("Items")
Set item = oXml.selectSingleNode("Items/Item[@Id='" & id & "']")
If item Is Nothing Then
Set item = oXml.createElement("Item")
item.setAttribute "Id", id
ArtistId = "mn0000000000" : AlbumId = "mw0000000000"
If TypeName(artistLink) = "String" Then ArtistId = Split(artistLink, "-")(UBound(Split(artistLink, "-")))
If TypeName(albumLink) = "String" Then AlbumId = Split(albumLink, "-")(UBound(Split(albumLink, "-")))
item.setAttribute "ArtistId", ArtistId
item.setAttribute "AlbumId", AlbumId
Set comment = oXml.createComment(Arg(0) & " // " & Arg(1))
item.appendChild comment
root.appendChild item
End If
Set data = oXml.createElement(Arg(2))
data.Text = t
item.appendChild data
oXml.save "foo_allmusic.xml"
End Sub
Function Request(URL)
Set HTTP = CreateObject("MSXML2.XMLHTTP")
On Error Resume Next
HTTP.open "GET", URL, False
HTTP.send ""
If Not CBool(Err.Number) Then resp = HTTP.responseText
On Error Goto 0
Request = resp
End Function
Function Match(s1, s2)
If InStr(LCase(Replace(s1, " ", "")), LCase(Replace(s2, " ", ""))) > 0 Or _
InStr(LCase(Replace(s2, " ", "")), LCase(Replace(s1, " ", ""))) > 0 Then
Match = True
Else Match = False End If
End Function
Function md5(s)
Set MDC = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
Set UTF = CreateObject("System.Text.UTF8Encoding")
hash = MDC.ComputeHash_2((UTF.GetBytes_4(s)))
For i = 1 To Lenb(hash)
md5 = md5 & LCase(Right("0" & Hex(Ascb(Midb(hash, i, 1))), 2))
Next
End Function
<!ELEMENT Items (Item+)>
<!ELEMENT Item ((review?, bio?) | (bio?, review?))>
<!ATTLIST Item
Id CDATA #REQUIRED
ArtistId CDATA #REQUIRED
AlbumId CDATA #REQUIRED
>
<!ELEMENT review (#PCDATA)>
<!ELEMENT bio (#PCDATA)>
@rornor
Copy link
Author

rornor commented Dec 28, 2012

Thread link

Example Biography View command:

for Album
cscript //nologo allmusic.vbs "%album artist%" "%album%" review

for Artist
cscript //nologo allmusic.vbs "%album artist%" "%album%" bio

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment