AutoHotkey Community

It is currently May 27th, 2012, 10:38 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: January 1st, 2008, 9:29 pm 
Offline

Joined: April 22nd, 2007, 6:33 pm
Posts: 1833
This is an example of a simple use of the TorrentRead Function. It will allow the user to pick a section from (Anime,Books,Games,Movies,Music,Pictures,Software,TV Shows) and update every minute. Torrent information is available and it can download and open the torrent specified.

Thanks go to bmcclure.

Code:
#SingleInstance Force

Menu, Tray, NoStandard
Menu, Tray, Add, Show
Menu, Tray, Default, Show
Menu, Tray, Add, Exit

OnExit, Exit

AppName := "MininovaRSS"
VersionNumber := "1.00"
Interval := "60"
KB := 1024**1, MB := 1024**2, GB := 1024**3
Sections := "Anime,Books,Games,Movies,Music,Pictures,Software,TV Shows"
StringSplit, Sections, Sections, `,

;#######################################################################################

IfNotExist %AppName%.ini
IniWrite, 1, %AppName%.ini, Settings, Section

IniRead, Section, %AppName%.ini, Settings, Section, 1
Sections := ""
Loop, %Sections0%
{
   If (A_Index = Section)
   Sections .= Sections%A_Index% "||"
   Else
   Sections .= Sections%A_Index% "|"
}
If (SubStr(Sections, StrLen(Sections)-1, 2) != "||")
StringTrimRight, Sections, Sections, 1

Gui, 1: Add, ListView, x10 y10 w450 h450 vMainList Grid Count50 -LV0x10 -Multi NoSortHdr NoSort ReadOnly AltSubmit gMainList, |URL
LV_ModifyCol(1, "450 Left")
LV_ModifyCol(2, "0 Left")

Gui, 1: Add, DropDownList, x+10 y10 w100 vSection gSection AltSubmit, %Sections%

Gui, 1: Add, ListView, xp+0 y+20 w400 h200 vInfoList Grid Count2 -Multi -ReadOnly, FileName | Size
LV_ModifyCol(1, "300 Left")
LV_ModifyCol(2, "95 Left")

Gui, 1: Add, Text, xp+0 y+10 w400 r1 -Multi -Wrap, Trackers:
Gui, 1: Add, Edit, xp+0 y+10 w400 h75 vTrackers
Gui, 1: Add, Text, xp+0 y+20 w400 r1 -Multi -Wrap, Comments:
Gui, 1: Add, Edit, xp+0 y+10 w400 h60 vComments
Gui, 1: Add, Button, x10 y480 w75 r1 -Multi -Wrap Default gDownload, &Download
Gui, 1: Add, Text, x+20 yp+5 w200 r1 -Multi -Wrap vStatus
;Gui, 1: Show, AutoSize, %AppName% - v%VersionNumber%

GoSub, Section
GoSub, UpdateRSS
SetTimer, UpdateRSS, % Interval*1000   ;%
Return

;#######################################################################################

Section:
Gui, 1: Submit, NoHide
URL := "http://www.mininova.org/rss.xml?cat=" Section
IniWrite, %Section%, %AppName%.ini, Settings, Section
GoSub, UpdateRSS
Return

;#######################################################################################

MainList:
If (A_GuiEvent  = "Normal")
{
   Gui, 1: ListView, MainList
   WhichTorrent := ""
   LV_GetText(WhichTorrent, A_EventInfo, 2)
   If WhichTorrent
   {
      GuiControl,, Status, Loading...
      StringReplace, WhichTorrent, WhichTorrent, /tor/, /get/
      TorrentRead(WhichTorrent, "Data", "Size", "Tracker", "Comments")
      Gui, 1: ListView, InfoList
      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%//KB " KB")
         Else If (Size%A_Index% >= MB) && (Size%A_Index% < GB)
         LV_Add("", Data%A_Index%, Round(Size%A_Index%/(MB), 1) " MB")
         Else If (Size%A_Index% >= GB)
         LV_Add("", Data%A_Index%, Round(Size%A_Index%/(GB), 1) " GB")
      }
      AllTrackers := ""
      Loop, %Tracker0%
      AllTrackers .= Tracker%A_index% "`n"
      GuiControl,, Trackers, % SubStr(AllTrackers, 1, StrLen(AllTrackers)-1)   ;%
      GuiControl,, Comments, %Comments%
      GuiControl,, Status,
   }
}
Return

