-
Notifications
You must be signed in to change notification settings - Fork 0
/
cp_bt_transmission
executable file
·76 lines (55 loc) · 2.07 KB
/
cp_bt_transmission
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
#!/usr/bin/python3
import os
import subprocess
from pathlib import PosixPath
home = os.getenv('HOME')
seed_folder = PosixPath(home + '/Downloads/a_torrents/seeding')
torr_folder = PosixPath(home + '/.config/transmission/torrents')
finished_folder = PosixPath(home + '/Downloads/a_torrents/a_finished')
suffix_witness = '.copied_torrent'
def is_witness(p):
return p.suffix == suffix_witness
def witness_exists(p):
return PosixPath(str(p) + suffix_witness).exists()
def create_witness(p):
subprocess.call(['touch', str(p) + suffix_witness])
def clean_witness():
print("Cleaning witness files")
for f in seed_folder.glob('*' + suffix_witness):
assert is_witness(f)
torr = f.parent / PosixPath('.'.join(f.name.split('.')[:-1]))
if not torr.exists():
subprocess.call(['gvfs-trash', str(f)])
print("Done")
def remove_unless_seeded():
print("Removing files that are not seeded anymore")
seeded_torrents = set(
(t.name for t in seed_folder.glob('*') if not is_witness(t)))
torrent_files = set((
'.'.join(tf.name.split('.')[:-2])
for tf in torr_folder.glob('*.torrent')
))
to_del = seeded_torrents.difference(torrent_files)
for file_to_del in to_del:
fn = seed_folder / PosixPath(file_to_del)
print("Removing {}".format(fn))
subprocess.call(['gvfs-trash', str(seed_folder / fn)])
print("Done")
def cp_seeded():
print("Copying files that have not been copied yet")
for f in seed_folder.glob('*'):
if not witness_exists(f) and not is_witness(f):
print("Copying {}".format(f.name))
subprocess.call(['cp', '-r', str(f), str(finished_folder)])
create_witness(f)
print("Done")
if __name__ == '__main__':
seed_folder.mkdir(parents=True, exist_ok=True)
torr_folder.mkdir(parents=True, exist_ok=True)
finished_folder.mkdir(parents=True, exist_ok=True)
assert seed_folder.exists()
assert torr_folder.exists()
assert finished_folder.exists()
cp_seeded()
remove_unless_seeded()
clean_witness()