TRAP LOCAL
INCLUDE "curl.bac"
INCLUDE "Regex-Plus"
' We store our data in this variable
DECLARE data$, version_num$
DECLARE regx TYPE mREGEX_type
FUNCTION progress_callback(long clientp, double dltotal, double dlnow, double ultotal, double ulnow )
LOCAL totaldotz, dotz, ii TYPE int
LOCAL fractiondownloaded TYPE double
LOCAL tmpStr$
IF dltotal <= 0.0 THEN RETURN 0
tmpStr$ = CONCAT$((STRING)clientp,"")
totaldotz = 40
fractiondownloaded = dlnow/dltotal
dotz = ROUND(fractiondownloaded * totaldotz)
PRINT fractiondownloaded * 100 FORMAT "%3.0f%% [";
WHILE ii < dotz DO
PRINT "=";
INCR ii
WEND
WHILE ii < totaldotz DO
PRINT " ";
INCR ii
WEND
PRINT "] \r";
RETURN 0
END FUNCTION
FUNCTION Save_Data(STRING buffer$, size_t size, size_t nmemb, void *userp)
data$ = CONCAT$(data$, buffer$)
RETURN size*nmemb
END FUNCTION
SUB getData$(STRING url) TYPE STRING
LOCAL handle TYPE long
LOCAL success
handle = curl_easy_init()
curl_easy_setopt(handle, CURLOPT_URL, url)
curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1)
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, Save_Data)
success = curl_easy_perform(handle)
curl_easy_cleanup(handle)
END SUB
SUB downloadFile(STRING url, STRING downloadFileName)
LOCAL handle TYPE long
LOCAL success
PRINT "Downloading Latest 64Bit Firefox (",version_num$,") for Windows.\n"
handle = curl_easy_init()
OPEN downloadFileName FOR WRITING AS download
curl_easy_setopt(handle, CURLOPT_URL, url)
curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION,1)
curl_easy_setopt(handle, CURLOPT_WRITEDATA, download)
curl_easy_setopt(handle, CURLOPT_NOPROGRESS,0)
curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION,progress_callback)
success = curl_easy_perform(handle)
CLOSE FILE download
curl_easy_cleanup(handle)
PRINT "\n\nDownload Complete.\n"
END SUB
CLEAR
PRINT "Firefox Download Challenge (Bacon Version) by AIR.\n"
getData$("https://www.mozilla.org/en-US/firefox/new")
regx = RegEx(data$,"data-latest-firefox=.([0-9]+\\.[0-9]+)")
IF regx.count THEN
version_num$ = regx.result$[0]
PRINT version_num$ FORMAT "Firefox Setup %s.exe" TO version$
END IF
regx = RegEx(data$,".+href=\"(http.+latest-ssl&os=win64&lang=en-US)\"")
IF regx.count THEN
downloadLink$ = regx.result$[0]
END IF
downloadFile(downloadLink$, version$)
PRINT