Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '' Begin New Last.fm Code
- Type LastFM Alias "LastFM"
- Private:
- m_artist(500) As WString * 1024
- m_name(500) As WString * 1024
- m_album(500) As WString * 1024
- m_length(500) As UInteger
- m_curtime(500) As Integer
- m_entries As Integer
- m_session As String
- c_username As String
- c_password As String
- c_apihost As String
- Public:
- Declare Constructor()
- Declare Sub readConfig Alias "readConfig" ()
- Declare Function getSessionKey Alias "getSessionKey" () As String
- Declare Function setNowPlaying Alias "setNowPlaying" () As SOCKET
- Declare Function scrobbleTrack Alias "scrobbleTrack" () As Integer
- Declare Function submitData Alias "submitData" () As SOCKET
- Declare Function saveScrobble Alias "saveScrobble" () As Integer
- Declare Function dumpScrobbles Alias "dumpScrobbles" () As Integer
- Declare Sub loadScrobbles Alias "loadScrobbles" ()
- End Type
- Constructor LastFM() Export
- this.readConfig()
- printf(!"LastFM::LastFM(): username: %s, password: %s.\n", this.c_username, String(Len(this.c_password), "*"))
- this.m_session = this.getSessionKey()
- printf(!"LastFM::LastFM(): Last.fm login successful!\n")
- End Constructor
- Sub LastFM.readConfig Alias "readConfig" () Export
- Dim fd As Integer = FreeFile()
- Open "lastfm_config.txt" For Input As #fd
- Line Input #fd, this.c_username
- Line Input #fd, this.c_password
- Close #fd
- End Sub
- Function LastFM.getSessionKey Alias "getSessionKey" () As String Export
- Dim As Integer curtime = time_(NULL)
- Dim As String authkey = MD5str(MD5str(this.c_password) & curtime)
- Dim As ZString * 10000 response
- Dim As String response_data, httpdata
- If this.c_username = "" Or this.c_password = "" Then Return ""
- printf(!"LastFM::getSessionKey(): Getting session key.\n")
- httpdata = "GET /?hs=true&p=1.2.1&c=psy&v=" & PSYMP3_VERSION & "&u=" & this.c_username & "&t=" & curtime & "&a=" & authkey & " HTTP/1.1" & Chr(10) & "Host: post.audioscrobbler.com" & Chr(13) & Chr(10) & "User-Agent: PsyMP3/" & PSYMP3_VERSION & Chr(13) & Chr(10) & Chr (13) & Chr(10)
- hStart()
- Dim s As SOCKET, addr As Integer
- s = hOpen()
- addr = hResolve("post.audioscrobbler.com")
- hConnect(s, addr, 80)
- hSend(s, strptr(httpdata), len(httpdata))
- hReceive(s, strptr(response), 10000)
- hClose(s)
- response_data = Mid(response, InStr(response, !"\r\n\r\n") + 4)
- If left(response_data,3) = !"OK\n" Then
- printf(!"LastFM::getSessionKey(): Session key retreived:%s\n", Mid(response_data, 4, 32))
- Function = Mid(response_data, 4, 32)
- Else
- printf(!"LastFM::getSessionKey(): Failed to authenticate (bad password?)\n")
- Function = ""
- End If
- End Function
- Function LastFM.setNowPlaying Alias "setNowPlaying" () As SOCKET Export
- Dim As Integer curtime = time_(NULL)
- Dim As String authkey = MD5str(MD5str(lastfm_password) & curtime)
- Dim As ZString * 10000 response
- Dim As String response_data, httpdata, postdata
- Dim As Integer length = Int(FSOUND_Stream_GetLengthMs(stream)/1000)
- httpdata = !"POST /np_1.2 HTTP/1.1\n" & _
- !"Host: post.audioscrobbler.com\n" & _
- !"User-Agent: PsyMP3" & "/" & PSYMP3_VERSION & !"\n"
- postdata = "s=" & lastfm_sessionkey & "&" & _
- "a=" & percent_encode(mp3artistW) & "&" & _
- "t=" & percent_encode(mp3nameW) & "&" & _
- "b=" & percent_encode(mp3albumW) & "&" & _
- "l=" & length & "&" & _
- "n=&m="
- httpdata &= !"Content-Length: " & Len(postdata) & !"\n" & _
- !"Content-Type: application/x-www-form-urlencoded\n\n" & _
- postdata
- End Function
- '' End Last.fm code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement