forked from Projeto-Pindorama/pico-torrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpico-torrent.py
98 lines (79 loc) · 3.1 KB
/
pico-torrent.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
#!/usr/bin/env python
'''
pico-torrent 1.0
A fast and small torrent client, initially made for Otto-PKG.
Developed by: Jazz_Man (alias).
Applied from the following example: https://www.libtorrent.org/python_binding.html
License:
Copyright © 2020 Projeto Pindorama.
pico-torrent is under the Caldera license.
Dependencies:
> python 3.4=<
> libtorrent-rasterbar
Syntax:
python3.? pico-torrent -f <file> -d <directory> (-s| )
or python3.? pico-torrent -L <file>
To-do list:
> Manually set up the download bandwidth priority;
> Create .torrent files;
> Cryptography support (perhaps).
'''
import libtorrent as lt
import os
def get_save_dir(dir):
if dir:
if os.path.isdir(dir):
return dir
print(">>> Invalid Directory")
else:
return os.getcwd()
def show_status(local):
torrent = local.status()
print("\r{:.2f}% complete (down: {:.1f} kB/s up: {:.1f} kB/s peers: {:d}) {}".format(
torrent.progress * 100, torrent.download_rate / 1000, torrent.upload_rate / 1000,
torrent.num_peers, torrent.state), end=" ")
def start_download(source, keep_seeding, save_dir):
if not source:
print('>>> Please provide a file/magnet link')
return
session = lt.session({'listen_interfaces': '0.0.0.0:10881'})
try:
if os.path.isfile(source):
params = {'save_path': get_save_dir(save_dir),
'ti': lt.torrent_info(source)}
else:
params = lt.parse_magnet_uri(source)
params.save_path = get_save_dir(save_dir)
local = session.add_torrent(params)
torrent = local.status()
if keep_seeding:
while True:
show_status(local)
print("\n>>> Download complete.\n>>>Seeding")
else:
while not torrent.is_seeding:
show_status(local)
print("\n>>> Download complete!")
except KeyboardInterrupt:
print("\n>>> Exiting...\n")
except Exception as error:
print(">>> There was an error: {}".format(error))
def create_magnet(filename):
return lt.make_magnet_uri(lt.torrent_info(filename))
if __name__ == "__main__":
import argparse
argument = argparse.ArgumentParser()
exclusive = argument.add_mutually_exclusive_group(required=True)
exclusive.add_argument("--file", "-f",help="Torrent file or magnet link to download")
argument.add_argument("--seeding", "-S", action="store_true",help="Keep seeding after the download is complete")
argument.add_argument("--infos", "-i",help="Show torrent info")
argument.add_argument("--output", "-O", action="store_true",help="File output; filename")
# argument.add_argument("--download-priority", "-P", default=lt.pri,
# help="Set download priority")
exclusive.add_argument("--link", "-L",help="Create a magnet link through a file")
argument.add_argument("--dir", "-d",help="Specify the download save directory")
arguments = argument.parse_args()
if arguments.link:
print(create_magnet(arguments.link))
else:
start_download(arguments.file, arguments.seeding, arguments.dir)