hooks: improve torrent downloader

This commit is contained in:
ed 2024-07-14 17:57:36 +00:00
parent c06aa683eb
commit 803e156509

View file

@ -1,10 +1,10 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# coding: utf-8 # coding: utf-8
from __future__ import print_function, unicode_literals
import os import os
import sys import sys
import json import json
import shutil
import subprocess as sp import subprocess as sp
@ -12,6 +12,16 @@ _ = r"""
start downloading a torrent by POSTing a magnet URL to copyparty, start downloading a torrent by POSTing a magnet URL to copyparty,
for example using 📟 (message-to-server-log) in the web-ui for example using 📟 (message-to-server-log) in the web-ui
by default it will download the torrent to the folder you were in
when you pasted the magnet into the message-to-server-log field
you can optionally specify another location by adding a whitespace
after the magnet URL followed by the name of the subfolder to DL into,
or for example "anime/airing" would download to /srv/media/anime/airing
because the keyword "anime" is in the DESTS config below
needs python3
example usage as global config (not a good idea): example usage as global config (not a good idea):
python copyparty-sfx.py --xm f,j,t60,bin/hooks/qbittorrent-magnet.py python copyparty-sfx.py --xm f,j,t60,bin/hooks/qbittorrent-magnet.py
@ -45,6 +55,15 @@ while you're in the /qb folder (or any folder below there)
ALLOWLIST = [ "ed", "morpheus" ] ALLOWLIST = [ "ed", "morpheus" ]
# list of destination aliases to translate into full filesystem
# paths; takes effect if the first folder component in the
# custom download location matches anything in this dict
DESTS = {
"iso": "/srv/pub/linux-isos",
"anime": "/srv/media/anime",
}
def main(): def main():
inf = json.loads(sys.argv[1]) inf = json.loads(sys.argv[1])
url = inf["txt"] url = inf["txt"]
@ -60,14 +79,40 @@ def main():
# which matches the URL that the magnet message was sent to # which matches the URL that the magnet message was sent to
os.chdir(inf["ap"]) os.chdir(inf["ap"])
# is there is a custom download location in the url?
dst = ""
if " " in url:
url, dst = url.split(" ", 1)
# is the location in the predefined list of locations?
parts = dst.replace("\\", "/").split("/")
if parts[0] in DESTS:
dst = os.path.join(DESTS[parts[0]], *(parts[1:]))
else:
# nope, so download to the current folder instead;
# comment the dst line below to instead use the default
# download location from your qbittorrent settings
dst = inf["ap"]
pass
# archlinux has a -nox suffix for qbittorrent if headless
# so check if we should be using that
if shutil.which("qbittorrent-nox"):
torrent_bin = "qbittorrent-nox"
else:
torrent_bin = "qbittorrent"
# the command to add a new torrent, adjust if necessary # the command to add a new torrent, adjust if necessary
cmd = ["qbittorrent-nox", url] cmd = [torrent_bin, url]
if dst:
cmd += ["--save-path=%s" % (dst,)]
# if copyparty and qbittorrent are running as different users # if copyparty and qbittorrent are running as different users
# you may have to do something like the following # you may have to do something like the following
# (assuming qbittorrent-nox* is nopasswd-allowed in sudoers): # (assuming qbittorrent* is nopasswd-allowed in sudoers):
# #
# cmd = ["sudo", "-u", "qbitter", "qbittorrent-nox", url] # cmd = ["sudo", "-u", "qbitter"] + cmd
print("🧲", cmd) print("🧲", cmd)