Skip to content

Commit 845262c

Browse files
committed
torrents: add torrents export option
1 parent 4631f03 commit 845262c

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

library/__main__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@
129129
"gallery_update": "Update online gallery media",
130130
"links_update": "Update a link-scraping database",
131131
"reddit_update": "Update reddit media",
132-
"computers_update": "Update computer stats",
133132
},
134133
"Misc subcommands": {
135134
"export_text": "Export HTML files from SQLite databases",
@@ -162,7 +161,6 @@ def print_help(parser) -> None:
162161

163162
modules = {
164163
"library.createdb.computers_add.computers_add": ["computer-add", "pc-add", "ssh-add"],
165-
"library.createdb.computers_add.computers_update": ["computer-update", "pc-update", "ssh-update"],
166164
"library.createdb.fs_add.fs_add": ["filesystem-add", "x", "extract"],
167165
"library.createdb.fs_add.fs_update": ["filesystem-update", "xu"],
168166
"library.createdb.gallery_add.gallery_add": ["gdl-add", "ga"],

library/createdb/computers_add.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
from library.utils.log_utils import log
88

99

10-
def parse_args(action, usage):
11-
parser = argparse_utils.ArgumentParser(usage=usage)
10+
def parse_args():
11+
parser = argparse_utils.ArgumentParser(usage=usage.computers_add)
1212
arggroups.debug(parser)
1313

1414
arggroups.database(parser)
15-
if action == consts.SC.computers_add:
16-
parser.add_argument("--ignore-mounts", nargs="+", default=[], help="List of mountpoints to ignore")
17-
parser.add_argument("hostnames", nargs="+", help="List of hostnames to connect to")
15+
parser.add_argument("--ignore-mounts", nargs="+", default=[], help="List of mountpoints to ignore")
16+
parser.add_argument("hostnames", nargs="+", help="List of hostnames to connect to")
1817
args = parser.parse_args()
1918
arggroups.args_post(args, parser, create_db=True)
2019

@@ -101,12 +100,6 @@ def computer_add(args, hostnames):
101100

102101

103102
def computers_add():
104-
args = parse_args(consts.SC.computers_add, usage.computers_add)
103+
args = parse_args()
105104

106105
computer_add(args, args.hostnames)
107-
108-
109-
def computers_update():
110-
args = parse_args(consts.SC.computers_update, usage.computers_update)
111-
112-
computer_add(args, [d["path"] for d in args.db.query("select path from playlists")])

library/playback/media_printer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def media_printer(args, data, units=None, media_len=None) -> None:
161161
D["duration"] = total_duration
162162
D["avg_duration"] = nums.safe_mean(m.get("duration") for m in media)
163163

164-
if hasattr(args, "action") and "history" in tables:
164+
if hasattr(args, "action") and "history" in tables and "id" in m_columns:
165165
if action in (SC.download, SC.download_status) and "time_downloaded" in m_columns:
166166
D["download_duration"] = cadence_adjusted_items(args, D["count"], time_column="time_downloaded")
167167
elif total_duration > 0:

library/playback/torrents_info.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from library import usage
66
from library.mediafiles import torrents_start
7-
from library.utils import arggroups, argparse_utils, consts, iterables, printing, processes, strings
7+
from library.utils import arggroups, argparse_utils, consts, iterables, path_utils, printing, processes, strings
88
from library.utils.path_utils import domain_from_url
99

1010

@@ -45,10 +45,13 @@ def parse_args():
4545
parser.add_argument("--all", action="store_true", help="Show active and inactive torrents")
4646
parser.add_argument("--active", action="store_true", help="Show active torrents")
4747
parser.add_argument("--inactive", "--dead", action="store_true", help="Show inactive torrents")
48+
parser.add_argument("--stopped", "--paused", action="store_true", help="Show stopped torrents")
4849

4950
parser.add_argument(
5051
"--force-start", "--start", action=argparse.BooleanOptionalAction, help="Force start matching torrents"
5152
)
53+
parser.add_argument("--stop", action="store_true", help="Stop matching torrents")
54+
parser.add_argument("--export", action="store_true", help="Export matching torrent files")
5255
arggroups.capability_soft_delete(parser)
5356
arggroups.capability_delete(parser)
5457
arggroups.debug(parser)
@@ -133,6 +136,9 @@ def shorten(s, width):
133136
printing.table(tbl)
134137
print()
135138

139+
if args.stopped:
140+
torrents = [t for t in torrents if t.state_enum.is_stopped]
141+
136142
torrents = filter_torrents_by_activity(args, torrents)
137143

138144
if args.torrent_search or args.file_search:
@@ -187,7 +193,7 @@ def shorten(s, width):
187193
or (t.state_enum.is_complete and t.uploaded_session > 0)
188194
]
189195
if active_torrents:
190-
print("Active Torrents")
196+
print(f"Active Torrents ({len(active_torrents)})")
191197

192198
def gen_row(t):
193199
d = {
@@ -248,7 +254,7 @@ def gen_row(t):
248254
or (t.state_enum.is_complete and t.uploaded_session == 0)
249255
]
250256
if inactive_torrents:
251-
print("Inactive Torrents")
257+
print(f"Inactive Torrents ({len(inactive_torrents)})")
252258

253259
def gen_row(t):
254260
d = {
@@ -318,6 +324,20 @@ def gen_row(t):
318324
print("Force-starting", len(torrents))
319325
qbt_client.torrents_set_force_start(args.force_start, torrent_hashes=torrent_hashes)
320326

327+
if args.stop:
328+
print("Stopping", len(torrents))
329+
qbt_client.torrents_stop(torrent_hashes=torrent_hashes)
330+
331+
if args.export:
332+
p = Path("exported_torrents")
333+
p.mkdir(exist_ok=True)
334+
for idx, t in enumerate(torrents):
335+
printing.print_overwrite("Exporting", idx + 1, "of", len(torrents), "to", p)
336+
337+
file_name = f"{qbt_get_tracker(qbt_client, t)}_{t.name}_{t.hash}.torrent"
338+
file_name = path_utils.clean_path(file_name.encode())
339+
(p / file_name).write_bytes(qbt_client.torrents_export(torrent_hash=t.hash))
340+
321341
if args.temp_drive and Path(args.temp_drive).is_absolute():
322342
temp_prefix = Path(args.temp_drive)
323343
else:

0 commit comments

Comments
 (0)