;#######################################################################################

Download:
GuiControl,, Status, Downloading...
Gui, 1: ListView, MainList
Highlighted := LV_GetNext()
LV_GetText(DownloadTorrent, Highlighted, 2)
StringReplace, DownloadTorrent, DownloadTorrent, /tor/, /get/
UrlDownloadToFile, %DownloadTorrent%, Torrent.torrent
Run, Torrent.torrent
GuiControl,, Status,
;FileDelete, Torrent.torrent
Return

;#######################################################################################

UpdateRSS:
GuiControl,, Status, Updating...
UrlDownloadToFile, %URL%, Update.txt
FileRead, XML, Update.txt
FileDelete, Update.txt

Gui, 1: ListView, MainList
Pos := InStr(XML, "<item>")
If !Pos
Pos := 1
LV_Delete()
Loop
{
   Pos := RegExMatch(XML, "<title>(.+?)</title>", Title, Pos)
   RegExMatch(XML, "<link>(.+?)</link>", Source, Pos)
   Pos += StrLen(Title)
   If (!Title1)
   Break
   LV_Add("", Title1, Source1)
   LV_Add("", "")
}
GuiControl,, Status,
Return

;#######################################################################################

Show:
Gui, 1: Show, AutoSize, %AppName% - v%VersionNumber%
Return

;#######################################################################################

Exit:
FileDelete, Torrent.torrent
FileDelete, TorrentReadTorrent.torrent
FileDelete, Update.txt
ExitApp
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 SubStr(Torrent, 1, 4) contains http,url,www
   {
      UrlDownloadToFile, %Torrent%, TorrentReadTorrent.torrent
      FileRead, Data, TorrentReadTorrent.torrent
      FileDelete,  TorrentReadTorrent.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 .= Name ? "\" SubStr(Data, StrOffset+StrLen(Loc), Loc1) : SubStr(Data, StrOffset+StrLen(Loc), Loc1)
         StrOffset := StrOffset+StrLen(Loc)+Loc1
      }
      If FileName
      {
         Num++
         %Files%%Num% := FileName, %Size%%Num% := Bytes1
      }
   }
}


The function by itself:

Code:
; 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 SubStr(Torrent, 1, 4) contains http,url,www
   {
      UrlDownloadToFile, %Torrent%, TorrentReadTorrent.torrent
      FileRead, Data, TorrentReadTorrent.torrent
      FileDelete,  TorrentReadTorrent.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 .= Name ? "\" SubStr(Data, StrOffset+StrLen(Loc), Loc1) : SubStr(Data, StrOffset+StrLen(Loc), Loc1)
         StrOffset := StrOffset+StrLen(Loc)+Loc1
      }
      If FileName
      {
         Num++
         %Files%%Num% := FileName, %Size%%Num% := Bytes1
      }
   }
}


Last edited by tic on January 2nd, 2008, 10:57 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 1st, 2008, 9:55 pm 
Offline

Joined: November 24th, 2007, 9:07 pm
Posts: 774
This rocks; nice work, Tic!

And currently much more advanced than my Torrent library functions. I've cross-linked to this topic from mine to offer the alternative :wink:

_________________
Ben

My Trac projects
My Wiki
[Broken] - My music


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 1st, 2008, 10:55 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
MiniNova :D

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 2nd, 2008, 10:57 pm 
Offline

Joined: April 22nd, 2007, 6:33 pm
Posts: 1833
thanks bmcclure. As FileName0 and FileSize0 are currently the same (they both contain the number of files) do you think it would be better if instead of FileSize0 just being equal, it actually contained the total torrent size (so them all added together). Ill also quickly add an option to put the piece size into a var as well.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 12 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group