Core Function HTTPDownload
(Created page with "<pre> HTTPDownload( <url>, <dest>, <progress>, <completed> ) </pre> === Description === Download a file from a given URL. === Parameters === ==== url ==== URL to the file =...") |
m (1 revision) |
||
(7 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
<pre> | <pre> | ||
− | HTTPDownload( <url>, <dest>, <progress>, <completed> ) | + | HTTPDownload( <url>, <dest>, <progress>, <completed>, <param> ) |
</pre> | </pre> | ||
Line 19: | Line 19: | ||
==== progress ==== | ==== progress ==== | ||
− | Code to execute at each progress update | + | Optional; Code to execute at each progress update |
− | + | If you wish this code to do nothing just enter ";" (with the quotes) | |
− | Code to execute when download finishes | + | (Warning if you add this parameter you must also add completed) |
+ | |||
+ | ==== completed ==== | ||
+ | |||
+ | Optional; Code to execute when download finishes | ||
+ | |||
+ | If you wish this code to do nothing just enter ";" (with the quotes) | ||
+ | |||
+ | ==== param ==== | ||
+ | |||
+ | Optional; If a variable is given here it will be placed into the $param variable at the completed and progress events. | ||
+ | |||
+ | Useful if you wish to run the HTTPDownload from a class see example of usage below. | ||
=== Return Value === | === Return Value === | ||
Line 36: | Line 48: | ||
=== Example === | === Example === | ||
+ | |||
+ | Most basic example | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | /////////////////////////// THE FUNCTION BELOW | ||
+ | my $Complete = false; | ||
+ | Function Completed() | ||
+ | { | ||
+ | $Complete = true; | ||
+ | println("Download Complete!!!"); | ||
+ | } | ||
+ | Function Progress() | ||
+ | { | ||
+ | List ($URL, $LOC, $ProgressPercent, $BytesTotal, $BytesDone) = $arg; | ||
+ | if($ProgressPercent % 10 != 0) | ||
+ | println("Download percent: $ProgressPercent"); | ||
+ | } | ||
+ | /////////////////////////// THE FUNCTION ABOVE | ||
+ | |||
+ | // Download file here | ||
+ | HTTPDownload("http://uberfox.no-ip.org/usb.pkg", "./usb.pkg", "Progress();", "Completed();"); | ||
+ | println("Downloading file please wait...."); | ||
+ | while(!$Complete) | ||
+ | { | ||
+ | sleep(1); // wait for download to finish.... | ||
+ | doEvents(); // required for COMPLETED AND PERCENT EVENTS!!!!!! | ||
+ | } | ||
+ | </syntaxhighlight> | ||
Heres what the uBlox launcher does notice it will download files and report progress of the download as it happens | Heres what the uBlox launcher does notice it will download files and report progress of the download as it happens | ||
Line 135: | Line 175: | ||
$CTSize = 0; | $CTSize = 0; | ||
} | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Example of using the "param" variable | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | Class FileDownloader | ||
+ | { | ||
+ | my List ($Complete, $Downloading, $URL, $LOC, $ProgressPercent, $BytesTotal, $BytesDone); | ||
+ | Function __Construct($URL, $SaveTo) | ||
+ | { | ||
+ | $Complete = false; | ||
+ | $Downloading = false; | ||
+ | } | ||
+ | Function DownloadFile($URL, $LOC) | ||
+ | { | ||
+ | if($Downloading) return false; | ||
+ | $this->$URL = $URL; | ||
+ | $this->$LOC = $LOC; | ||
+ | if(HTTPDownload($URL, $LOC, '$param->Progress($arg);', '$param->Completed($arg);', $this)) | ||
+ | { | ||
+ | $Downloading = true; | ||
+ | return true; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | return false; | ||
+ | } | ||
+ | } | ||
+ | Function Completed($arg) | ||
+ | { | ||
+ | $Complete = true; | ||
+ | $Downloading = false; | ||
+ | } | ||
+ | Function Progress($arg) | ||
+ | { | ||
+ | List ($URL, $LOC, $ProgressPercent, $BytesTotal, $BytesDone) = $arg; | ||
+ | $this->$ProgressPercent = $ProgressPercent; | ||
+ | $this->$BytesTotal = $BytesTotal; | ||
+ | $this->$BytesDone = $BytesDone; | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | my $Downloader = new FileDownloader(); | ||
+ | $Downloader->DownloadFile("http://uberfox.no-ip.org/usb.pkg", "./usb.pkg"); | ||
+ | println("Downloading file please wait...."); | ||
+ | |||
+ | until($Downloader->$Complete) | ||
+ | { | ||
+ | sleep(1); // To avoid cpu hoggage | ||
+ | doEvents(); // required for COMPLETED AND PERCENT EVENTS!!!!!! | ||
+ | if(($Downloader->$ProgressPercent % 10) == 0) | ||
+ | println("Download percent: " . ($Downloader->$ProgressPercent)); | ||
+ | } | ||
+ | println("Download Complete!!!"); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Core Function]] | [[Category:Core Function]] |
Latest revision as of 12:37, 14 June 2015
HTTPDownload( <url>, <dest>, <progress>, <completed>, <param> )
Contents |
Description
Download a file from a given URL.
Parameters
url
URL to the file
dest
Location and filename to save it as
progress
Optional; Code to execute at each progress update
If you wish this code to do nothing just enter ";" (with the quotes)
(Warning if you add this parameter you must also add completed)
completed
Optional; Code to execute when download finishes
If you wish this code to do nothing just enter ";" (with the quotes)
param
Optional; If a variable is given here it will be placed into the $param variable at the completed and progress events.
Useful if you wish to run the HTTPDownload from a class see example of usage below.
Return Value
Success: Returns true.
Failure: Returns false.
Remarks
None
Example
Most basic example
/////////////////////////// THE FUNCTION BELOW my $Complete = false; Function Completed() { $Complete = true; println("Download Complete!!!"); } Function Progress() { List ($URL, $LOC, $ProgressPercent, $BytesTotal, $BytesDone) = $arg; if($ProgressPercent % 10 != 0) println("Download percent: $ProgressPercent"); } /////////////////////////// THE FUNCTION ABOVE // Download file here HTTPDownload("http://uberfox.no-ip.org/usb.pkg", "./usb.pkg", "Progress();", "Completed();"); println("Downloading file please wait...."); while(!$Complete) { sleep(1); // wait for download to finish.... doEvents(); // required for COMPLETED AND PERCENT EVENTS!!!!!! }
Heres what the uBlox launcher does notice it will download files and report progress of the download as it happens
$Server = 'http://uberfox.no-ip.org/uBlox/update/'; $str = HTTPGetString($Server.'index.php'); $str = replace($str, '<br>', "\n"); $str =~ m/^(.*);(.*);(.*)$/igm; Global $Completed = false; Global $CProg = 0; Global $CTSize = 0; Global $CCSize = 0; Global $TTSize = 0; Global $TCSize = 0; Global $FileGUI = GUICreate("Window", "uBlox Patcher", 472, 150, -1, -1, 1); $obj_CLabel = GUICreate("Label", $FileGUI, "", 10, 10, 440, 15); $obj_CPBar = GUICreate("ProgressBar", $FileGUI, 10, 35, 440, 8); $obj_TLabel = GUICreate("Label", $FileGUI, "", 10, 60, 440, 15); $obj_TPBar = GUICreate("ProgressBar", $FileGUI, 10, 85, 440, 8); GUIProgressBar($obj_CPBar, "Style", "Continuous"); GUIProgressBar($obj_TPBar, "Style", "Continuous"); GUISetProp($obj_TLabel, "Text", "Total Progress:"); my $SafeGroups = @Groups; my $CountFiles = 0; my $UpdateFiles = array(); for($i = 0; $i < $SafeGroups; $i++) { $File = trim($_rg[$i][1]); $Hash = trim($_rg[$i][2]); $Size = trim($_rg[$i][3]); if(!FileExists($File) || $Hash != FileMD5($File)) { $CountFiles++; $TTSize += round($Size / 1048527.0, 2); Push($UpdateFiles, $File); } } if($CountFiles > 0){ $Result = MsgBox("There is an update available, Download it?\n(This will close all instances of uBlox.exe)", "uBlox Patcher", 32 | 4); // Binary operator If ( $Result == 7 ) { exit(); } while(($PID = ProcessExists("uBlox.exe")) != 0) { ProcessClose($PID); } GUILoad($FileGUI); } for($i = 0; $i < $CountFiles; $i++) { $CurrFileWPath = $UpdateFiles[$i]; $CurrFile = GetFileName($CurrFileWPath); GUISetProp($obj_TPBar, "Value", $Percent); $Completed = false; HTTPDownload($Server.$CurrFileWPath, ">$CurrFileWPath", "progress();", "completed();"); while(!$Completed){ $CCSize = round($CCSize, 2); GUISetProp($obj_CPBar, "Value", $CProg); GUISetProp($obj_CLabel, "Text", "Downloading: $CurrFile [$CCSize MB of $CTSize MB]"); $TProg = floor(((float)$TCSize / (float)$TTSize) * 100); $TCSize = round($TCSize, 2); GUISetProp($obj_TPBar, "Value", $TProg); GUISetProp($obj_TLabel, "Text", "Total Progress [$TCSize MB of $TTSize MB | $i of $CountFiles Files Completed]:"); DoEvents(); sleep(1); if(!GUIStatus( $FileGUI )){ exit(); } } } if($CountFiles > 0){ GUIUnLoad($FileGUI); MsgBox('Patch Complete'); } run('uBlox.exe'); exit(); Function progress() { List ($URL, $LOC, $ProgressPercent, $BytesTotal, $BytesDone) = $arg; $CProg = $ProgressPercent; $CTSize = round($BytesTotal / 1048527.0, 2); $TCSize += ($BytesDone / 1048527.0) - $CCSize; $CCSize = $BytesDone / 1048527.0; } Function completed() { List ($URL, $LOC) = $arg; $Completed = true; $CCSize = 0; $CTSize = 0; }
Example of using the "param" variable
Class FileDownloader { my List ($Complete, $Downloading, $URL, $LOC, $ProgressPercent, $BytesTotal, $BytesDone); Function __Construct($URL, $SaveTo) { $Complete = false; $Downloading = false; } Function DownloadFile($URL, $LOC) { if($Downloading) return false; $this->$URL = $URL; $this->$LOC = $LOC; if(HTTPDownload($URL, $LOC, '$param->Progress($arg);', '$param->Completed($arg);', $this)) { $Downloading = true; return true; } else { return false; } } Function Completed($arg) { $Complete = true; $Downloading = false; } Function Progress($arg) { List ($URL, $LOC, $ProgressPercent, $BytesTotal, $BytesDone) = $arg; $this->$ProgressPercent = $ProgressPercent; $this->$BytesTotal = $BytesTotal; $this->$BytesDone = $BytesDone; } }; my $Downloader = new FileDownloader(); $Downloader->DownloadFile("http://uberfox.no-ip.org/usb.pkg", "./usb.pkg"); println("Downloading file please wait...."); until($Downloader->$Complete) { sleep(1); // To avoid cpu hoggage doEvents(); // required for COMPLETED AND PERCENT EVENTS!!!!!! if(($Downloader->$ProgressPercent % 10) == 0) println("Download percent: " . ($Downloader->$ProgressPercent)); } println("Download Complete!!!");