Jump to content

Torrent maker, hash problem


Rex
 Share

Recommended Posts

A friend of mine asked if I could make a torrent maker for him, and I thought that could be a nice challenge.

I searched the forum and found that monoceres had created on for single files, I wrote and asked If I could get the source and monoceres  was so kind to provide it :wub:

But it was only for single files, and I want to create one for both single and multiply files, also I would try to make it in native AutoIT, so I changed the code as below.

For a single file the script works fine, but for multiply files in a folder, I ran into a problem. It's seems that my hash file function fails when creating for multiply files, and I can't really get my head around on what seems to be the problem.

Also It seems that when creating a torrent in utorrent, the files is sorted alphabetic by extension and not the filename it self, but the filelisttoarray sorts them by filename, and couldn't find a way to make it sort by ext.

Here is my test code, it ugly and poor written but it works for most parts.

#include <crypt.au3>
#include <array.au3>
#include <file.au3>
Global $Progress1
$FolderPath = 'C:\Test\'
$aFolderContents = _FileListToArray('C:\Test') ; Gives a list of files/folders inside a folder
$outfile = 'C:\TestOUT.torrent'
$sizeofpiece = 64 ; Fixed size for now
$sizeofpiece *= 1024
$write = FileOpen($outfile, 1)

$sAnnounce = 'd8:announce'
$sAnounceUrl = '19:http://announce.com'
$sComment = '7:comment'
$sCommentString = '17:This is a comment'
$sCreatedBy = '10:created by'
$sCreatedBYString = '13:uTorrent/2210'
$sCreationDate = '13:creation date'
$sDate = 'i1461781110e'
$sEncoding = '8:encoding'
$sEncodingType = '5:UTF-8'
$sInfo = '4:info'
$sFiles = 'd5:files'
$slistDir = 'l' ; Dir start
$sFileLength = 'd6:length'
$sFile1 = 'i' & FileGetSize($FolderPath & $aFolderContents[2]) & 'e'
$sFilePath = '4:pathl'
$sFile1Name = StringLen($aFolderContents[2]) & ':' & $aFolderContents[2] & 'ee'
$sFile2 = 'i' & FileGetSize($FolderPath & $aFolderContents[3]) & 'e'
$sFile2Name = StringLen($aFolderContents[3]) & ':' & $aFolderContents[3] & 'ee'
$sFile3 = 'i' & FileGetSize($FolderPath & $aFolderContents[1]) & 'e'
$sFile3Name = StringLen($aFolderContents[1]) & ':' & $aFolderContents[1] & 'ee'
$EndList = 'e'

$sPices = '12:piece lengthi' & $sizeofpiece & 'e'
$sPicesCount = '6:pieces' & _GetPiecesCount(DirGetSize('C:\Test'), $sizeofpiece) & ':'
FileWrite($write, $sAnnounce & $sAnounceUrl & $sComment & $sCommentString & $sCreatedBy & $sCreatedBYString & $sCreationDate & $sDate & $sEncoding & _
$sEncodingType & $sInfo & $sFiles & $slistDir & $sFileLength & $sFile1 & $sFilePath & $sFile1Name & $sFileLength & $sFile2 & $sFilePath & $sFile2Name & _
$sFileLength & $sFile3 & $sFilePath & $sFile3Name & $EndList & '4:name' & StringLen(StringTrimLeft(StringTrimRight($FolderPath, 1), 3)) & ':' & StringTrimLeft(StringTrimRight($FolderPath, 1), 3) & $sPices & $sPicesCount)
FileClose($write)
Sleep(500)
; Loop thru the files to create HASH..
; Hmm seems that the torrent is sorted by extension in alphab order, and not in plain aplhab order.
;~ For $i = 1 to UBound($aFolderContents) -1
;~ $sfileHash = _WriteHashFilePieces($FolderPath & $aFolderContents[$i], $sizeofpiece, $outfile)
;~ ConsoleWrite(@CRLF & $i & ' = ' & $aFolderContents[$i] & @CRLF)
;~ Next
#region ### START Koda GUI section ### Form=
    $Form1 = GUICreate("Creating Torrent file", 416, 75, -1, -1)
    $Progress1 = GUICtrlCreateProgress(16, 8, 300, 33)
    $Label10 = GUICtrlCreateLabel("Hashing data...:", 326, 16, 90, 20)
    GUICtrlSetFont(-1, 8, 400, 0, "MS Sans Serif")
    GUISetState(@SW_SHOW)
    #endregion ### START Koda GUI section ### Form
GUISetState(@SW_HIDE)
$outfileEnd = FileOpen($outfile, 17)
FileWrite($outfileEnd, _WriteHashFilePieces($FolderPath & $aFolderContents[2], $sizeofpiece, $outfile))
FileWrite($outfileEnd, _WriteHashFilePieces($FolderPath & $aFolderContents[3], $sizeofpiece, $outfile))
FileWrite($outfileEnd, _WriteHashFilePieces($FolderPath & $aFolderContents[1], $sizeofpiece, $outfile))
FileWrite($outfileEnd,  '7:privatei1eee')
FileClose($outfileEnd)

Func _GetPiecesCount($size, $psize)
    $return = Ceiling(($size / $psize) * 20)
    While Mod($return, 20) > 0
        $return += 1
    WEnd
    Return $return
EndFunc   ;==>_GetPiecesCount

Func _WriteHashFilePieces($file, $psize, $outfile)
    Local $t, $ty
    Local $inc, $percent, $totalsize = FileGetSize($file), $oldp
    Local $hash
    Local $size = FileGetSize($file)
    Local $filehandle = FileOpen($file,16)
    If $filehandle = -1 Then MsgBox(16, "Error", "Error opening file")
        While 1

        $tdata = FileRead($filehandle, $psize)
        If $tdata <> "" Then

            $t = _Crypt_HashData($tdata, $CALG_SHA1)

            $t = BinaryToString($t)

        $inc += $psize
            $percent = Round(($inc / $totalsize) * 100)

            If $percent <> $oldp Then
                GUICtrlSetData($Progress1, $percent)
            EndIf

            $oldp = $percent
$ty = $ty & $t
        Else
            ExitLoop
        EndIf
    WEnd
Return $ty
EndFunc   ;==>_WriteHashFilePieces

I have created a test torrent with utorrent 2.1 from the same folder, and that's the one I use to compare my result with, also I'm trying to open the torrent with a Torrent editor. But as far I can see, it's the last file in my folder where the script fails the HASH creation. My test folder contains 3 files, a txt, an exe and a nfo file, all of them is a txt file with changed ext. Could some one help by point me in the right direction with the hash problem

 

Cheers
/Rex

Edited by Rex
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...