Sign in to follow this  
Followers 0
FuryCell

_TorrentGetFileNames($sFile)

14 posts in this topic

#1 ·  Posted (edited)

After fooling around and trying to figure out how to read Bencode I came up with a simple solution to extract the file names from a *.torrent file.

#include<String.au3>
#include<Array.au3>
Func _TorrentGetFileNames($sFile)
    Local $sData, $asBetween, $iCounter, $asReturn[1] = [0], $iPos
    $sData = FileRead($sFile)
    $asBetween = _StringBetween($sData, ":path", "eed6")
    $asBetween2 = _StringBetween($sData, ":name", "12")
    For $iCounter = 0 To UBound($asBetween) - 1
        $iPos = StringInStr($asBetween[$iCounter], ":", Default, -1)
        _ArrayAdd($asReturn, StringTrimLeft($asBetween[$iCounter], $iPos))
        $asReturn[0] += 1
    Next
    If $asReturn[0] = 0 Then
        For $iCounter = 0 To UBound($asBetween2) - 1
            $iPos = StringInStr($asBetween2[$iCounter], ":", Default)
            _ArrayAdd($asReturn, StringTrimLeft($asBetween2[$iCounter], $iPos))
            $asReturn[0] += 1
        Next
    EndIf
    Return $asReturn
EndFunc   ;==>_TorrentGetFileNames
Edited by FuryCell

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Heres a quick example.

#include<String.au3>
#include<Array.au3>
#include<File.au3>

;Download the torrent to a temp file
$Path = _TempFile(@TempDir, "~", ".torrent") 
InetGet("http://vodo.net/assets/torrents/Pioneer.One.S01E01.REFIX.Xvid-VODO.torrent", $Path)

;Extract file names and display them
$Array = _TorrentGetFileNames($Path)
_ArrayDisplay($Array)

;Remove the temp file used
FileDelete($Path) 

Func _TorrentGetFileNames($sFile)
    Local $sData, $asBetween, $iCounter, $asReturn[1] = [0], $iPos
    $sData = FileRead($sFile)
    $asBetween = _StringBetween($sData, ":path", "eed6")
    $asBetween2 = _StringBetween($sData, ":name", "12")
    For $iCounter = 0 To UBound($asBetween) - 1
        $iPos = StringInStr($asBetween[$iCounter], ":", Default, -1)
        _ArrayAdd($asReturn, StringTrimLeft($asBetween[$iCounter], $iPos))
        $asReturn[0] += 1
    Next
    If $asReturn[0] = 0 Then
        For $iCounter = 0 To UBound($asBetween2) - 1
            $iPos = StringInStr($asBetween2[$iCounter], ":", Default)
            _ArrayAdd($asReturn, StringTrimLeft($asBetween2[$iCounter], $iPos))
            $asReturn[0] += 1
        Next
    EndIf
    Return $asReturn
EndFunc   ;==>_TorrentGetFileNames

I would like to note that the torrent file in the example contains content that is liscened under the Creative Commons license and is not breaking the law in anyway.

Edited by FuryCell

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

Share this post


Link to post
Share on other sites

It's not returning the folders, here is what i came up with:

#include <Array.au3>

$Array = _TorrentGetFileNames("http://vodo.net/assets/torrents/Beyond.the.Game.720p.x264-VODO.torrent")
_ArrayDisplay($Array)

Func _TorrentGetFileNames($sFile)
    Local $aData, $aRet[1]
    $aData = StringSplit(BinaryToString(InetRead($sFile)), ':')
    
    For $i = 1 To $aData[0]
        If StringLeft($aData[$i], 4) = 'path' Then
            StringRegExpReplace($aData[$i+1], '^.*\.', '')
            
            If @extended = 0 Then ;it's a folder
                $aRet[0] += 1
                ReDim $aRet[$aRet[0]+1]
                $aRet[$aRet[0]] = StringRegExpReplace($aData[$i+1], '\d+$', '') & '|' & StringRegExpReplace($aData[$i+2], 'e\w+\d$', '')
            Else
                $aRet[0] += 1
                ReDim $aRet[$aRet[0]+1]
                $aRet[$aRet[0]] = StringRegExpReplace($aData[$i+1], 'e\w+\d$', '')
            EndIf
        EndIf
    Next
    
    Return $aRet
EndFunc

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

@MrCreatoR

Excellent. Thanks for the tweak (well rewrite pretty much). I really have to cave in and learn regular expressions.


HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

@FuryCell and @MrCreatoR

Are your both functions limited to vodo.net ?

The first torrent link I try return an array bigger than 10 thousand ! Posted Image

Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

I meant to post this awhile back:

