Skip to content

Instantly share code, notes, and snippets.

@rornor rornor/lastfm_track.vbs Secret
Created Dec 28, 2012

Embed
What would you like to do?
foobar2000, foo_run script that provides last.fm track specific actions
Const username = "<YOUR LASTFM USERNAME>"
Const password = "<YOUR LASTFM PASSWORD>"
Set REG = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
Const HKCU = &H80000001
Const h = "a2bb4e3245e4b53a81bf6de1bb96a0673077a7c2b4d32332c896236559f8c9d"
sec = Mid(h, 1, Len(h)/2) : api_key = Replace(h, sec, Chr(48))
Set XML = CreateObject("MSXML2.DOMDocument.6.0")
XML.async = False
Set ARG = WScript.Arguments
If ARG.Count <> 3 Then
WScript.Echo "Usage: wscript lastfm_track.vbs ""%artist%"" ""%title%"" love|unlove|ban|unban"
WScript.Quit()
Else
artist = ARG.Item(0) : track = ARG.Item(1) : action = ARG.Item(2)
XML.loadXML(Request("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&limit=1&api_key=" &_
api_key & "&user=" & username, "POST"))
If XML.selectSingleNode("lfm").getAttribute("status") = "ok" Then
authToken = md5(username & md5(password))
api_sig = md5("api_key" & api_key & "authToken" & authToken & "methodauth.getMobileSessionusername" & username & sec)
REG.GetStringValue HKCU, "Software\foo_lastfm_track", username, skVal
If Len(skVal) = 32 Then sk = skVal Else sk = auth End If
api_sig = md5("api_key" & api_key & "artist" & artist & "methodtrack." & action & "sk" & sk & "track" & track & sec)
url = "http://ws.audioscrobbler.com/2.0/?method=track." & action &_
"&artist=" & URLEncode(artist) &_
"&track=" & URLEncode(track) &_
"&api_key=" & api_key &_
"&api_sig=" & api_sig &_
"&sk=" & sk
XML.loadXML(Request(url, "POST"))
If XML.selectSingleNode("lfm").getAttribute("status") = "failed" Then
WScript.Echo XML.selectSingleNode("lfm/error").text
Else
WScript.Echo "Track: " & track & ", " & action & "-ed!"
End If
Else
WScript.Echo "Username: " & username & " is not valid."
End If
End If
Function Request(url, method)
Set HTTP = CreateObject("MSXML2.XMLHTTP")
HTTP.open method, url, False
HTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"
HTTP.setRequestHeader "User-Agent", _
"Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; TITAN X310e)"
HTTP.send ""
If Err.number <> 0 then
WScript.Echo "Error: " & HTTP.parseError.url & vbCrLf & HTTP.parseError.Reason
WScript.Quit()
End If
Request = HTTP.responseText
End Function
Function auth()
url = "https://ws.audioscrobbler.com/2.0/?method=auth.getMobileSession" &_
"&api_key=" & api_key &_
"&api_sig=" & api_sig &_
"&authToken=" & authToken &_
"&username=" & username
XML.loadXML(Request(url, "POST"))
If XML.selectSingleNode("lfm").getAttribute("status") = "ok" Then
auth = XML.selectSingleNode("lfm/session/key").text
REG.SetStringValue HKCU, "Software\foo_lastfm_track", username, auth
Else
WScript.Echo XML.selectSingleNode("lfm/error").text
WScript.Quit()
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
Function URLEncode(s)
For i = 1 To Len(s)
cc = Mid(s, i, 1) : c = Asc(cc)
If (c > 35 And c < 48) Or (c > 57 And c < 65) _
Or (c > 90 And c < 97) Or (c > 122 And c < 127) Then
o = o + "%" + Hex(c)
Else : o = o + cc : End If
Next
URLEncode = o
End Function
@rornor

This comment has been minimized.

Copy link
Owner Author

commented Dec 28, 2012

Thread: link

Example foo_run action:

love current track:
wscript foo_lastfm_track.vbs "%artist%" "%title%" love

Note: last.fm username and password has to be set in the script (first two lines)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.