Page 1 of 1

Console Download URL With Progress

Posted: Tue Apr 22, 2014 4:22 pm
by bgeraghty
Hi,

New to PB, with some background in C++ and Pascal, and just having a little fun as I learn. I took the example in this thread http://forum.purebasic.com/english/view ... hp?p=39370 and ported it to a console program. Compile it as wget.exe and run using the following syntax:

Code: Select all

wget.exe "http://url1.com/file.html" "http://url2.com/file2.html" ...
and so on...

You can supply an unlimited amount of URL's as parameters to the console program, and it will download each, one at a time, displaying a super-cool Text-Based Progress Bar. Kind of primitive, but it looks neat.

Image

Code Compiled in PB 5.22 LTS

Code: Select all

Global ArgC.l

Global StartLine = 0

;Procedure Area
Procedure DrawProgress(Percent.f)
  If Percent < 3
    ConsoleLocate(0,StartLine + 2)
    Print("[>                                       ] - " + Str(Percent) + "%")
  ElseIf Percent >= 3 And Percent <= 5
    ConsoleLocate(0,StartLine + 2)
    Print("[=>                                      ] - " + Str(Percent) + "%")
  ElseIf Percent >= 5 And Percent <= 7
    ConsoleLocate(0,StartLine + 2)
    Print("[==>                                     ] - " + Str(Percent) + "%")
  ElseIf Percent >= 7 And Percent <= 10
    ConsoleLocate(0,StartLine + 2)
    Print("[===>                                    ] - " + Str(Percent) + "%")
  ElseIf Percent >= 10 And Percent <= 13
    ConsoleLocate(0,StartLine + 2)
    Print("[====>                                   ] - " + Str(Percent) + "%")
  ElseIf Percent >= 13 And Percent <= 15
    ConsoleLocate(0,StartLine + 2)
    Print("[=====>                                  ] - " + Str(Percent) + "%")
  ElseIf Percent >= 15 And Percent <= 17
    ConsoleLocate(0,StartLine + 2)
    Print("[======>                                 ] - " + Str(Percent) + "%")
  ElseIf Percent >= 17 And Percent <= 20
    ConsoleLocate(0,StartLine + 2)
    Print("[=======>                                ] - " + Str(Percent) + "%")
  ElseIf Percent >= 20 And Percent <= 23
    ConsoleLocate(0,StartLine + 2)
    Print("[========>                               ] - " + Str(Percent) + "%")
  ElseIf Percent >= 23 And Percent <= 25
    ConsoleLocate(0,StartLine + 2)
    Print("[=========>                              ] - " + Str(Percent) + "%")
  ElseIf Percent >= 25 And Percent <= 27
    ConsoleLocate(0,StartLine + 2)
    Print("[==========>                             ] - " + Str(Percent) + "%")
  ElseIf Percent >= 27 And Percent <= 30
    ConsoleLocate(0,StartLine + 2)
    Print("[===========>                            ] - " + Str(Percent) + "%")
  ElseIf Percent >= 30 And Percent <= 33
    ConsoleLocate(0,StartLine + 2)
    Print("[============>                           ] - " + Str(Percent) + "%")
  ElseIf Percent >= 33 And Percent <= 35
    ConsoleLocate(0,StartLine + 2)
    Print("[=============>                          ] - " + Str(Percent) + "%")
  ElseIf Percent >= 35 And Percent <= 37
    ConsoleLocate(0,StartLine + 2)
    Print("[==============>                         ] - " + Str(Percent) + "%")
  ElseIf Percent >= 37 And Percent <= 40
    ConsoleLocate(0,StartLine + 2)
    Print("[===============>                        ] - " + Str(Percent) + "%")
  ElseIf Percent >= 40 And Percent <= 43
    ConsoleLocate(0,StartLine + 2)
    Print("[================>                       ] - " + Str(Percent) + "%")
  ElseIf Percent >= 43 And Percent <= 45
    ConsoleLocate(0,StartLine + 2)
    Print("[=================>                      ] - " + Str(Percent) + "%")
  ElseIf Percent >= 45 And Percent <= 47
    ConsoleLocate(0,StartLine + 2)
    Print("[==================>                     ] - " + Str(Percent) + "%")
  ElseIf Percent >= 47 And Percent <= 50
    ConsoleLocate(0,StartLine + 2)
    Print("[===================>                    ] - " + Str(Percent) + "%")
  ElseIf Percent >= 50 And Percent <= 53
    ConsoleLocate(0,StartLine + 2)
    Print("[=====================>                  ] - " + Str(Percent) + "%")
  ElseIf Percent >= 53 And Percent <= 55
    ConsoleLocate(0,StartLine + 2)
    Print("[======================>                 ] - " + Str(Percent) + "%")
  ElseIf Percent >= 55 And Percent <= 57
    ConsoleLocate(0,StartLine + 2)
    Print("[=======================>                ] - " + Str(Percent) + "%")
  ElseIf Percent >= 57 And Percent <= 60
    ConsoleLocate(0,StartLine + 2)
    Print("[========================>               ] - " + Str(Percent) + "%")
  ElseIf Percent >= 60 And Percent <= 63
    ConsoleLocate(0,StartLine + 2)
    Print("[=========================>              ] - " + Str(Percent) + "%")
  ElseIf Percent >= 63 And Percent <= 65
    ConsoleLocate(0,StartLine + 2)
    Print("[==========================>             ] - " + Str(Percent) + "%")
  ElseIf Percent >= 65 And Percent <= 67
    ConsoleLocate(0,StartLine + 2)
    Print("[===========================>            ] - " + Str(Percent) + "%")
  ElseIf Percent >= 67 And Percent <= 70
    ConsoleLocate(0,StartLine + 2)
    Print("[============================>           ] - " + Str(Percent) + "%")
  ElseIf Percent >= 70 And Percent <= 73
    ConsoleLocate(0,StartLine + 2)
    Print("[=============================>          ] - " + Str(Percent) + "%")
  ElseIf Percent >= 73 And Percent <= 75
    ConsoleLocate(0,StartLine + 2)
    Print("[==============================>         ] - " + Str(Percent) + "%")
  ElseIf Percent >= 75 And Percent <= 77
    ConsoleLocate(0,StartLine + 2)
    Print("[===============================>        ] - " + Str(Percent) + "%")
  ElseIf Percent >= 77 And Percent <= 80
    ConsoleLocate(0,StartLine + 2)
    Print("[================================>       ] - " + Str(Percent) + "%")
  ElseIf Percent >= 80 And Percent <= 83
    ConsoleLocate(0,StartLine + 2)
    Print("[=================================>      ] - " + Str(Percent) + "%")
  ElseIf Percent >= 83 And Percent <= 85
    ConsoleLocate(0,StartLine + 2)
    Print("[==================================>     ] - " + Str(Percent) + "%")
  ElseIf Percent >= 85 And Percent <= 87
    ConsoleLocate(0,StartLine + 2)
    Print("[===================================>    ] - " + Str(Percent) + "%")
  ElseIf Percent >= 87 And Percent <= 90
    ConsoleLocate(0,StartLine + 2)
    Print("[====================================>   ] - " + Str(Percent) + "%")
  ElseIf Percent >= 90 And Percent <= 93
    ConsoleLocate(0,StartLine + 2)
    Print("[=====================================>  ] - " + Str(Percent) + "%")
  ElseIf Percent >= 93 And Percent <= 95
    ConsoleLocate(0,StartLine + 2)
    Print("[======================================> ] - " + Str(Percent) + "%")
  ElseIf Percent >= 95 And Percent <= 97
    ConsoleLocate(0,StartLine + 2)
    Print("[=======================================>] - " + Str(Percent) + "%")
  ElseIf Percent >= 97 And Percent >= 99
    ConsoleLocate(0,StartLine + 2)
    Print("[========================================] - " + Str(Percent) + "%")
  EndIf