Func encode($mixed)
    If IsArray($mixed) Then
        Return encode_array($mixed)
    ElseIf IsInt($mixed) Then
        Return encode_integer($mixed)
    Else
        Return encode_string($mixed)
    EndIf
EndFunc

Func encode_array($array)
    Local $return
    If UBound($array, 0) = 2 Then
        $return = 'd'
        For $i = 0 To UBound($array) - 1
            $return &= encode(String($array[$i][0])) & encode($array[$i][1])
        Next
    Else
        $return = 'l'
        For $i = 0 To UBound($array) - 1
            $return &= encode($array[$i])
        Next
    EndIf
    Return $return & 'e'
EndFunc

Func encode_integer($integer)
    Return 'i' & $integer & 'e'
EndFunc

Func encode_string($string)
    Return StringLen($string) & ':' & $string
EndFunc

Func decode($string)
    If FileExists($string) Then
        Local $fp = FileOpen($string, 16)
        Local $data = FileRead($fp)
        FileClose($fp)
    Else
        Local $data = $string
    EndIf
    Return decode_data($data)
EndFunc

Func decode_data(ByRef $data)
    Local $return
    Switch char($data)
        Case 'i'
            $data = BinaryMid($data, 2)
            $return = decode_integer($data)
        Case 'l'
            $data = BinaryMid($data, 2)
            $return = decode_list($data)
        Case 'd'
            $data = BinaryMid($data, 2)
            $return = decode_dictionary($data)
        Case Else
            $return = decode_string($data)
    EndSwitch
;~  ConsoleWrite(@error & @TAB & @extended & @CRLF)
    Return $return
EndFunc

Func decode_dictionary(ByRef $data)
    Local $dictionary
    Local $previous, $char = char($data), $key, $value
    While $char <> 'e'
        If $char = False Then Return SetError(1, 1,'')
        If Not Number($char) Then Return SetError(1, 2, '')
        $key = decode_string($data)
        $value = decode_data($data)
;~      ConsoleWrite($key & " = " & $value & @CRLF)
        If Not IsArray($dictionary) Then
            Local $dictionary[1][2] = [[$key, $value]]
        Else
            ReDim $dictionary[UBound($dictionary) + 1][2]
            $dictionary[UBound($dictionary) - 1][0] = $key
            $dictionary[UBound($dictionary) - 1][1] = $value
        EndIf
        $char = char($data)
    WEnd
    $data = BinaryMid($data, 2)
    Return $dictionary
EndFunc

Func decode_list(ByRef $data)
    Local $list
    Local $char = char($data)
    While $char <> 'e'
        If $char = False Then Return SetError(2, 1, '')
        If Not IsArray($list) Then
            Local $list[1] = [decode_data($data)]
        Else
            ReDim $list[UBound($list) + 1]
            $list[UBound($list) - 1] = decode_data($data)
        EndIf
        $char = char($data)
    WEnd
    $data = BinaryMid($data, 2)
    Return $list
EndFunc

Func decode_string(ByRef $data)
    If char($data) = '0'AND BinToStr(BinaryMid($data, 2, 1)) <> ':' Then Return SetError(3, 1, '')
    If Not StringInStr(BinToStr($data), ":") Then Return SetError(3, 2, '')
    Local $colon = StringInStr(BinToStr($data), ":")
    Local $length = Number(BinToStr(BinaryMid($data, 1, $colon)))
    If $length + $colon + 1 > BinaryLen($data) Then Return SetError(3, 3, '')
    Local $string = BinToStr(BinaryMid($data, $colon + 1, $length))
    $data = BinaryMid($data, $colon + $length + 1)
    Return $string
EndFunc

Func decode_integer(ByRef $data)
    Local $start = 1, $end = StringInStr(BinToStr($data), 'e')
    If $end = 0 Then Return SetError(4, 1, '')
    If char($data) = '-' Then $start += 1
    If BinToStr(BinaryMid($data, $start, 1)) = '0' AND ($start <> 1 OR $end > $start + 1) Then Return SetError(4, 2, '')
    If Not Number(BinToStr(BinaryMid($data, $start, $end))) And BinToStr(BinaryMid($data, $start, $end)) <> "0e" Then Return SetError(4, 3, '')
    Local $integer = BinToStr(BinaryMid($data, 1, $end))
    $data = BinaryMid($data, $end + 1)
    Return $integer + 0
EndFunc

Func char($data)
    If $data = "" Then
        Return False
    Else
        Return BinToStr(BinaryMid($data, 1, 1))
    EndIf
EndFunc

Func BinToStr($data)
    Return BinaryToString($data)
EndFunc

