#include <GUIConstantsEx.au3>
#include <GUIListView.au3>
#include "ListViewProgress.au3"
Global $ahProgress[1], $ahInetGet[1][2]
Global $sURL = "http://gigablah.psiblade.net/Best%20Song%20Contest%202008/Main%20Event/"
Global $sMp3s_Path = @DesktopDir & "\Best Song Contest Mp3s"
$hGUI = GUICreate("Best Song Contest Downloader", 800, 700)
GUICtrlCreateLabel("MP3s Path:", 20, 8)
$nMp3Path_Input = GUICtrlCreateInput($sMp3s_Path, 80, 5, 700, 20)
$nMp3_List = GUICtrlCreateListView("Name|Progress|URL", 20, 30, 760, 640, $LVS_SHOWSELALWAYS)
$hMp3_List = GUICtrlGetHandle($nMp3_List)
_GUICtrlListView_SetColumnWidth($hMp3_List, 0, 450)
_GUICtrlListView_SetColumnWidth($hMp3_List, 1, 100)
_GUICtrlListView_SetColumnWidth($hMp3_List, 2, 200)
GUICtrlCreateLabel("Status:", 400, 675)
GUICtrlSetFont(-1, 9, 800)
GUICtrlSetColor(-1, 0xFF0000)
$nStatus_Label = GUICtrlCreateLabel("", 460, 675, 350, 20)
$nRefreshList_Button = GUICtrlCreateButton("Refresh List", 20, 675, 70, 20)
$nDownload_Button = GUICtrlCreateButton("Download Selected", 100, 675, 110, 20)
GUISetState(@SW_SHOW)
_SetMp3List_Proc($sURL)
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
Exit
Case $nRefreshList_Button
_SetMp3List_Proc($sURL)
Case $nDownload_Button
_DownloadMp3_Proc()
EndSwitch
WEnd
Func _SetMp3List_Proc($sURL)
_GUICtrlListView_DeleteAllItems($hMp3_List)
GUICtrlSetData($nStatus_Label, "Please wait...")
Local $sMp3_Links = InetRead($sURL)
If @error Then
Return SetError(1, 0, 0)
EndIf
$sMp3_Links = StringRegExpReplace(BinaryToString($sMp3_Links), '(?is).*<a href=".*">Parent Directory</a>', '')
Local $aMp3_Links = StringRegExp($sMp3_Links, '<a href="([^"]*)">', 3)
ReDim $ahProgress[UBound($aMp3_Links)]
For $i = 0 To UBound($aMp3_Links)-1
GUICtrlCreateListViewItem(_Encoding_HexToURL($aMp3_Links[$i]) & "||" & $sURL & $aMp3_Links[$i], $nMp3_List)
$ahProgress[$i] = _GUICtrlListView_ProgressCreate($hMp3_List, $i, 1)
_GUICtrlListView_ProgressSetColor($ahProgress[$i], 0x0000FF)
Next
GUICtrlSetData($nStatus_Label, "MP3s List Generated!")
EndFunc
Func _DownloadMp3_Proc()
Local $aSelected = _GUICtrlListView_GetSelectedIndices($hMp3_List, 1)
Local $sMp3_Url, $sMp3_Name, $aTmp
Dim $ahInetGet[$aSelected[0]+1][2] = [[$aSelected[0]]]
DirCreate($sMp3s_Path)
GUICtrlSetData($nStatus_Label, "Downloading...")
GUICtrlSetData($nDownload_Button, "Abort Selected")
GUICtrlSetState($nRefreshList_Button, $GUI_DISABLE)
Opt("GUIOnEventMode", 1)
GUICtrlSetOnEvent($nDownload_Button, "_AbortDownload_Proc")
For $i = 1 To $aSelected[0]
$sMp3_Name = _GUICtrlListView_GetItemText($hMp3_List, $aSelected[$i], 0)
$sMp3_Url = _GUICtrlListView_GetItemText($hMp3_List, $aSelected[$i], 2)
$ahInetGet[$i][0] = InetGet($sMp3_Url, $sMp3s_Path & "\" & $sMp3_Name, 0, 1)
$ahInetGet[$i][1] = $aSelected[$i]
Next
$iDownload_Counter = $ahInetGet[0][0]
While $ahInetGet[0][0] > 0
For $i = $ahInetGet[0][0] To 1 Step -1
If $ahInetGet[$i][0] = 0 Then
ContinueLoop
EndIf
$aInfo = InetGetInfo($ahInetGet[$i][0], -1)
If @error Then
ContinueLoop
EndIf
If $aInfo[2] = True Then
_GUICtrlListView_ProgressSetPos($ahProgress[$ahInetGet[$i][1]], 100)
_InetGet_ArrayDelete($ahInetGet[$i][0])
ExitLoop
EndIf
_GUICtrlListView_ProgressSetPos($ahProgress[$ahInetGet[$i][1]], 100 / ($aInfo[1] / $aInfo[0]))
Next
Sleep(1000)
WEnd
GUICtrlSetData($nStatus_Label, "Downloading Completed!")
GUICtrlSetData($nDownload_Button, "Download Selected")
GUICtrlSetState($nRefreshList_Button, $GUI_ENABLE)
Opt("GUIOnEventMode", 0)
EndFunc
Func _AbortDownload_Proc()
Local $aSelected = _GUICtrlListView_GetSelectedIndices($hMp3_List, 1)
For $i = $ahInetGet[0][0] To 1 Step -1
For $j = 1 To $aSelected[0]
If $ahInetGet[$i][1] = $aSelected[$j] Then
_GUICtrlListView_ProgressSetPos($ahProgress[$aSelected[$j]], 0)
InetClose($ahInetGet[$i][0])
_InetGet_ArrayDelete($ahInetGet[$i][0])
EndIf
Next
Next
EndFunc
Func _InetGet_ArrayDelete($vElement)
Local $aTmp[$ahInetGet[0][0]+1][2] = [[0]]
For $j = 1 To $ahInetGet[0][0]
If $ahInetGet[$j][0] <> $vElement Then
$aTmp[0][0] += 1
$aTmp[$aTmp[0][0]][0] = $ahInetGet[$j][0]
$aTmp[$aTmp[0][0]][1] = $ahInetGet[$j][1]
EndIf
Next
$ahInetGet = $aTmp
EndFunc
Func _Encoding_HexToURL($sURLHex)
Local $aURLHexSplit = StringSplit($sURLHex, "")
Local $sRetString = "", $iDec, $iUbound = UBound($aURLHexSplit)
For $i = 1 To $iUbound - 1
If $aURLHexSplit[$i] = "%" And $i + 2 <= $iUbound - 1 Then
$i += 2
$iDec = Dec($aURLHexSplit[$i - 1] & $aURLHexSplit[$i])
If Not @error Then
$sRetString &= Chr($iDec)
Else
$sRetString &= $aURLHexSplit[$i - 2]
EndIf
Else
$sRetString &= $aURLHexSplit[$i]
EndIf
Next
Return $sRetString
EndFunc