Jump to content

Use WGET to Dl files


nobbe
 Share

Recommended Posts

hi

some people may find it useful - i do :)

; -----------------------------------------------------------------------------------------------
; runs WGET from scriptdir , saves file into TEMP and loads into a new variable
;
; by nobbe 2008


#include <GUIConstants.au3>
#include <String.au3>

#Region ### START Koda GUI section ### Form=

$GUI = GUICreate("Wget Sample", 612, 387, 193, 115)
$edit_input = GUICtrlCreateInput("http://www.google.de/", 16, 48, 449, 21)
$btn_download = GUICtrlCreateButton("Download", 480, 48, 75, 25, 0)

GUISetState(@SW_SHOW)
#EndRegion ### START Koda GUI section ### Form=




While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        
        ;; do a single Download ..
        Case $btn_download
            
            
            $dwnlink = GUICtrlRead($edit_input)

            $result_html = _download_this_url_with_wget($dwnlink, "http://localhost:8080/")
            MsgBox(0, "Result", $result_html)

            ;; close
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd


;
; downloads the give URL wth WGET
; if proxy setting is given, changes .wgetrc to http proxy
;
Func _download_this_url_with_wget($url, $proxy)
    
    Local $res_html = "";
    
    ; want to change proxy settings ? then write into ".wgetrc"..
    
    If $proxy <> "" Then
        Local $f = @ScriptDir & "\.wgetrc";
        Local $f_out = @ScriptDir & "\.wgetrc.out";
        
        $fh_in = FileOpen($f, 0)
        $fh_out = FileOpen($f_out, 2) ; write
        
        ; Check if file opened for reading OK
        If $fh_in = -1 Then
            ;MsgBox(0, "Error", "Unable to open file.")
            Return
        EndIf

        
        
        While 1
            $line = FileReadLine($fh_in)
            If @error = -1 Then ExitLoop
            
            ;
            $pos = StringInStr($line, 'http_proxy')
            If $pos > 0 Then

                FileWriteLine($fh_out, "http_proxy = " & $proxy & @CRLF);

            Else
                FileWriteLine($fh_out, $line); or regular line ?

            EndIf
        WEnd
        
        FileClose($fh_in)
        FileClose($fh_out)
        
        ; copy .wgetrc.out to .wgetrc
        FileMove($f_out, $f, 1); overwrite

    EndIf


    ;; now call WGET with output file
    $tmp_file = @ScriptDir & "\wget.tmp";
    FileDelete($tmp_file);
    
    $c = @ScriptDir & "\wget.exe --output-document=" & '"' & $tmp_file & '"' & ' "' & $url & '"' ; into temp file
    ;   ClipPut($c);
    ;   MsgBox(0, "WGET ", $c)
    $pid = RunWait($c, @ScriptDir, @SW_HIDE); @SW_MAXIMIZE) ; wait to finish , or we cant open result file
    
    $res_html = _LoadFiletoVar($tmp_file)
    ;   MsgBox(0, "Tmp file from WGET", $res_html)

    Return $res_html ;

EndFunc   ;==>_download_this_url_with_wget



; ====================================================================================================
; Description ..:
; Parameters ...:
; Return values :
; Author .......:
; Notes ........:
; ====================================================================================================
Func _LoadFiletoVar($which_file)
    Local $ret = "";
    Local $line;

    Local $file = FileOpen($which_file, 0) ; öffnen und lesen
    If $file = -1 Then
        ; MsgBox(0, "Error", "Unable to open file.")
        Return -1;
    EndIf

    While 1
        ; read only 1 line .. all info is in one line
        $line = FileReadLine($file)
        If @error = -1 Then ExitLoop
        
        $ret &= $line & @CRLF;
    WEnd

    FileClose($file)
    
    ;return var
    Return $ret;
    
EndFunc   ;==>_LoadFiletoVar
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...