File: make_torrent.py

package info (click to toggle)
libtorrent-rasterbar 2.0.11-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,304 kB
  • sloc: cpp: 190,670; python: 7,142; makefile: 1,374; ansic: 574; sh: 317; xml: 104
file content (61 lines) | stat: -rwxr-xr-x 1,470 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3


import sys
import os
import libtorrent

if len(sys.argv) < 3:
    print('usage make_torrent.py file tracker-url')
    sys.exit(1)

input = os.path.abspath(sys.argv[1])

fs = libtorrent.file_storage()

# def predicate(f):
#   print f
#   return True
# libtorrent.add_files(fs, input, predicate)

parent_input = os.path.split(input)[0]

# if we have a single file, use it because os.walk does not work on a single files
if os.path.isfile(input):
    size = os.path.getsize(input)
    fs.add_file(input, size)

for root, dirs, files in os.walk(input):
    # skip directories starting with .
    if os.path.split(root)[1][0] == '.':
        continue

    for f in files:
        # skip files starting with .
        if f[0] == '.':
            continue

        # skip thumbs.db on windows
        if f == 'Thumbs.db':
            continue

        fname = os.path.join(root[len(parent_input) + 1:], f)
        size = os.path.getsize(os.path.join(parent_input, fname))
        print('%10d kiB  %s' % (size / 1024, fname))
        fs.add_file(fname, size)

if fs.num_files() == 0:
    print('no files added')
    sys.exit(1)

t = libtorrent.create_torrent(fs, 0, 4 * 1024 * 1024)

t.add_tracker(sys.argv[2])
t.set_creator('libtorrent %s' % libtorrent.__version__)

libtorrent.set_piece_hashes(t, parent_input, lambda x: sys.stdout.write('.'))
sys.stdout.write('\n')

f = open('out.torrent', 'wb+')
f.write(libtorrent.bencode(t.generate()))
f.close()