Skip to content

Instantly share code, notes, and snippets.

@SophiaH67
Last active July 21, 2023 17:39
Show Gist options
  • Save SophiaH67/66b3a59999af7a98ff9598eb383ff040 to your computer and use it in GitHub Desktop.
Save SophiaH67/66b3a59999af7a98ff9598eb383ff040 to your computer and use it in GitHub Desktop.
This script is to fix 0 byte fastresume files. There is no documentation and the code is god awful. It has worked once, and probably will not ever again. If you want help with running it, feel free to leave a comment or contact me any other way (check my bio).
import sys
import os
import glob
import requests
import subprocess
def get_torrent_name(torrent_file):
# Use transmission-show to get the torrent name
torrent_name = subprocess.check_output(["transmission-show", torrent_file]).decode(
"utf-8"
)
torrent_name = torrent_name.split("\n")[0].split(":")[1].strip()
return torrent_name
URL = "http://192.168.67.85:8080"
# TORRENTS_FOLDER = "/config/qBittorrent/BT_backup"
TORRENTS_FOLDER = "/mnt/media/tmp/BT_backup/"
# TORRENTS_FOLDER = "/home/marnix/Downloads/"
# TORRENTS_FOLDER = "/mnt/media/torrents/torrents/"
CATEGORIES = requests.get(f"{URL}/api/v2/torrents/categories").json().values()
DEFAULT_SAVE_PATH = requests.get(f"{URL}/api/v2/app/defaultSavePath").text
# Transform category paths
for category in CATEGORIES:
category["savePath"] = category["savePath"].replace(
"/downloads", "/mnt/media/torrents"
)
DEFAULT_SAVE_PATH = DEFAULT_SAVE_PATH.replace("/downloads", "/mnt/media/torrents")
from qbittorrentapi import Client
client = Client(host="192.168.67.85:8080")
failed_list = []
for torrent_file in glob.glob(f"{TORRENTS_FOLDER}/*.torrent"):
torrent_found = False
torrent_name = get_torrent_name(torrent_file)
# Loop through categories
for category in CATEGORIES:
save_path = category["savePath"] or f"{DEFAULT_SAVE_PATH}/{category['name']}"
# Check if torrent name exists as a folder in save path
if os.path.exists(f"{save_path}/{torrent_name}"):
print(f"Found {torrent_name} in category {category['name']}")
torrent_found = True
# Check if the torrent is already in qBittorrent
torrents = requests.get(f"{URL}/api/v2/torrents/info").json()
torrent_already_added = False
for torrent in torrents:
if torrent["name"] == torrent_name:
torrent_already_added = True
break
if not torrent_already_added:
print(f"Adding {torrent_name} to qBittorrent")
# Add torrent to qBittorrent
print(
save_path.replace("/mnt/media/torrents", "/downloads"),
)
# res = requests.post(
# f"{URL}/api/v2/torrents/add",
# params={
# "savepath": save_path.replace(
# "/mnt/media/torrents", "/downloads"
# ),
# "category": category["name"],
# "skip_checking": "true",
# "autoTMM": "false",
# },
# files={"torrents": open(torrent_file, "rb")},
# )
# print(res.text)
# print(res.status_code)
try:
client.torrents_add(
torrent_files=[torrent_file],
save_path=save_path.replace(
"/mnt/media/torrents", "/downloads"
),
category=category["name"],
use_auto_torrent_management=False,
is_skip_checking=True,
)
except Exception as e:
print(e)
failed_list.append(torrent_file)
continue
break
if not torrent_found:
print(f"Could not find {torrent_name} in any category")
for failed in failed_list:
print(f"Failed: {failed}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment