Thanks Titan.
I made a couple of changes to my previous code and made the gui a bit nicer. I added a quick option as demonstrated to allow the function to receive a url as its input. its a shame that urldownloadtofile is still so limited (should definitely be in the top 5 needed changes for the next verison of ahk)
Code:
#SingleInstance Force
Space := " "
KB := 1024**1, MB := 1024**2, GB := 1024**3
Gui, 1: Add, Button, x10 y10 w75 r1 -Multi -Wrap gBrowse Default, &Browse
Gui, 1: Add, Edit, x+10 y10 w430 r1 -Multi -Wrap vEdit, http://isohunt.com/download/29934468/pure+pwnage
Gui, 1: Add, Button, x+10 y10 w75 r1 -Multi -Wrap gRead Default, &Read
Gui, 1: Add, ListView, x10 y+20 w600 h400, File %Space%%Space%%Space%|Size
Gui, 1: Add, Text, x10 y470 w200 r1 -Multi -Wrap, Announce URLs:
Gui, 1: Add, Edit, x10 y+10 w295 h100 vAnnounce
Gui, 1: Add, Text, x+10 y470 w200 r1 -Multi -Wrap, Comments:
Gui, 1: Add, Edit, x315 y+10 w295 h100 vComment
Gui, 1: Show, AutoSize
Return
Browse:
Gui, 1: +OwnDialogs
FileSelectFile, Torrent,,, Select Torrent to read, Torrents (*.torrent)
If ErrorLevel
Return
GuiControl,, Edit, %Torrent%
Return
Read:
AnnounceURL := ""
Gui, 1: Submit, NoHide
TorrentRead(Edit, "Data", "Size", "Announce", "Comment")
LV_Delete()
Loop, %Data0%
{
If (Size%A_Index% < KB)
LV_Add("", Data%A_Index%, Size%A_Index% " Bytes")
Else If (Size%A_Index% >= KB) && (Size%A_Index% < MB)
LV_Add("", Data%A_Index%, Size%A_Index%//1024 " KB")
Else If (Size%A_Index% >= MB) && (Size%A_Index% < GB)
LV_Add("", Data%A_Index%, Round(Size%A_Index%/(1024*1024), 1) " MB")
Else If (Size%A_Index% >= GB)
LV_Add("", Data%A_Index%, Round(Size%A_Index%/(1024*1024*1024), 1) " GB")
}
Loop, %Announce0%
AnnounceURL .= Announce%A_Index% "`n"
GuiControl,, Announce, % SubStr(AnnounceURL, 1, StrLen(AnnounceURL)-1)
GuiControl,, Comment, %Comment%
Return
;#######################################################################################
; Torrent: Path to torrent
; Files: Variable to put names of torrent files into. Variable0 contains number of files
; Size: Variable to put sizes of files into. Variable0 contains number of files, same as above
; Announce: Variable to put announce url into. Variable0 contains number of urls
; TorrentComment: Variable to put the comment for the torrent into
TorrentRead(Torrent, Files="", Size="", Announce="", TorrentComment="")
{
Local Data,Name,Name1,Name2,StrOffset,FileName,Num,Loc,Loc1,URL,URL1,Tracker,RetComment,RetComment1,Bytes,Bytes1
If Torrent contains http,url,www
{
UrlDownloadToFile, %Torrent%, Torrent.torrent
FileRead, Data, Torrent.torrent
FileDelete, Torrent.torrent
}
Else
FileRead, Data, %Torrent%
RegExMatch(Data, "4:name([0-9]+):(.+?):", Name)
Name := Substr(Name2, 1, Name1)
If Announce
{
StrOffset := 1
Loop
{
If A_Index = 1
StrOffset := RegExMatch(Data, "announce([0-9]+):", URL, StrOffset)
Else
StrOffset := RegExMatch(Data, "([0-9]+):", URL, StrOffset)
Tracker := SubStr(Data, StrOffset+StrLen(URL), URL1)
If Tracker contains http,udp
{
Num++
%Announce%%Num% := Tracker, StrOffset := StrOffset+StrLen(URL)+URL1
}
Else If (Tracker = "announce-list")
StrOffset := StrOffset+StrLen(URL)+URL1
Else
Break
}
%Announce%0 := Num
}
If TorrentComment
StrOffset := RegExMatch(Data, "comment([0-9]+):", RetComment), %TorrentComment% := SubStr(Data, StrOffset+StrLen(RetComment), RetComment1)
StrOffset := 1, Num := ""
Loop
{
If Size
RegExMatch(Data, "6:lengthi([0-9]+)e", Bytes, StrOffset)
StrOffset := InStr(Data, "4:path", 0, StrOffset) + 6
If StrOffset = 6
{
If A_Index = 1
{
%Files%0 := 1, %Files%1 := Name
If Size
%Size%0 := %Files%0, %Size%1 := Bytes1
}
Else
{
%Files%0 := Num
If Size
%Size%0 := %Files%0
}
Return
}
FileName := Name
Loop
{
If (SubStr(Data, StrOffset, 1) = "e")
Break
StrOffset := RegExMatch(Data, "([0-9]+):", Loc, StrOffset)
FileName .= "\" SubStr(Data, StrOffset+StrLen(Loc), Loc1), StrOffset := StrOffset+StrLen(Loc)+Loc1
}
If FileName
{
Num++
%Files%%Num% := FileName, %Size%%Num% := Bytes1
}
}
}