Last.fm track actions with authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Const username = "set_username" | |
Const password = "set_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 foo_lastfm_track.vbs ""%artist%"" ""%title%"" love|unlove|ban|unban|addTags|removeTag" | |
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) | |
If InStr("addTags;removeTag", action) Then | |
t = "t" & Split(action, "T")(1) | |
q = Trim(InputBox(vbCrLf & vbCrLf & "Enter tags:" & vbCrLf & "(a comma delimited list of a maximum 10 tags)", "Last.fm " & action , "")) | |
api_sig = md5("api_key" & api_key & "artist" & artist & "methodtrack." & action & "sk" & sk & t & q & "track" & track & sec) | |
If q <> "" Then artist = artist & "&" & t & "=" & q | |
End If | |
url = "http://ws.audioscrobbler.com/2.0/?method=track." & action &_ | |
"&artist=" & artist &_ | |
"&track=" & 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 & vbCrLf & "Applied action: " & action | |
End If | |
Else | |
WScript.Echo "Username: " & username & " is not valid." | |
End If | |
End If | |
Function Request(url, method) | |
Set HTTP = CreateObject("MSXML2.ServerXMLHTTP") | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment