Author Topic: Fun with libcurl....  (Read 2901 times)

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Fun with libcurl....
« on: October 29, 2018, 09:48:44 AM »
On the Bacon Website, Peter has an example of using libcurl to download a file over http, using the CURL module by Doyle Wisenant.

It works, but there is no progress indicator so you don't know how far in the download process you actually are.

I decided to add a progressbar to Peter's code; here is the code with a screenshot showing how it will look...

Code: Text
  1. '
  2. ' Download a binary file example over HTTP using CURL - PvE december 2010
  3. '
  4. ' Added progressbar callback, redirect support, and save filename - AIR october 2018
  5. '
  6.  
  7.  
  8. ' ADAPTED FROM: https://stackoverflow.com/questions/1637587/c-libcurl-console-progress-bar
  9. FUNCTION progress_callback(void* clientp, double dltotal, double dlnow, double ultotal, double ulnow )
  10.     LOCAL totaldotz, dotz, ii TYPE int
  11.     LOCAL fractiondownloaded TYPE double
  12.  
  13.     IF dltotal <= 0.0 THEN RETURN 0
  14.    
  15.     totaldotz = 40
  16.     fractiondownloaded = dlnow/dltotal
  17.     dotz = ROUND(fractiondownloaded * totaldotz)
  18.  
  19.     PRINT fractiondownloaded * 100 FORMAT "%3.0f%% [";
  20.  
  21.     WHILE ii < dotz DO
  22.         PRINT "=";
  23.         INCR ii
  24.     WEND
  25.  
  26.     WHILE ii < totaldotz DO
  27.         PRINT " ";
  28.         INCR ii
  29.     WEND
  30.  
  31.     PRINT (STRING)clientp FORMAT "] %s\r";
  32.  
  33.     RETURN 0
  34. END FUNCTION
  35.  
  36. ' Include the CURL context
  37. INCLUDE "curl.bac"
  38.  
  39. CONST filename$ = "documentation.pdf"
  40. CONST savefilename$ = "Bacon.pdf"
  41.  
  42. CLEAR
  43. PRINT "Downloading Bacon Documentation in PDF format...\n"
  44. ' File where we store the download
  45. OPEN savefilename$ FOR WRITING AS download
  46.  
  47. ' Create the handle
  48. curl = curl_easy_init()
  49.  
  50. ' Set the options
  51. curl_easy_setopt(curl, CURLOPT_URL, CONCAT$("http://www.basic-converter.org/", filename$))
  52. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,1)
  53. curl_easy_setopt(curl, CURLOPT_WRITEDATA, download)
  54. curl_easy_setopt(curl, CURLOPT_NOPROGRESS,0)
  55. curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION,progress_callback)
  56. curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, ADDRESS(savefilename$))
  57.  
  58. ' Perform the GET
  59. success = curl_easy_perform(curl)
  60.  
  61. ' Cleanup
  62. curl_easy_cleanup(curl)
  63.  
  64. ' Close filehandle
  65. CLOSE FILE download
  66.  
  67. PRINT "\n\nDownload Complete.\n"
  68.  

This was pretty easy to implement because Bacon is a phenomenal language!

Tested on both macOS and Linux (64bit Ubuntu Server and Raspbian on a RasPi).

For macOS, you'll need to change the name of the library in the curl.bac file from:

Just change the $lib= section towards the top of the curl.bac file to

Code: Text
  1. IF INSTR(OS$, "Darwin") THEN
  2.     lib$ = "libcurl.dylib"
  3. ELSE
  4.     lib$ = "libcurl.so"
  5. END IF

AIR.

« Last Edit: October 29, 2018, 10:05:25 AM by AIR »

Offline John

  • Forum Support / SB Dev
  • Posts: 3562
    • ScriptBasic Open Source Project
Re: Fun with libcurl....
« Reply #1 on: October 29, 2018, 09:53:15 AM »
By far BaCon is the best Linux/Unix BASIC compiler/translator available today.

Its syntax is an extension of Script BASIC which makes it a natural migration path to binary executables.
« Last Edit: October 29, 2018, 09:58:06 AM by John »