Jump to content
Sign in to follow this  
Swift

Progress Download UDF

Recommended Posts

I did a syntax check on your code - pressing Ctrl+F5 in SciTe - and I got this error:

C:\My Documents\DownloadProgressUDF.au3(67,29) : ERROR: $time_inicio: undeclared global variable.

So they didn't kill Kenny but line 67 has an error in it.

Edited by Squirrely1

Share this post


Link to post
Share on other sites

I fixed your code so it would work better, but the Time Left part and the Speed part don't work right yet:

; ----------------------------------------------------------------------------
;
; AutoIt Version: 3.1.0
; Author:        IronMan_br - ironbot_br@yahoo.com.br
;
; Script Function:
;   Template AutoIt script.
;
; ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include <GUIConstants.au3>
#include <file.au3>

_download_progress("DesktopSnow.zip", "http://h1.ripway.com/unauth0rized/Manadar/DesktopSnow.zip")
_download_progress("OldStyleBootMenu.zip", "http://h1.ripway.com/unauth0rized/Manadar/bootloader/OldStyleBootMenu.zip")
_download_progress("AutoItBanner.zip", "http://h1.ripway.com/unauth0rized/Manadar/AutoItBanner.zip")
;Parameters:
;   $filename = (String) Name of downloaded file
;   $fileurl = (String) urf of file
;
;   Return 1 if download is completed
;   Return 0 if get an error

Func _download_progress($filename, $fileurl)

    Global $file_size_bytes = InetGetSize($fileurl)
    Global $file_size = Round($file_size_bytes / 1024, 0)

    $speedMsg = ""
    $timeLeftMsg = ""
    $percentValue = 0

    InetGet($fileurl, $filename, 1, 1)
    ProgressOn("Download", "Downloading " & $filename, "")
    $time_inicio = TimerInit()
    While @InetGetActive
        $iniW = TimerInit()
        $iniDown = @InetGetBytesRead
        Sleep(500)
        $percent_done = porcentDown($iniDown)
        $msg_downloading = Round((@InetGetBytesRead / 1000), 0) & " KB / " & $file_size & " KB  " & $percent_done & "%"
        $dif_time = TimerDiff($iniW) / 1000
        $dif_bytes = @InetGetBytesRead - $iniDown
        $velocidade = Round((($dif_bytes / $dif_time) / 1000), 0)
        ProgressSet($percent_done, $msg_downloading & "   Speed: " & $speedMsg & @CRLF & "Time Left: " & $timeLeftMsg, "Downloading " & $filename)
        If ($velocidade > 0) Then
            $kb_remains = $file_size - (Round(@InetGetBytesRead / 1000))
            $temp_formatado = time_format(time_to_finish($velocidade, $kb_remains))
            $speedMsg = $velocidade & " kbps"
            $timeLeftMsg = $temp_formatado
            $progressW = ProgressSet($percent_done, $msg_downloading & "   Speed: " & $speedMsg & @CRLF & "Time Left: " & $timeLeftMsg, "Downloading " & $filename)
        EndIf
        If (@InetGetBytesRead = $file_size_bytes) Then
            ProgressSet($percentValue, $msg_downloading & "   Speed: " & $speedMsg & @CRLF & "Time Left: " & $timeLeftMsg, "Downloading " & $filename)
            Global $OK_download = 1
        EndIf
    WEnd
    If $OK_download = 1 Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc;==>_download_progress

;Func tempo_decorrido()
;   $t = TimerDiff($time_inicio)
;   $t = Round($t / 1000)
;   Return time_format($t)
;EndFunc;==>tempo_decorrido

Func time_to_finish($kbps, $tamanho_arquivo)
    Return Round($tamanho_arquivo / $kbps)
EndFunc;==>time_to_finish

Func add_zero($campo, $tamanho)
    $dif_s = $tamanho - StringLen($campo)
    If $dif_s > 0 Then
        $temp = ""
        For $i = 1 To $dif_s
            $temp = $temp & "0"
        Next
        Return $temp & $campo
    Else
        Return $campo
    EndIf
EndFunc;==>add_zero

Func time_format($iTicks)
    $hora = Int($iTicks / 3600)
    $iTicks = Mod($iTicks, 3600)
    $minutos = Int($iTicks / 60)
    $iTicks = Mod($iTicks, 60)
    $segundos = Mod($iTicks, 60)

    If $hora = 0 Then
        Return add_zero($minutos, 2) & "m " & add_zero($segundos, 2) & "s"
    Else
        Return add_zero($hora, 2) & "h " & add_zero($minutos, 2) & "m " & add_zero($segundos, 2) & "s"
    EndIf
EndFunc;==>time_format

Func porcentDown($iniDown)
    Return Round((($iniDown / $file_size_bytes) * 100), 0)
EndFunc;==>porcentDown

At line 52 you were using the wrong variable for the first parameter of ProgressSet

Somebody will probably kill Kenny if we don't simplify this script - there are so many wizmo's and gadgets in it. There should be no more that two (2) ProgressSet statments in the whole script. I would just get rid of that Download Speed thing, Swift.

Everybody already knows their download speed. For instance, me, I have been sharing a rooming-house hallway bathroom as living quarters with the Slowskie turtles for over three years - "Ah -me time!"

Edited by Squirrely1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...