-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmaketorrent.py
executable file
·466 lines (359 loc) · 11.4 KB
/
maketorrent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# -*- coding: utf-8 -*-
#
# Copyright (C) 2009 Andrew Resch <[email protected]>
#
# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
# the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details.
#
from __future__ import division, unicode_literals, print_function
import os
import sys
from hashlib import sha1 as sha
import logging
from bencode import bencode
def removeit(p):
while p[0] == "\\" or p[0] == "/":
p = p[1:]
return p
def get_path_size(path):
"""
Gets the size in bytes of 'path'
:param path: the path to check for size
:type path: string
:returns: the size in bytes of the path or -1 if the path does not exist
:rtype: int
"""
if not os.path.exists(path):
return -1
if os.path.isfile(path):
return os.path.getsize(path)
dir_size = 0
for (p, dummy_dirs, files) in os.walk(path):
for _file in files:
filename = os.path.join(p, _file)
dir_size += os.path.getsize(filename)
return dir_size
def utf8_encode_structure(data):
"""Recursively convert all unicode keys and values in a data structure to utf8.
e.g. converting keys and values for a dict with nested dicts and lists etc.
Args:
data (any): This can be any structure, dict, list or tuple.
Returns:
input type: The data with unicode keys and values converted to utf8.
"""
if isinstance(data, (list, tuple)):
return type(data)([utf8_encode_structure(d) for d in data])
elif isinstance(data, dict):
return dict([utf8_encode_structure(d) for d in data.items()])
elif not isinstance(data, bytes):
try:
return data.encode('utf8')
except AttributeError:
pass
return data
class InvalidPath(Exception):
"""Raised when an invalid path is supplied."""
pass
class InvalidPieceSize(Exception):
"""Raised when an invalid piece size is set.
Note:
Piece sizes must be multiples of 16KiB.
"""
pass
class TorrentMetadata(object):
"""This class is used to create .torrent files.
Examples:
>>> t = TorrentMetadata()
>>> t.data_path = '/tmp/torrent'
>>> t.comment = 'My Test Torrent'
>>> t.trackers = [['http://tracker.openbittorent.com']]
>>> t.save('/tmp/test.torrent')
"""
def __init__(self):
self.__data_path = None
self.__piece_size = 0
self.__comment = ''
self.__private = False
self.__trackers = []
self.__webseeds = []
self.__pad_files = False
self.__source = ''
self.skip = [
"Thumbs.db",
"edition.txt",
"tracklist.txt",
"formatted_tracklist.txt",
"credits.txt",
]
def save(self, torrent_path, progress=None):
"""Creates and saves the torrent file to `path`.
Args:
torrent_path (str): Location to save the torrent file.
progress(func, optional): The function to be called when a piece is hashed. The
provided function should be in the format `func(num_completed, num_pieces)`.
Raises:
InvalidPath: If the data_path has not been set.
"""
if not self.data_path:
raise InvalidPath('Need to set a data_path!')
torrent = {
'info': {}
}
if self.source:
torrent['info']['source'] = self.source
if self.comment:
torrent['comment'] = self.comment
if self.private:
torrent['info']['private'] = True
if self.trackers:
torrent['announce'] = self.trackers[0][0]
#torrent['announce-list'] = self.trackers
else:
torrent['announce'] = ''
if self.webseeds:
httpseeds = []
webseeds = []
for w in self.webseeds:
if w.endswith('.php'):
httpseeds.append(w)
else:
webseeds.append(w)
if httpseeds:
torrent['httpseeds'] = httpseeds
if webseeds:
torrent['url-list'] = webseeds
datasize = get_path_size(self.data_path)
if self.piece_size:
piece_size = self.piece_size * 1024
else:
# We need to calculate a piece size
piece_size = 16384
while (datasize // piece_size) > 1024 and piece_size < (8192 * 1024):
piece_size *= 2
# Calculate the number of pieces we will require for the data
num_pieces = datasize // piece_size
if datasize % piece_size:
num_pieces += 1
torrent['info']['piece length'] = piece_size
torrent['info']['name'] = os.path.split(self.data_path)[1]
# Create the info
if os.path.isdir(self.data_path):
files = []
padding_count = 0
# Collect a list of file paths and add padding files if necessary
for (dirpath, dirnames, filenames) in os.walk(self.data_path):
for index, filename in enumerate(filenames):
if filename in self.skip or filename.endswith(".torrent") or filename.endswith(".json") or filename.endswith(".ini") or filename.endswith(".ico"):
logging.info("Skipped " + filename)
else:
size = get_path_size(os.path.join(self.data_path, dirpath, filename))
p = dirpath[len(self.data_path):]
p = p.lstrip('/')
p = p.split('/')
if p[0]:
p += [filename]
else:
p = [filename]
files.append((size, p))
# Add a padding file if necessary
if self.pad_files and (index + 1) < len(filenames):
left = size % piece_size
if left:
p = list(p)
p[-1] = '_____padding_file_' + str(padding_count)
files.append((piece_size - left, p))
padding_count += 1
# Run the progress function with 0 completed pieces
if progress:
progress(0, num_pieces)
fs = []
pieces = []
# Create the piece hashes
buf = b''
for size, path in files:
logging.info(path)
path = [removeit(s) for s in path]
#path = [removeit(s.decode(sys.getfilesystemencoding()).encode('UTF-8')) for s in path]
fs.append({'length': size, 'path': path})
#if path[-1].startswith('_____padding_file_'):
# buf += '\0' * size
# pieces.append(sha(buf).digest())
# buf = b''
# fs[-1]['attr'] = 'p'
#else:
#print (self.data_path, path, "==>", os.path.join(self.data_path, *path))
with open(os.path.join(self.data_path, *path), 'rb') as _file:
r = _file.read(piece_size - len(buf))
while r:
buf = buf + r
if len(buf) == piece_size:
pieces.append(sha(buf).digest())
# Run the progress function if necessary
if progress:
progress(len(pieces), num_pieces)
buf = b''
else:
break
r = _file.read(piece_size - len(buf))
torrent['info']['files'] = fs
if buf:
pieces.append(sha(buf).digest())
if progress:
progress(len(pieces), num_pieces)
buf = ''
elif os.path.isfile(self.data_path):
torrent['info']['length'] = get_path_size(self.data_path)
pieces = []
with open(self.data_path, 'rb') as _file:
r = _file.read(piece_size)
while r:
pieces.append(sha(r).digest())
if progress:
progress(len(pieces), num_pieces)
r = _file.read(piece_size)
torrent['info']['pieces'] = b''.join(pieces)
# Write out the torrent file
with open(torrent_path, 'wb') as _file:
_file.write(bencode(utf8_encode_structure(torrent)))
#f = open(os.path.abspath(torrent_path), 'wb')
#f.write(bencode(utf8_encode_structure(torrent)))
#f.close()
def get_data_path(self):
"""Get the path to the files that the torrent will contain.
Note:
It can be either a file or a folder.
Returns:
str: The torrent data path, either a file or a folder.
"""
return self.__data_path
def set_data_path(self, path):
"""Set the path to the files (data) that the torrent will contain.
Note:
This property needs to be set before the torrent file can be created and saved.
Args:
path (str): The path to the torrent data and can be either a file or a folder.
Raises:
InvalidPath: If the path is not found.
"""
if os.path.exists(path) and (os.path.isdir(path) or os.path.isfile(path)):
self.__data_path = os.path.abspath(path)
else:
raise InvalidPath('No such file or directory: %s' % path)
def get_piece_size(self):
"""The size of the pieces.
Returns:
int: The piece size in multiples of 16 KiBs.
"""
return self.__piece_size
def set_piece_size(self, size):
"""Set piece size.
Note:
If no piece size is set, one will be automatically selected to
produce a torrent with less than 1024 pieces or the smallest possible
with a 8192KiB piece size.
Args:
size (int): The desired piece size in multiples of 16 KiBs.
Raises:
InvalidPieceSize: If the piece size is not a valid multiple of 16 KiB.
"""
if size % 16 and size:
raise InvalidPieceSize('Piece size must be a multiple of 16 KiB')
self.__piece_size = size
def get_comment(self):
"""Get the torrent comment.
Returns:
str: An informational string about the torrent.
"""
return self.__comment
def set_comment(self, comment):
"""Set the comment for the torrent.
Args:
comment (str): An informational string about the torrent.
"""
self.__comment = comment
def get_source(self):
"""Get the torrent source.
Returns:
str: The source of the torrent.
"""
return self.__source
def set_source(self, source):
"""Set the source for the torrent.
Args:
source (str): The source of the torrent.
"""
self.__source = source
def get_private(self):
"""Get the private flag of the torrent.
Returns:
bool: True if private flag has been set, else False.
"""
return self.__private
def set_private(self, private):
"""Set the torrent private flag.
Note:
Private torrents only announce to trackers and will not use DHT or
Peer Exchange. See http://bittorrent.org/beps/bep_0027.html
Args:
private (bool): True if the torrent is to be private.
"""
self.__private = private
def get_trackers(self):
"""Get the announce trackers.
Note:
See http://bittorrent.org/beps/bep_0012.html
Returns:
list of lists: A list containing a list of trackers.
"""
return self.__trackers
def set_trackers(self, trackers):
"""Set the announce trackers.
Args:
private (list of lists): A list containing lists of trackers as strings, each list is a tier.
"""
self.__trackers = trackers
def get_webseeds(self):
"""Get the webseeds.
Note:
The web seeds can either be:
Hoffman-style: http://bittorrent.org/beps/bep_0017.html
GetRight-style: http://bittorrent.org/beps/bep_0019.html
If the url ends in '.php' then it will be considered Hoffman-style, if
not it will be considered GetRight-style.
Returns:
list: The webseeds.
"""
return self.__webseeds
def set_webseeds(self, webseeds):
"""Set webseeds.
Note:
The web seeds can either be:
Hoffman-style: http://bittorrent.org/beps/bep_0017.html
GetRight-style: http://bittorrent.org/beps/bep_0019.html
If the url ends in '.php' then it will be considered Hoffman-style, if
not it will be considered GetRight-style.
Args:
private (list): The webseeds URLs which can be either Hoffman or GetRight style.
"""
self.__webseeds = webseeds
def get_pad_files(self):
"""Get status of padding files for the torrent.
Returns:
bool: True if padding files have been enabled to align files on piece boundaries.
"""
return self.__pad_files
def set_pad_files(self, pad):
"""Enable padding files for the torrent.
Args:
private (bool): True adds padding files to align files on piece boundaries.
"""
self.__pad_files = pad
data_path = property(get_data_path, set_data_path)
piece_size = property(get_piece_size, set_piece_size)
comment = property(get_comment, set_comment)
source = property(get_source, set_source)
private = property(get_private, set_private)
trackers = property(get_trackers, set_trackers)
webseeds = property(get_webseeds, set_webseeds)
pad_files = property(get_pad_files, set_pad_files)