Advertisement
Guest User

Untitled

a guest
May 10th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '' Begin New Last.fm Code
  2.  
  3. Type LastFM Alias "LastFM"
  4. Private:
  5.    m_artist(500) As WString * 1024
  6.    m_name(500) As WString * 1024
  7.    m_album(500) As WString * 1024
  8.    m_length(500) As UInteger
  9.    m_curtime(500) As Integer
  10.    m_entries As Integer
  11.    m_session As String
  12.    c_username As String
  13.    c_password As String
  14.    c_apihost As String
  15. Public:
  16.    Declare Constructor()
  17.    Declare Sub readConfig Alias "readConfig" ()
  18.    Declare Function getSessionKey Alias "getSessionKey" () As String
  19.    Declare Function setNowPlaying Alias "setNowPlaying" () As SOCKET
  20.    Declare Function scrobbleTrack Alias "scrobbleTrack" () As Integer
  21.    Declare Function submitData Alias "submitData" () As SOCKET
  22.    Declare Function saveScrobble Alias "saveScrobble" () As Integer
  23.    Declare Function dumpScrobbles Alias "dumpScrobbles" () As Integer
  24.    Declare Sub loadScrobbles Alias "loadScrobbles" ()
  25. End Type
  26.  
  27. Constructor LastFM() Export
  28.    this.readConfig()
  29.    printf(!"LastFM::LastFM(): username: %s, password: %s.\n", this.c_username, String(Len(this.c_password), "*"))
  30.    this.m_session = this.getSessionKey()
  31.    printf(!"LastFM::LastFM(): Last.fm login successful!\n")
  32. End Constructor
  33.  
  34. Sub LastFM.readConfig Alias "readConfig" () Export
  35.    Dim fd As Integer = FreeFile()
  36.    Open "lastfm_config.txt" For Input As #fd
  37.    Line Input #fd, this.c_username
  38.    Line Input #fd, this.c_password
  39.    Close #fd
  40. End Sub
  41.  
  42. Function LastFM.getSessionKey Alias "getSessionKey" () As String Export
  43.    Dim As Integer curtime = time_(NULL)
  44.     Dim As String authkey = MD5str(MD5str(this.c_password) & curtime)
  45.     Dim As ZString * 10000 response
  46.     Dim As String response_data, httpdata
  47.  
  48.    If this.c_username = "" Or this.c_password = "" Then Return ""
  49.    
  50.    printf(!"LastFM::getSessionKey(): Getting session key.\n")
  51.  
  52.     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)
  53.     hStart()
  54.     Dim s As SOCKET, addr As Integer
  55.     s = hOpen()
  56.     addr = hResolve("post.audioscrobbler.com")
  57.     hConnect(s, addr, 80)
  58.     hSend(s, strptr(httpdata), len(httpdata))
  59.     hReceive(s, strptr(response), 10000)
  60.     hClose(s)
  61.    
  62.     response_data = Mid(response, InStr(response, !"\r\n\r\n") + 4)
  63.    
  64.     If left(response_data,3) = !"OK\n" Then
  65.         printf(!"LastFM::getSessionKey(): Session key retreived:%s\n", Mid(response_data, 4, 32))
  66.         Function = Mid(response_data, 4, 32)
  67.     Else
  68.         printf(!"LastFM::getSessionKey(): Failed to authenticate (bad password?)\n")
  69.         Function = ""
  70.     End If
  71. End Function
  72.  
  73. Function LastFM.setNowPlaying Alias "setNowPlaying" () As SOCKET Export
  74.     Dim As Integer curtime = time_(NULL)
  75.     Dim As String authkey = MD5str(MD5str(lastfm_password) & curtime)
  76.     Dim As ZString * 10000 response
  77.     Dim As String response_data, httpdata, postdata
  78.    
  79.    Dim As Integer length = Int(FSOUND_Stream_GetLengthMs(stream)/1000)
  80.  
  81.     httpdata =  !"POST /np_1.2 HTTP/1.1\n" & _
  82.                     !"Host: post.audioscrobbler.com\n" & _
  83.                     !"User-Agent: PsyMP3" & "/" & PSYMP3_VERSION & !"\n"
  84.  
  85.     postdata =  "s=" & lastfm_sessionkey & "&" & _
  86.                     "a=" & percent_encode(mp3artistW) & "&" & _
  87.                     "t=" & percent_encode(mp3nameW) & "&" & _
  88.                     "b=" & percent_encode(mp3albumW) & "&" & _
  89.                     "l=" & length & "&" & _
  90.                     "n=&m="
  91.  
  92.     httpdata &= !"Content-Length: " & Len(postdata) & !"\n" & _
  93.                     !"Content-Type: application/x-www-form-urlencoded\n\n" & _
  94.                     postdata
  95. End Function
  96.  
  97. '' End Last.fm code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement