Jump to content
Sign in to follow this  
Nunos

How to get a result from an array

Recommended Posts

Nunos

I am trying to use the script that Storme made to download a file from Filehippo. I would just like to learn how to get one of the fields from the array so I can try to run it from a script. Basicaly it returns tons of information about the file it downloaded and offers you to copy a selected field. I would like to not display the array but instead get the filename that is in field 3 of the returned array. I looked through the beginners guide but I couldn't wrap my head around how to accomplish it. If someone could help I would appreciate it. The script is pasted below. Oh and if possible to keep that killer little progress bar thing that would be killer my attempts at making it do what I want I have removed that on accident :)

#include-once
#include <array.au3>
; #FUNCTION# ====================================================================================================================
; Name...........: _FileHippoDownload
; Description ...: Downloads the designated file from FileHippo and/or returns the latest verison number
; Syntax.........: _FileHippoDownload($sProgramTitle, $sDownloadFolder[, $sDownloadFileName = ""][, $bDownload = True])
; Parameters ....: $sProgramTitle    - the title of the program as it appears in the URL
;                                    - "malwarebytes_anti_malware" from [url="http://filehippo.com/download_malwarebytes_anti_malware/"]http://filehippo.com/download_malwarebytes_anti_malware/[/url]
;                 $sDownloadFolder   - the full path for the file to be downloaded to
;                 $sDownloadFileName - the filename of the file to be downloaded
;                                    - if filename = "" use filename from filehippo
;                 $bDownload         - [optional] True = program will be downloaded  False = return Version number ONLY
;                 $FunctionToCall    - [optional] A function which can update a Progressbar and react on UserInput like Click on Abort or Close App.
;                                    - If "$FunctionToCall" returns False abort download
;                                     -    Func _UpdateProgress($Percentage)
;                                     -    ProgressSet($Percentage,$Percentage &"%")
;                                     -    If _IsPressed("77") Then Return False ; Abort on F8
;                                     -    Return True ; ALL OK Continue download
;                                     -    Endfunc
; Return values .: Success - returns a 2D array containing technical details about the file (eg Filename, author, etc)
;                         - element [0][0] contains row count
;                 Failure - ""
;                  @error = 1 - Unable to download programs/description page
;                  @error = 2 - Unable to read programs/description page
;                  @error = 3 - Unable to read download page
;                  @error = 4 - Unable to read URL to download program
;                  @error = 5 - Unable to download program
;                  @error = 6 - Destination not reachable
;                  @error = 7 - Aborted by $FunctionToCall
;                  @error = 8 - $FunctionToCall doesn't exist
; Author ........: John Morrison aka Storm-E
; Modified.......: V2.0  Added details from TECH tab
;               : V2.1  Added create destination folder and error code if it can't be created @error = 6
;               :      Changed InetGet to work in the backgroud and added sleep to give script time to do things (eg for adlib)
;               :      Changed returned array to return row count in element 0,0
;               :      Added "Latest Version" number to array (element[1][1] (left it out in V2.0 OOPS
;               : V2.11 Added Progress function $FunctionToCall
;               : V2.12 Added check if $FunctionToCall function exists
;               : V2.121 Minor fix for properties that may not exist (Author HomePage) Thanks 'Chimaera'
; Remarks .......: Thanks to skin27 for your suggestion to add details and auto filename from filehippo
; Related .......:
; Link ..........:
; Example .......:
; ===============================================================================================================================
Func _FileHippoDownload($sProgramTitle, $sDownloadFolder, $sDownloadFileName = "", $bDownload = True, $FunctionToCall = "")
    Local $DownloadUrl, $asDownloadUrl
    Local $sCurrentVersion, $asCurrentVersion
    Local $sBaseSite = 'http://filehippo.com'
    Local $sBaseFolder = '/download_' & $sProgramTitle & '/'
    ;String trailing slash ""
    If StringRight($sDownloadFolder, 1) = "" Then StringLeft($sDownloadFolder, StringLen($sDownloadFolder) - 1)
    If DirCreate($sDownloadFolder) = 0 Then Return SetError(6, 0, "") ; destination unreachable
    ;Get source for program page
    Local $sPageSource = _GetSourceCode($sBaseSite & $sBaseFolder)
    If @error Then
        ;Failed to get programs page
        Return SetError(1, 0, "")
    EndIf
    ;Get download URL
    $asDownloadUrl = StringRegExp($sPageSource, '(?s)(?i)<a href="' & $sBaseFolder & 'download/(.*?)/">', 3)
    If @error Then
        Return SetError(2, 0, "")
    EndIf
    $_DownloadUrl1 = $asDownloadUrl[0]
    ;Get source for details page
    Local $sPageSource = _GetSourceCode($sBaseSite & $sBaseFolder & 'tech/')
    If @error Then
        ;Failed to get programs page
        Return SetError(1, 0, "")
    EndIf
    ;Get Details/description Table
    $asDescTable = StringRegExp($sPageSource, '(?s)(?i)<div class="desc">.*?<table>(.*?)</table>', 3)
    If @error Then
        Return SetError(2, 0, "")
    EndIf
    $sDescTable = $asDescTable[0]
    ;Split up table
    $asDescTable = StringRegExp($sDescTable, '(?s)(?i)<b>(.*?):</b>(.*?)', 3)
    If @error Then
        Return SetError(2, 0, "")
    EndIf
    ;Latest Version
<b>Malwarebytes Anti-Malware 1.51.1</b>

Old Versions
    $asCurrentVersion = StringRegExp($sPageSource, '(?s)(?i)Latest Version


Old Versions', 3)
    If @error Then
        Return SetError(2, 0, "")
    EndIf
    $sCurrentVersion = StringMid($asCurrentVersion[0], StringInStr($asCurrentVersion[0], " ", 0, -1) + 1)
    ;Convert to 2D array
    Dim $_FileDetails[UBound($asDescTable) / 2 + 2][2]
    $_FileDetails[0][0] = UBound($asDescTable) / 2 ; number of rows in array
    $_FileDetails[2][0] = $asDescTable[0]
    $_FileDetails[2][1] = $asDescTable[1]
    For $item = 2 To UBound($asDescTable) - 1 Step 2
        $_FileDetails[$item / 2 + 2][0] = $asDescTable[$item]
        $_FileDetails[$item / 2 + 2][1] = $asDescTable[$item + 1]
    Next
    ;Cleanup Author
    Local $iIndex = _ArraySearch($_FileDetails, "Author")
    If Not @error Then
    $_FileDetails[$iIndex][1] = StringLeft($_FileDetails[$iIndex][1], StringInStr($_FileDetails[$iIndex][1], "<") - 1)
    EndIf
    ;Cleanup HomePage
    $iIndex = _ArraySearch($_FileDetails, "HomePage")
    If Not @error Then
    $_FileDetails[$iIndex][1] = StringMid($_FileDetails[$iIndex][1], StringInStr($_FileDetails[$iIndex][1], "href=") + 6)
    $_FileDetails[$iIndex][1] = StringMid($_FileDetails[$iIndex][1], 1, StringInStr($_FileDetails[$iIndex][1], '"') - 1)
    EndIf
    ;Latest Version
<b>Malwarebytes Anti-Malware 1.51.1</b>

Old Versions
    $asCurrentVersion = StringRegExp($sPageSource, '(?s)(?i)Latest Version


Old Versions', 3)
    If @error Then
        Return SetError(2, 0, "")
    EndIf
    $_FileDetails[1][0] = "Latest Version"
    $_FileDetails[1][1] = StringMid($asCurrentVersion[0], StringInStr($asCurrentVersion[0], " ", 0, -1) + 1)
    If $bDownload Then
        ;</br(.*?)<></br(.*?)<></div></a><div class="desc"><a href="/download_malwarebytes_anti_malware/download/f9c81a8e689661f472f47aa7a0b12ada/"><img alt="Download" src="[url=" img="" url]?="" down5.png[="" cache.filehippo.com=""></a>
        ;Get source for download page
        $sPageSource = _GetSourceCode($sBaseSite & $sBaseFolder & 'download/' & $_DownloadUrl1) ; downlaod page
        If @error Then
            ;Failed to get programs page
            Return SetError(3, 0, "")
        EndIf
        ;<a id="_ctl0_contentMain_lnkURL" class="black" href="/download/file/de709ff1117ec419609d7fceecd86e625fe523b1385fa00515aa824249206a40/">If not then please click this link</a>
        $asDownloadUrl = StringRegExp($sPageSource, '(?s)(?i)href="/download/file/(.*?)/">', 3)
        If @error Then
            Return SetError(4, 0, "")
        EndIf
        $DownloadUrl = "/download/file/" & $asDownloadUrl[0] & "/"
        If $sDownloadFileName = "" Then
            ;Use FileHippo filename
            $sDownloadFileName = $_FileDetails[_ArraySearch($_FileDetails, "Filename")][1]
        EndIf
        ; DOWNLOAD FILE
        Local $iFileSize = InetGetSize($sBaseSite & $DownloadUrl, 1)
        Local $DownloadedSoFar = 0 ; how many bytes of the file have been downloaded
        Local $bRtn = True ; Progress function return OK
        Local $hDownload = InetGet($sBaseSite & $DownloadUrl, $sDownloadFolder & "" & $sDownloadFileName, 1, 1)
        Do
            If $FunctionToCall <> "" Then
                $bRtn = Call($FunctionToCall, Floor((InetGetInfo($hDownload, 0) / $iFileSize) * 100))
                If @error Then
                    InetClose($hDownload) ; Close the handle to release resourcs.
                    Return SetError(8, 0, "")
                EndIf
            EndIf
            Sleep(250)
        Until InetGetInfo($hDownload, 2) Or $bRtn = False ; Check if the download is complete.
        Local $nBytes = InetGetInfo($hDownload, 0)
        InetClose($hDownload) ; Close the handle to release resourcs.
        If @error Then
            Return SetError(5, 0, "")
        EndIf
        If $bRtn = False Then
            ;Download aborted by $FunctionToCall
            Return SetError(7, 0, "")
        EndIf
    EndIf
    Return $_FileDetails
 MsgBox(0, "Name", $_FileDetails[3])
EndFunc   ;==>_FileHippoDownload
Func _GetSourceCode($_Url)
    Local $_InetRead = InetRead($_Url)
    If Not @error Then
        Local $_BinaryToString = BinaryToString($_InetRead)
        If Not @error Then Return $_BinaryToString
    EndIf
EndFunc   ;==>_GetSourceCode


ProgressOn("FileHippo Download", "Downloading : malwarebytes_anti_malware")
$test = _FileHippoDownload("malwarebytes_anti_malware", @ScriptDir & "downloads", "", True, "_UpdateProgress")
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $test = ' & $test & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
ProgressOff()
_ArrayDisplay($test)
Exit
#include <misc.au3>
Func _UpdateProgress($Percentage)
    ProgressSet($Percentage, $Percentage & "%")
    If _IsPressed("77") Then Return False ; Abort on F8
    Return True ; bei 1 Fortsetzten
EndFunc   ;==>_UpdateProgress
Edited by Melba23
Added code tags

Share this post


Link to post
Share on other sites
water

Directly access the element in the array.

$aResult = _FileHippoDownload(...)
ConsoleWrite($aResult[3] & @CRLF)
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Nunos

ok so where would I insert that? Do I need to declare $aResult as a local or global variable first somewhere?

Thank you for teaching me :)

Share this post


Link to post
Share on other sites
Melba23

Nunos,

When you post code please use Code tags - put [autoit] before and [/autoit] after your posted code. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Nunos

Sorry I don't know much about html code either but I will learn it. :)

Nunos

Share this post


Link to post
Share on other sites
water

This is the line whre you call function _FileHippoDownload:

$test = _FileHippoDownload("malwarebytes_anti_malware", @ScriptDir & "\downloads\", "", True, "_UpdateProgress")

So $test is the array. Use $test[3] to access the 3rd element.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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  

×