Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "crt.bi"
- #include "wshelper.bas"
- #include "inc\OpenSSL.bi"
- Declare Sub loadConfig()
- Declare function URLEncode(sStr as String) as String
- Declare Function getOldImageID(sBuff as String) as String
- const IDENTIFIER = "<lfm status=""ok""><artist><name>"
- 'Image IDs are always 32 characters long
- dim shared as zString*33 szBadID = "2a96cbd8b46e442fc41c2b86b821562f"
- dim as string sHost = "www.last.fm"
- dim as integer iPort = 443
- loadConfig()
- dim as integer iIDLen = len(szBadID)
- 'init wshelper
- hStart()
- 'Init SSL
- SSL_load_error_strings()
- ERR_load_SSL_strings()
- OpenSSL_add_ssl_algorithms()
- var SSL_MethTLS = TLSv1_2_client_method()
- var SSL_Context = SSL_CTX_new(SSL_MethTLS)
- ' Open connection to last.fm
- var hCliSock = hOpen()
- if hConnect(hCliSock, hResolve(sHost), iPort) = 0 then
- printf(!"Failed to connect to %s:%i\n", sHost, iPort)
- sleep:system
- end if
- 'create a new SSL object and set the Socket / Name
- var pSSL = SSL_new(SSL_Context)
- SSL_set_fd( pSSL , hCliSock)
- SSL_set_tlsext_host_name( pSSL , strptr(sHost) )
- if SSL_connect( pSSL ) < 0 then
- puts("Failed to secure connection")
- sleep:system
- end if
- dim as string sBuff ' For reading the file in
- dim as string sFile = dir("*.xml")
- while (sFile <> "")
- printf("Opening %s...", sFile)
- ' Open cache file
- open sFile for input as #1
- sBuff = space(lof(1))
- get #1,, sBuff
- close #1
- ' First verify this is the cache file for an artist bio
- dim as integer iNameStart = instr(sBuff, IDENTIFIER)
- if iNameStart > 0 then
- ' only update cache if image ID is placeholder image
- dim as string sOldImageID = getOldImageID(sBuff)
- if sOldImageID <> szBadID then
- print "Cache file already fixed"
- sFile = dir()
- continue while
- end if
- ' Get the artist name so we can request the actual artwork
- iNameStart += len(IDENTIFIER)
- dim as integer iNameEnd = instr(iNameStart, sBuff, "<")
- Dim as string sName = mid(sBuff, iNameStart, iNameEnd-iNameStart)
- ' Make HTTPS request
- print URLEncode(sName)'sName
- dim as string sURL = "/music/"+URLEncode(sName)
- dim as zstring*4096 szBuffer
- var iSz = sprintf( szBuffer, _
- "GET %s HTTP/1.1" !"\r\n" _
- "Host: %s:%i" !"\r\n" _
- "Connection: close" !"\r\n\r\n", _
- sUrl , sHost , iPort )
- if SSL_write(pSSL, strptr(szBuffer), iSz) <= 0 then
- puts("failed to send request")
- end if
- ' Download the HTML responce from the server
- dim as string sResponce
- do
- var iLen = SSL_Read(pSSL, strptr(szBuffer), 4095)
- if iLen <= 0 then exit do
- szBuffer[iLen] = 0
- sResponce += szBuffer
- loop
- print sResponce
- ' Locate image URL in the HTML document
- const IMAGE_FOLDER = "+images/"
- dim as integer iImgUrlIndex = instr(sResponce, IMAGE_FOLDER)
- if iImgUrlIndex < 0 then
- print "Failed to find image ID"
- sleep: system
- end if
- ' Extract the image ID from the HTML
- dim as integer iStartTrim = iImgUrlIndex+len(IMAGE_FOLDER)
- dim as integer iEndTrim = instr(iStartTrim, sResponce, """")
- dim as string sID = mid(sResponce, iStartTrim, iEndTrim-iStartTrim)
- print sID
- ' Replace all instances of placeholder artist image with new
- dim as integer iPos = instr(1, sBuff, szBadID)
- while iPos > 0
- ' Copy new ID over the placeholder ID in the file buff
- memcpy(@sBuff[iPos-1], strptr(sID), iIDLen)
- ' Find next instance of placeholder ID in file
- iPos = instr(iPos, sBuff, szBadID)
- wend
- ' Write updated cache file
- open sFile for output as #1
- put #1,, sBuff
- close #1
- else
- print "Not an artist bio file"
- end if
- sFile = dir()
- wend
- ' Free the socket
- SSL_free( pSSL )
- hClose( hCliSock )
- ' Free the SSL context
- SSL_CTX_free( SSL_Context )
- sleep
- function getOldImageID(sBuff as String) as String
- dim as integer iPos = instr(sBuff, ".png")
- iPos -= 32
- return mid(sBuff, iPos, 32)
- end function
- ' Search backwards for image name starting at extension
- function getOldImageIDOld(sBuff as String) as String
- dim as integer iEnd = instr(sBuff, ".png")
- dim as integer iStart = iEnd
- while sBuff[iStart-2] <> asc("/")
- iStart -= 1
- wend
- return mid(sBuff, iStart, iEnd-iStart)
- end function
- function URLEncode(sStr as String) as String
- dim as string sOut
- for i as integer = 0 to len(sStr)-1
- ' Check if is an alphanumeric character
- if (sStr[i] >= asc("0") AndAlso sStr[i] <= asc("9")) OrElse _
- (sStr[i] >= asc("A") AndAlso sStr[i] <= asc("Z")) OrElse _
- (sStr[i] >= asc("a") AndAlso sStr[i] <= asc("z")) then
- sOut += chr(sStr[i])
- else
- sOut += "%"+hex(sStr[i], 2)
- end if
- next i
- return sOut
- end function
- sub loadConfig()
- const PATH_CONST = "path="
- const ID_CONST = "placeholderid="
- Dim as string sDir = "C:\subsonic\lastfmcache\"
- if open ("config.ini" for input as #1) then
- print "Failed to load config file..."
- printf(!"Defaulting to %s\r\n", sDir)
- else
- dim as string sLine
- do
- line input #1, sLine
- if lcase(left(sLine, len(PATH_CONST))) = PATH_CONST then
- sDir = mid(sLine, len(PATH_CONST)+1)
- elseif lcase(left(sLine, len(ID_CONST))) = ID_CONST then
- szBadID = mid(sLine, len(ID_CONST)+1)
- end if
- loop until eof(1)
- end if
- if chdir(sDir) then
- print "Failed to find local directory"
- print sDir
- sleep:system
- end if
- end sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement