Resumable Download w/progress bar using wget (NO APIs!)
Posted: Mon Aug 06, 2012 8:17 pm
On a different forum I was asked about doing a resumable download. I provided an example here. They wanted to use wget.exe instead.
So I whipped up a quick example - no error checking is done. This is just an example of how to use wget and process its output to translate to a PB progress bar....
I suppose this might be easier for some than going the API route -- especially since it should be cross platform since it uses no API's
NOTE: On windows you must download the wget port and put it in the path.
So I whipped up a quick example - no error checking is done. This is just an example of how to use wget and process its output to translate to a PB progress bar....
I suppose this might be easier for some than going the API route -- especially since it should be cross platform since it uses no API's
NOTE: On windows you must download the wget port and put it in the path.
Code: Select all
;- Enumerations
;{
Enumeration ; Windows
#winDownload
EndEnumeration
Enumeration ; Gadgets
#percent
#btnCancel
EndEnumeration
Enumeration ; Regular Expressions
#rgxSize
#rgxCurrent
EndEnumeration
;}
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
#wget = "wget.exe" ; Provided wget is in the path.
CompilerCase #PB_OS_Linux
#wget = "/usr/local/bin/wget"
CompilerCase #PB_OS_MacOS
#PB_OS_Linux
CompilerDefault
CompilerError "Unknown OS"
CompilerEndSelect
Procedure GetSize(cString.s, nRegEx)
Protected Dim Found$(0)
Protected cFound.s = "", nSize = 0
If ExtractRegularExpression(nRegEx,cString,Found$())
cFound = Found$(0)
nSize = Val(cFound)
Select Right(cFound,1)
Case "M": nSize * 1024 * 1024
Case "K": nSize * 1024
EndSelect
EndIf
ProcedureReturn nSize
EndProcedure
Procedure DownloadFile( cURL.s, cFolder.s )
Protected nPercent.f, hProg, stderr.s
CreateRegularExpression(#rgxSize, "\b[0-9]+\b")
CreateRegularExpression(#rgxCurrent, "[0-9]+[KM]")
OpenWindow(#winDownload, 450, 200, 389, 69, "Downloading...", #PB_Window_TitleBar)
ProgressBarGadget(#percent, 5, 10, 380, 25, 0, 100, #PB_ProgressBar_Smooth)
ButtonGadget(#btnCancel, 130, 45, 135, 20, "Cancel")
hProg = RunProgram(#wget,"--continue --progress=bar "+URLEncoder(cURL),cFolder,#PB_Program_Read|#PB_Program_Open|#PB_Program_Error|#PB_Program_Hide)
While ProgramRunning(hProg)
Select WaitWindowEvent(10)
Case #PB_Event_Gadget
Select EventGadget()
Case #btnCancel
; NOTE: While dialog is presented, download continues, but display is not updated.
If MessageRequester("Downloading...","Are you sure you want to cancel?",#PB_MessageRequester_YesNo|#MB_DEFBUTTON2|#MB_SYSTEMMODAL)=#PB_MessageRequester_Yes
KillProgram(hProg)
Break
EndIf
EndSelect
EndSelect
If AvailableProgramOutput(hProg)
ReadProgramString(hProg) ; Consume any output (there seems to be a bug in this area)
EndIf
stderr = ReadProgramError(hProg)
If stderr <> ""
If FindString(stderr, "Length:")
nSize = GetSize( stderr,#rgxSize )
EndIf
If FindString(stderr, "%")
nCurrent = GetSize(stderr,#rgxCurrent)
nPercent = (nCurrent/nSize)*100
SetGadgetState(#percent, nPercent)
SetWindowTitle(#winDownload,"Downloading... "+StrF(nPercent,2)+"%")
EndIf
EndIf
Wend
CloseProgram(hProg)
CloseWindow(#winDownload)
FreeRegularExpression(#rgxCurrent)
FreeRegularExpression(#rgxSize)
EndProcedure
DownloadFile("http://www.purebasic.com/download/PureBasic_Demo.exe","c:\temp")