121 lines
4.0 KiB
Python
121 lines
4.0 KiB
Python
import logging
|
|
import random
|
|
import subprocess
|
|
import httpx
|
|
from pathlib import Path
|
|
import torf
|
|
import time
|
|
|
|
class Deluge:
|
|
def __init__(self, session: httpx.Client) -> None:
|
|
self.session = session
|
|
|
|
@classmethod
|
|
def new(cls) -> 'Deluge':
|
|
logging.debug("Connecting to Deluge")
|
|
session = httpx.Client(base_url="https://t.zzzp.win/", timeout=10, auth=httpx.BasicAuth('abdus', 'xAsametk50'))
|
|
res = session.post("/json", json={"method": "auth.login", "params": ["xAsametk50"], "id": cls.make_id()})
|
|
res.raise_for_status()
|
|
|
|
# find first available host
|
|
res = session.post("/json", json={"method": "web.get_hosts", "params": [], "id": cls.make_id()})
|
|
res.raise_for_status()
|
|
host_id = res.json()["result"][0][0]
|
|
|
|
# connect to a host
|
|
res = session.post("/json", json={"method": "web.connect", "params": [host_id], "id": cls.make_id()})
|
|
res.raise_for_status()
|
|
|
|
return cls(session)
|
|
|
|
@classmethod
|
|
def make_id(cls) -> int:
|
|
return time.time_ns()
|
|
|
|
def add(self, torrent_path: Path, download_path: Path, paused: bool = True, skip_check: bool = True):
|
|
# upload torrent
|
|
logging.debug("Uploading torrent file")
|
|
with torrent_path.open("rb") as f:
|
|
res = self.session.post("/upload", files={"file": f})
|
|
res.raise_for_status()
|
|
remote_path: str = res.json()["files"][0]
|
|
|
|
# add torrent
|
|
logging.debug("Adding torrent file")
|
|
res = self.session.post(
|
|
"/json",
|
|
timeout=1,
|
|
json={
|
|
"method": "web.add_torrents",
|
|
"params": [
|
|
[
|
|
{
|
|
"path": remote_path,
|
|
"options": {
|
|
"file_priorities": [1],
|
|
"add_paused": paused,
|
|
"sequential_download": False,
|
|
"pre_allocate_storage": False,
|
|
"download_location": str(download_path),
|
|
# "move_completed": False,
|
|
# "move_completed_path": "/root/Downloads",
|
|
# "prioritize_first_last_pieces": True,
|
|
"seed_mode": skip_check,
|
|
# "super_seeding": False,
|
|
},
|
|
}
|
|
]
|
|
],
|
|
"id": self.make_id(),
|
|
},
|
|
)
|
|
|
|
res.raise_for_status()
|
|
|
|
|
|
def find_torrents() -> list[Path]:
|
|
files = list(Path('/tmp/deluge/config/state').glob('*.torrent'))
|
|
random.shuffle(files)
|
|
return files
|
|
|
|
def find_existing_torrents() -> dict[str, Path]:
|
|
lines = subprocess.check_output(['ssh', 'klein', '--', 'find', '/mnt/box/files/_torrents', '-maxdepth', '1']).decode()
|
|
existing = {}
|
|
lines = lines.strip().splitlines(keepends=False)
|
|
for line in lines:
|
|
if line.startswith('.'):
|
|
continue
|
|
p = Path(line)
|
|
existing[p.name] = p
|
|
return existing
|
|
|
|
|
|
def main():
|
|
existing = find_existing_torrents()
|
|
d = Deluge.new()
|
|
done = set()
|
|
for torrent_path in find_torrents():
|
|
if torrent_path in done:
|
|
continue
|
|
if len(done) == len(existing):
|
|
return
|
|
with torrent_path.open('rb') as f:
|
|
t = torf.Torrent.read_stream(f)
|
|
if '.PRT' in t.name:
|
|
continue
|
|
if et := existing.get(t.name):
|
|
logging.debug(f'adding {t.name}')
|
|
download_path = Path("/dl")
|
|
try:
|
|
d.add(torrent_path, download_path=download_path)
|
|
torrent_path.unlink()
|
|
done.add(torrent_path)
|
|
logging.info(f'added: {t.name}')
|
|
except Exception as e:
|
|
pass
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
logging.basicConfig(level=logging.INFO, format=f'%(asctime)s {logging.BASIC_FORMAT}')
|
|
main() |