Use Decode to split the file into fields you can then edit. Encoding was done, so I could find out the hash of a torrent.

Share this post


Link to post
Share on other sites

return an array bigger than 10 thousand !

So what is the problem, the torrent contain that much files :)

Although there is few mistakes in the returned array.

I meant to post this awhile back

Interesting, i think i saw these functions on C somewhere.

Now the question is, how to use it to get files list?

I have tried like this:

#include <Array.au3>

$Array = _TorrentGetFileNames("http://vodo.net/assets/torrents/Beyond.the.Game.720p.x264-VODO.torrent")
_ArrayDisplay($Array)

Func _TorrentGetFileNames($sFile)
    Local $sData = BinaryToString(InetRead($sFile))
    Return decode($sData)
EndFunc

But it's showing only the torrent's info :)


Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

So what is the problem, the torrent contain that much files :)

Although there is few mistakes in the returned array.

Interesting, i think i saw these functions on C somewhere.

I saw them too somewhere. I think my best bet is to make them into a dll from C/C++ but I'm only beginning to grasp C++ however thats next on my list as a learning project.

Edited by FuryCell

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

Share this post


Link to post
Share on other sites

@FuryCell and @MrCreatoR

Are your both functions limited to vodo.net ?

The first torrent link I try : "http://torrents.thepiratebay.org/6210411/Team_Fortress_2_Patch_v1.1.3.4__AutoUpdate_(No-Steam)_OrangeBox.6210411.TPB.torrent"

return an array bigger than 10 thousand ! Posted Image

Please remove that link. I know Jon dosen't appreciate links to illegal content and I'd rather not get this topic locked as torrents can quickly become a shady subject if we are not careful. :)

I meant to post this awhile back:

Thanks for sharing. This should make things even easier.

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

Share this post


Link to post
Share on other sites

That GPL logo under the script means nothing. Please remove it or license your code under GPL proper.

So what would be more proper? Zipping it up and putting the gpl liscense text file in the zip as well?


HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

Share this post


Link to post
Share on other sites

There is a text manual on how to license your application under GPL the right way: http://www.gnu.org/licenses/gpl-howto.html

Interesting read. I've removed the GPL logo and will license it proper.


HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

New version I whipped up this morning. Detects files in subfolders and returns proper results.

(so far :))

If someone finds a torrent that makes it fail please let me know. (PM me the link to the torrent.Please don't post it here.)

Func _TorrentGetFileNames($sFile)
    Local $sData, $iCounter, $iStartPos, $iEndPos, $iLen, $sPath, $asReturn[1] = [0]
    $sData = FileRead($sFile)

    ;;Loop Through Pathl
    While 1
        $iCounter += 1
        $iStartPos = StringInStr($sData, ":pathl", 0, $iCounter)
        If $iStartPos = 0 Then ExitLoop
        $iStartPos += 6
        $iEndPos = StringInStr($sData, ":", 0, 1, $iStartPos + 1)
        $iLen = StringMid($sData, $iStartPos, $iEndPos - $iStartPos)
        $sPath = StringMid($sData, $iEndPos + 1, $iLen)

        ;;File is in a subfolder
        If StringIsDigit(StringRight(StringMid($sData, $iEndPos + 1, $iLen + 1), 1)) Then
            $iPos = StringInStr($sData, ":", 0, 1, $iEndPos + 1 + $iLen)
            $iLen = StringMid($sData, $iEndPos + 1 + $iLen, $iPos - ($iEndPos + 1 + $iLen))
            MsgBox(0, $iPos, $iLen);debug
            $sPath = StringMid($sData, $iPos + 1, $iLen)
        EndIf

        ;;Add file to return array
        _ArrayAdd($asReturn, $sPath)
        $asReturn[0] += 1
    WEnd

    ;;loop through names (only if no files were found in first loop)
    If $asReturn[0] = 0 Then
        $iCounter = 0
        While 1
            $iCounter += 1
            $iStartPos = StringInStr($sData, ":name", 0, $iCounter)
            If $iStartPos = 0 Then ExitLoop
            $iStartPos += 5
            $iEndPos = StringInStr($sData, ":", 0, 1, $iStartPos + 1)
            $iLen = StringMid($sData, $iStartPos, $iEndPos - $iStartPos)
            $sPath = StringMid($sData, $iEndPos + 1, $iLen)
            _ArrayAdd($asReturn, $sPath)
            $asReturn[0] += 1
        WEnd
    EndIf
    Return $asReturn
EndFunc   ;==>_TorrentGetFileNames

Edit:Yes I know its ugly and could be done better with StringRegExp()

Edit2:Further improvements

Edited by FuryCell

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

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  
Followers 0