Originally, I attempted to utilize JQuery load in the following manner
$.get('https://www.google.com/search?q=btc+value', function(p) {
console.log(p);
});
Unfortunately, my progress was hindered by cross-origin rules.
While there are paid services available with APIs for this purpose, my intention was to achieve it without any financial cost. Consequently, I opted for a server-based approach using PowerBasic for the backend, coupled with the SocketTools Library.
#COMPILE EXE
#DIM ALL
#Include "pbcgi.inc"
#Include "C:\bas_src\socketTools\v9.5\inc\cstools9.inc"
Function PBMain () As Long
Local btc As String
Local html As String
html = httpGet("https://www.google.com/search?q=btc+value")
' Filtering out just the current BTC value
' By isolating the content between "Bitcoin =" and "United States Dollar"
btc = Remain$(html,"Bitcoin =")
btc = Extract$(btc,"United States Dollar")
btc = Trim$(btc)
writeCGI "{" + jsonPad("btc") + ":" + jsonPad(btc) + "," + jsonPad("error") + ":" + jsonPad("0") + "}"
END FUNCTION
'================================================================
' Retrieve the page and return it as a string
Function httpGet(ByVal URL As String) As String
If IsFalse( HttpInitialize($CSTOOLS9_LICENSE_KEY) ) Then
Function = "Unable to initialize socket library"
Exit Function
End If
Local hClient As Dword
Local lpszURL As STRINGZ * 4096
Local lpszBuffer As STRINGZ * 100000
Local httpContents As String
Local httpPort As Long
Local httpOptions As Dword
Local nResult As Long
If LCase$(Trim$(Left$(URL, 8))) = "https://" Then
httpPort = 443
httpOptions = %HTTP_OPTION_SECURE Or %HTTP_OPTION_REDIRECT
Else
httpPort = 80
httpOptions = %HTTP_OPTION_REDIRECT
End If
lpszURL = URL & Chr$(0)
hClient = HttpConnect(lpszURL, _
httpPort, _
%HTTP_TIMEOUT, _
httpOptions, _
%HTTP_VERSION_10)
If hClient = %INVALID_CLIENT Then
Function = "Could not connect to: " & URL
Exit Function
End If
HttpSetEncodingType(hClient, %HTTP_ENCODING_NONE)
nResult = HttpGetText(hClient, lpszURL, lpszBuffer, 100000)
If nResult = %HTTP_ERROR Then
Function = "Error retrieving file." + Str$(nResult)
Else
' Success
httpContents = lpszBuffer
Function = httpContents
End If
Call HttpDisconnect(hClient)
HttpUninitialize()
End Function
'================================================================
' Pad string for JSON return
Function jsonPad(jstr As String) As String
Local i As Long
Replace Chr$(10) With " " In jstr
Replace Chr$(13) With " " In jstr
Replace "\" With "\\" In jstr
Replace $Dq With "\" + $Dq In jstr
For i = 0 To 9
Replace Chr$(i) With " " In jstr
Next
Function = $Dq + Trim$(jstr) + $Dq
End Function
On my website, I invoke it via AJAX
function showBTC(){
$.ajax({
type: "POST",
url: "../cgi/btcGet.exe",
dataType: "json",
success: function(json){
if(json.error !== "0" ){
console.log(json.error);
return;
}
$("#btcValue").html("Current BTC value $" + json.btc + "<br><br>");
}
});
}
I understand that this may seem like a very specific solution, but this is how I have chosen to address it.
UPDATED ON 5/11/2020:
An easier method has been discovered through a Bitcoin API accessible at coindesk.com, simplifying the process significantly.