'$Console:Only
Const INTERNET_OPEN_TYPE_DIRECT
= 1
Const INTERNET_DEFAULT_HTTP_PORT
= 80 Const INTERNET_DEFAULT_HTTPS_PORT
= 443
Const INTERNET_SERVICE_HTTP
= 3
Const INTERNET_FLAG_SECURE
= &H00800000 Const INTERNET_FLAG_RELOAD
= &H80000000
Const HTTP_QUERY_CONTENT_LENGTH
= 5
Const CLIENT_ID
= "43b28a744c8947babd5766c3674e0a13" Const CLIENT_SECRET
= "965f8e02e48a48ad9063e89bf5b0995f" Const REDIRECT_URI
= "http://localhost:8888/"
'$Include:'httpserv.BI'
Login
'Print code
'ReDim As Long albumart(1 To 3)
'ReDim As String albumarturl(0)
Dim As Long songlength
, position
, cseconds
, cminutes
, chours
, dseconds
, dminutes
, dhours
token = GetToken
'Print token
'Print "refresh_token:"; refreshtoken
'Dim As String current: current = SpotifyAPI("https://api.spotify.com/v1/me/player/currently-playing?market=US", token)
playbackstate = SpotifyAPI("https://api.spotify.com/v1/me/player?market=US", token)
albumartstring = GetKey(playbackstate, "url")
'albumart(1) = OnlineImage(albumarturl(1))
'albumart(2) = OnlineImage(albumarturl(2))
'albumart(3) = OnlineImage(albumarturl(3))
If GetKey2
(playbackstate
, "is_playing") = "true" Then playing
= "Playing" Else playing
= "Paused" GetAllKey playbackstate, "name", names()
token = GetRefreshedToken
oldart = albumartstring
oldsong
= names
((UBound(names
))) albumart = OnlineImage(albumartstring)
Title "Playing - " + names
(UBound(names
)) + " by " + names
(2) songlength
= Val(GetKey
(playbackstate
, "duration_ms")) dseconds
= Int(songlength \
1000) Mod 60 dminutes
= Int((songlength \
(1000 * 60)) Mod 60) dhours
= Int((songlength \
(1000 * 60 * 60)) Mod 24) Print "Artist: "; names
(2) Print "Album : "; names
(3) position
= Val(GetKey
(playbackstate
, "progress_ms")) cseconds
= Int(position \
1000) Mod 60 cminutes
= Int((position \
(1000 * 60)) Mod 60) chours
= Int((position \
(1000 * 60 * 60)) Mod 24)
errorhandler:
Dim As String URL: URL
= "https://accounts.spotify.com/authorize?response_type=code&client_id=" + CLIENT_ID
+ "&scope=user-modify-playback-state%20user-read-playback-state%20user-read-currently-playing%20user-read-recently-played&state=cAzgzTDzXnVYqbAM&show_dialog=true&redirect_uri=" + REDIRECT_URI
ShellExecute
0, "open" + Chr$(0), URL
+ Chr$(0), 0, 0, SW_SHOWNORMAL
OAuth2ServerStart
outj
= Mid$(outj
, 1, Len(outj
) - 2) GetKey2 = outj
Dim As String URL: URL
= "https://accounts.spotify.com/api/token" server
= Mid$(server
, 1, InStr(server
, "/") - 1) server
= server
+ Chr$(0) Dim As Offset hInternet: hInternet
= InternetOpen
(0, INTERNET_OPEN_TYPE_DIRECT
, 0, 0, 0) Dim As Offset hConnect: hConnect
= InternetConnect
(hInternet
, Offset(server
), INTERNET_DEFAULT_HTTPS_PORT
, 0, 0, INTERNET_SERVICE_HTTP
, 0, 0) InternetCloseHandle hInternet
sessiontype
= "POST" + Chr$(0) accepttypes
= "application/json" + Chr$(0) Dim As Offset hRequest: hRequest
= HttpOpenRequest
(hConnect
, Offset(sessiontype
), Offset(apipath
), 0, 0, Offset(accepttypes
), INTERNET_FLAG_RELOAD
Or INTERNET_FLAG_SECURE
, 0) InternetCloseHandle hConnect
InternetCloseHandle hInternet
Dim As String opt: opt
= "grant_type=refresh_token&refresh_token=" + refreshtoken
Dim As String headers: headers
= "Authorization: Basic " + encodeBase64
(CLIENT_ID
+ ":" + CLIENT_SECRET
) + Chr$(13) + Chr$(10) + "Content-Type: application/x-www-form-urlencoded" + Chr$(0) InternetCloseHandle hRequest
InternetCloseHandle hConnect
InternetCloseHandle hInternet
If HttpQueryInfo
(hRequest
, HTTP_QUERY_CONTENT_LENGTH
, Offset(queryinfo
), Offset(querylen
), 0) <> TRUE
Then InternetCloseHandle hRequest
InternetCloseHandle hConnect
InternetCloseHandle hInternet
InternetReadFile hRequest
, Offset(szBuffer
), Len(szBuffer
) - 1, Offset(dwRead
) If dwRead
> 0 Then response
= response
+ Mid$(szBuffer
, 1, dwRead
) InternetCloseHandle hRequest
InternetCloseHandle hConnect
InternetCloseHandle hInternet
'refreshtoken = GetKey(response, "refresh_token")
GetRefreshedToken = GetKey(response, "access_token")
Dim As String URL: URL
= "https://accounts.spotify.com/api/token" server
= Mid$(server
, 1, InStr(server
, "/") - 1) server
= server
+ Chr$(0) Dim As Offset hInternet: hInternet
= InternetOpen
(0, INTERNET_OPEN_TYPE_DIRECT
, 0, 0, 0) Dim As Offset hConnect: hConnect
= InternetConnect
(hInternet
, Offset(server
), INTERNET_DEFAULT_HTTPS_PORT
, 0, 0, INTERNET_SERVICE_HTTP
, 0, 0) InternetCloseHandle hInternet
sessiontype
= "POST" + Chr$(0) accepttypes
= "application/json" + Chr$(0) Dim As Offset hRequest: hRequest
= HttpOpenRequest
(hConnect
, Offset(sessiontype
), Offset(apipath
), 0, 0, Offset(accepttypes
), INTERNET_FLAG_RELOAD
Or INTERNET_FLAG_SECURE
, 0) InternetCloseHandle hConnect
InternetCloseHandle hInternet
Dim As String opt: opt
= "grant_type=authorization_code&code=" + code
+ "&redirect_uri=http://localhost:8888/" Dim As String headers: headers
= "Authorization: Basic " + encodeBase64
(CLIENT_ID
+ ":" + CLIENT_SECRET
) + Chr$(13) + Chr$(10) + "Content-Type: application/x-www-form-urlencoded" + Chr$(0) InternetCloseHandle hRequest
InternetCloseHandle hConnect
InternetCloseHandle hInternet
If HttpQueryInfo
(hRequest
, HTTP_QUERY_CONTENT_LENGTH
, Offset(queryinfo
), Offset(querylen
), 0) <> TRUE
Then InternetCloseHandle hRequest
InternetCloseHandle hConnect
InternetCloseHandle hInternet
InternetReadFile hRequest
, Offset(szBuffer
), Len(szBuffer
) - 1, Offset(dwRead
) If dwRead
> 0 Then response
= response
+ Mid$(szBuffer
, 1, dwRead
) InternetCloseHandle hRequest
InternetCloseHandle hConnect
InternetCloseHandle hInternet
refreshtoken = GetKey(response, "refresh_token")
GetToken = GetKey(response, "access_token")
server
= Mid$(server
, 1, InStr(server
, "/") - 1) server
= server
+ Chr$(0) Dim As Offset hInternet: hInternet
= InternetOpen
(0, INTERNET_OPEN_TYPE_DIRECT
, 0, 0, 0) Dim As Offset hConnect: hConnect
= InternetConnect
(hInternet
, Offset(server
), INTERNET_DEFAULT_HTTPS_PORT
, 0, 0, INTERNET_SERVICE_HTTP
, 0, 0) InternetCloseHandle hInternet
sessiontype
= "GET" + Chr$(0) accepttypes
= "application/json" + Chr$(0) Dim As Offset hRequest: hRequest
= HttpOpenRequest
(hConnect
, Offset(sessiontype
), Offset(apipath
), 0, 0, Offset(accepttypes
), INTERNET_FLAG_RELOAD
Or INTERNET_FLAG_SECURE
, 0) InternetCloseHandle hConnect
InternetCloseHandle hInternet
Dim As String headers: headers
= "Authorization: Bearer " + token
+ Chr$(0) If HttpSendRequest
(hRequest
, Offset(headers
), -1, 0, 0) <> TRUE
Then InternetCloseHandle hRequest
InternetCloseHandle hConnect
InternetCloseHandle hInternet
If HttpQueryInfo
(hRequest
, HTTP_QUERY_CONTENT_LENGTH
, Offset(queryinfo
), Offset(querylen
), 0) <> TRUE
Then InternetCloseHandle hRequest
InternetCloseHandle hConnect
InternetCloseHandle hInternet
InternetReadFile hRequest
, Offset(szBuffer
), Len(szBuffer
) - 1, Offset(dwRead
) If dwRead
> 0 Then response
= response
+ Mid$(szBuffer
, 1, dwRead
) InternetCloseHandle hRequest
InternetCloseHandle hConnect
InternetCloseHandle hInternet
SpotifyAPI = response
jkey = JSON
jkey
= Mid$(jkey
, 1, Len(jkey
) - 1) GetKey = ""
GetKey = jkey
unchangejson = JSON
x = x + 1
unchangejson
= Mid$(unchangejson
, InStr(unchangejson
, Chr$(34) + keyname
+ Chr$(34)) + Len(keyname
)) jkey = unchangejson
jkey
= Mid$(jkey
, 1, Len(jkey
) - 1) ParseKey(x) = jkey
r$ = a
Const CRYPT_STRING_NOCRLF
= &H40000000 Const CRYPT_STRING_BASE64
= &H00000001 If CryptBinaryToString
(Offset(encode
), Len(encode
), CRYPT_STRING_BASE64
Or CRYPT_STRING_NOCRLF
, 0, Offset(lenEncoded
)) <> FALSE
Then encodeBase64 = ""
If CryptBinaryToString
(Offset(encode
), Len(encode
), CRYPT_STRING_BASE64
Or CRYPT_STRING_NOCRLF
, Offset(encoded
), Offset(lenEncoded
)) <> FALSE
Then encodeBase64
= Mid$(encoded
, 1, lenEncoded
) encodeBase64 = ""
Dim num_active_connections
If num_active_connections
< MAX_CONNECTIONS
Then num_active_connections = num_active_connections + 1
For i
= 1 To MAX_CONNECTIONS
If Connections
(i
).handle
= 0 Then Dim empty_connection
As connection_t
Connections(i) = empty_connection
Connections(i).handle = new_connection
For i
= 1 To MAX_CONNECTIONS
Get #Connections
(i
).handle
, , buf$
Connections(i).read_buf = Connections(i).read_buf + buf$
process_request i
http_error_complete:
code
= Mid$(code
, Len("?/code=") + 1) code
= Mid$(code
, 1, InStr(code
, "&state") - 1) code = ""
'$Include:'httpserv.BM'
hsession = InternetOpen(0, INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0)
httpsession
= InternetConnect
(hsession
, _Offset(URL
), INTERNET_DEFAULT_HTTPS_PORT
, 0, 0, INTERNET_SERVICE_HTTP
, 0, 0)
sessiontype
= "GET" + Chr$(0) httprequest
= HttpOpenRequest
(httpsession
, _Offset(sessiontype
), _Offset(location
), 0, 0, 0, INTERNET_FLAG_RELOAD
Or INTERNET_FLAG_SECURE
, 0)
sendrequest = HttpSendRequest(httprequest, 0, 0, 0, 0)
If dwRead
> 0 Then picfile
= picfile
+ Mid$(szBuffer
, 1, dwRead
) InternetCloseHandle hsession