-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
executable file
·56 lines (39 loc) · 1.51 KB
/
main.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
#!/usr/bin/env python
import os
import argparse
from twisted.internet import reactor
from client import BitTorrentClient
from torrentfile import TorrentFile
from tracker import Tracker
def get_args():
parser = argparse.ArgumentParser(description='Download a small single file via BitTorrent protocol')
parser.add_argument('-f', '--file',
required=True,
dest = 'file',
type = argparse.FileType('r'))
parser.add_argument('-d', '--dir',
help='path to download directory',
required=True,
dest='dir',
type=str)
args = parser.parse_args()
file_location = os.path.abspath(args.file.name)
download_dir = args.dir
if not os.path.isdir(download_dir):
parser.error('Download directory must exist.')
return file_location, download_dir
def main():
file_location, download_dir = get_args()
# parse the torrent file and make into an object
torrentfile = TorrentFile(file_location)
# create bittorrent client (brain behind everything)
client = BitTorrentClient(download_dir, torrentfile)
client.create_files()
# create tracker and add it to the client
tracker = Tracker(client.peer_id, torrentfile)
client.add_tracker(tracker)
# connect to the peers associated to the tracker
tracker.get_peers_and_connect(client, torrentfile)
reactor.run()
if __name__ == '__main__':
main()