EndProcedure

Procedure.s GetQueryInfo(hHttpRequest, iInfoLevel)
  lBufferLength=0
  lBufferLength = 1024
  sBuffer.s=Space(lBufferLength)
  HttpQueryInfo_(hHttpRequest, iInfoLevel, sBuffer, @lBufferLength, 0)
  ProcedureReturn Left(sBuffer, lBufferLength)
EndProcedure

Procedure UrlToFileWithProgress(myFile.s, URL.s)
  isLoop.b=1
  Bytes=0
  fBytes=0
  Buffer=4096
  res.s=""
  tmp.s=""
  floater.f
  
  OpenType.b=1
  INTERNET_FLAG_RELOAD = $80000000
  INTERNET_DEFAULT_HTTP_PORT = 80
  INTERNET_SERVICE_HTTP = 3
  HTTP_QUERY_STATUS_CODE = 19
  HTTP_QUERY_STATUS_TEXT = 20
  HTTP_QUERY_RAW_HEADERS = 21
  HTTP_QUERY_RAW_HEADERS_CRLF = 22
  
  m0 = AllocateMemory(Buffer)
  
  Result = CreateFile(1, myFile)
  hInet = InternetOpen_("", OpenType, #Null, #Null, 0)
  hURL = InternetOpenUrl_(hInet, URL, #Null, 0, INTERNET_FLAG_RELOAD, 0)
  
  ;get Filesize
  domain.s = ReplaceString(Left(URL,(FindString(URL, "/",8) - 1)),"http://","")
  hInetCon = InternetConnect_(hInet,domain, INTERNET_DEFAULT_HTTP_PORT, #Null, #Null, INTERNET_SERVICE_HTTP, 0, 0)
  If hInetCon > 0
    hHttpOpenRequest = HttpOpenRequest_(hInetCon, "HEAD", ReplaceString(URL,"http://"+domain+"/",""), "http/1.1", #Null, 0, INTERNET_FLAG_RELOAD, 0)
    If hHttpOpenRequest > 0
      iretval = HttpSendRequest_(hHttpOpenRequest, #Null, 0, 0, 0)
      If iretval > 0
        tmp = GetQueryInfo(hHttpOpenRequest, HTTP_QUERY_STATUS_CODE)
        If Trim(tmp) = "200"
          tmp = GetQueryInfo(hHttpOpenRequest, HTTP_QUERY_RAW_HEADERS_CRLF)
          If FindString(tmp,"Content-Length:",1)>0
            ii=FindString(tmp, "Content-Length:",1) + Len("Content-Length:")
            tmp = Mid(tmp, ii, Len(tmp)-ii)
            myMax = Val(Trim(tmp))
          EndIf
        EndIf
      EndIf
    EndIf
  EndIf
  
  ConsoleLocate(0,StartLine)
  Print(URL)
  ConsoleLocate(0,StartLine + 2)
  Print("[                                        ]")

  ;start downloading
  Repeat
    InternetReadFile_(hURL, m0, Buffer, @Bytes)
    If Bytes = 0
      isLoop=0
    Else
      fBytes=fBytes+Bytes

      If myMax >= fBytes
        floater = fBytes / myMax

        DrawProgress(floater * 100)
        ConsoleLocate(0,StartLine + 1)
        ConsoleColor(8,0)
        Print("[ " + Str(fBytes) + " / " + Str(myMax) + " ]")
        ConsoleColor(7,0)
        Print(" Bytes Written.")
        
      EndIf
      
      WriteData(1,m0, Bytes)
      
    EndIf

  Until isLoop=0
  
  ConsoleLocate(0,StartLine + 2)
  Print("[========================================]")
  ConsoleColor(2,0)
  Print(" [Complete]")
  ConsoleColor(7,0)
  ConsoleColor(15,0)
  ConsoleLocate(0, StartLine + 2)
  Print("[========================================]")
  Delay(100)
  ConsoleColor(7,0)
  ConsoleLocate(0, StartLine + 2)
  Print("[========================================]")
  ConsoleColor(10,0)
  Print(" [Complete]")
  ConsoleColor(7,0)
  Delay(100)
  ConsoleLocate(0, StartLine + 2)
  Print("[========================================]")
  ConsoleColor(2,0)
  Print(" [Complete]")
  ConsoleColor(7,0)
  ConsoleLocate(0,StartLine + 1)
  ConsoleColor(8,0)
  Print("[ " + Str(fBytes) + " / " + Str(myMax) + " ]")
  ConsoleColor(7,0)
  Print(" Bytes Written.")
  StartLine = StartLine + 4
  ConsoleLocate(0,StartLine)
  
  
  InternetCloseHandle_(hURL)
  InternetCloseHandle_(hInet)
  CloseFile(1)   
  FreeMemory(m0)
  
EndProcedure

;Main Program
OpenConsole()
EnableGraphicalConsole(1)

ArgC = CountProgramParameters()

If ArgC < 1
  PrintN("Use:")
  PrintN("")
  PrintN("WGET.EXE [URL1] [URL2] [URL3] [URL4...")
  PrintN("")
Else
  ClearConsole()
  For i = 0 To ArgC - 1
    
    UrlToFileWithProgress(GetCurrentDirectory() + GetFilePart(ProgramParameter(i)), URLEncoder(ProgramParameter(i)))
  Next
EndIf
CloseConsole()
End

Re: Console Download URL With Progress

Posted: Tue Apr 22, 2014 5:39 pm
by Demivec
Welcome to PureBasic and the forum! :)
bgeraghty wrote:You can supply an unlimited amount of URL's as parameters to the console program, and it will download each, one at a time, displaying a super-cool Text-Based Progress Bar. Kind of primitive, but it looks neat.
You may run into problems with the 'unlimited amount' of parameters. IMHO, I believe a console program may hit a length limitation for parameters around 8000 characters total.


You can also shorten your program considerably by replacing lines 6 -> 128 with these lines:

Code: Select all

Procedure DrawProgress(Percent.f)
  x$ = Left(LSet(x$, Round((Percent - 1) * 41/99, #PB_Round_Nearest), "=") + ">" + Space(40), 40)
  ConsoleLocate(0,StartLine + 2)
  Print("[" + x$ + "] - " + Str(Percent) + "%")
EndProcedure

Re: Console Download URL With Progress

Posted: Thu Apr 24, 2014 2:36 pm
by bgeraghty
Demivec wrote:Welcome to PureBasic and the forum! :)
bgeraghty wrote:You can supply an unlimited amount of URL's as parameters to the console program, and it will download each, one at a time, displaying a super-cool Text-Based Progress Bar. Kind of primitive, but it looks neat.
You may run into problems with the 'unlimited amount' of parameters. IMHO, I believe a console program may hit a length limitation for parameters around 8000 characters total.


You can also shorten your program considerably by replacing lines 6 -> 128 with these lines:

Code: Select all

Procedure DrawProgress(Percent.f)
  x$ = Left(LSet(x$, Round((Percent - 1) * 41/99, #PB_Round_Nearest), "=") + ">" + Space(40), 40)
  ConsoleLocate(0,StartLine + 2)
  Print("[" + x$ + "] - " + Str(Percent) + "%")
EndProcedure

Hey! Thanks. I figured there would be a relatively easy way to programmatically draw the progress bar, but you beat me to the punch. Works great!