forked from jabberjabberjabber/qbit-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
285 lines (227 loc) · 7.44 KB
/
main.py
File metadata and controls
285 lines (227 loc) · 7.44 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import os
from typing import Any
from fastmcp import FastMCP
import qbittorrentapi
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Initialize FastMCP server
mcp = FastMCP("qBittorrent MCP Server")
# qBittorrent client instance (will be initialized on first use)
qbt_client = None
def get_qbt_client():
"""Get or create qBittorrent client instance."""
global qbt_client
if qbt_client is None:
host = os.getenv("QBITTORRENT_HOST", "http://localhost:8080")
username = os.getenv("QBITTORRENT_USERNAME", "admin")
password = os.getenv("QBITTORRENT_PASSWORD", "adminadmin")
qbt_client = qbittorrentapi.Client(
host=host,
username=username,
password=password
)
try:
qbt_client.auth_log_in()
except qbittorrentapi.LoginFailed as e:
raise Exception(f"Failed to login to qBittorrent: {e}")
return qbt_client
@mcp.tool()
def search_torrents(query: str, plugins: str = "all", category: str = "all") -> list[dict[str, Any]]:
"""
Search for torrents using qBittorrent's search plugins.
Args:
query: Search query string
plugins: Comma-separated list of plugin names or "all" for all enabled plugins
category: Filter by category (all, movies, tv, music, games, anime, software, pictures, books)
Returns:
List of search results with torrent information
"""
client = get_qbt_client()
# Start search
search_job = client.search_start(pattern=query, plugins=plugins, category=category)
# Get search job ID
search_id = search_job.id
# Wait for results (check status until complete or timeout)
import time
max_wait = 30 # seconds
waited = 0
while waited < max_wait:
status = client.search_status(search_id=search_id)
if status[0].status == "Stopped":
break
time.sleep(1)
waited += 1
# Get results
results = client.search_results(search_id=search_id, limit=100)
# Stop search
client.search_delete(search_id=search_id)
# Format results
formatted_results = []
for result in results.results:
formatted_results.append({
"name": result.fileName,
"size": result.fileSize,
"size_readable": f"{result.fileSize / (1024**3):.2f} GB",
"seeders": result.nbSeeders,
"leechers": result.nbLeechers,
"url": result.fileUrl,
"description_url": result.descrLink,
"site": result.siteUrl
})
return formatted_results
@mcp.tool()
def download_torrent(
url: str,
save_path: str = None,
category: str = None,
tags: str = None,
paused: bool = False
) -> dict[str, Any]:
"""
Download a torrent by URL or magnet link.
Args:
url: Torrent URL or magnet link
save_path: Directory to save the torrent (optional)
category: Category to assign to the torrent (optional)
tags: Comma-separated tags to assign (optional)
paused: Start torrent in paused state (default: False)
Returns:
Status information about the download
"""
client = get_qbt_client()
# Prepare options
options = {}
if save_path:
options["savepath"] = save_path
if category:
options["category"] = category
if tags:
options["tags"] = tags
if paused:
options["paused"] = "true"
# Add torrent
try:
result = client.torrents_add(urls=url, **options)
if result == "Ok.":
return {
"status": "success",
"message": "Torrent added successfully",
"url": url
}
else:
return {
"status": "error",
"message": f"Failed to add torrent: {result}",
"url": url
}
except Exception as e:
return {
"status": "error",
"message": f"Error adding torrent: {str(e)}",
"url": url
}
@mcp.tool()
def get_torrent_info(torrent_hash: str = None) -> list[dict[str, Any]]:
"""
Get information about torrents in qBittorrent.
Args:
torrent_hash: Specific torrent hash to get info for (optional, returns all if not provided)
Returns:
List of torrent information
"""
client = get_qbt_client()
if torrent_hash:
torrents = client.torrents_info(torrent_hashes=torrent_hash)
else:
torrents = client.torrents_info()
result = []
for torrent in torrents:
result.append({
"hash": torrent.hash,
"name": torrent.name,
"size": torrent.size,
"size_readable": f"{torrent.size / (1024**3):.2f} GB",
"progress": f"{torrent.progress * 100:.2f}%",
"state": torrent.state,
"download_speed": f"{torrent.dlspeed / (1024**2):.2f} MB/s",
"upload_speed": f"{torrent.upspeed / (1024**2):.2f} MB/s",
"eta": torrent.eta,
"seeders": torrent.num_seeds,
"leechers": torrent.num_leechs,
"ratio": torrent.ratio,
"category": torrent.category,
"tags": torrent.tags,
"save_path": torrent.save_path
})
return result
@mcp.tool()
def list_search_plugins() -> list[dict[str, Any]]:
"""
List all available search plugins in qBittorrent.
Returns:
List of search plugins with their status
"""
client = get_qbt_client()
plugins = client.search_plugins()
result = []
for plugin in plugins:
result.append({
"name": plugin.name,
"version": plugin.version,
"enabled": plugin.enabled,
"url": plugin.url,
"supported_categories": plugin.supportedCategories
})
return result
@mcp.tool()
def pause_torrent(torrent_hash: str) -> dict[str, str]:
"""
Pause a torrent.
Args:
torrent_hash: Hash of the torrent to pause
Returns:
Status message
"""
client = get_qbt_client()
try:
client.torrents_pause(torrent_hashes=torrent_hash)
return {"status": "success", "message": f"Torrent {torrent_hash} paused"}
except Exception as e:
return {"status": "error", "message": str(e)}
@mcp.tool()
def resume_torrent(torrent_hash: str) -> dict[str, str]:
"""
Resume a paused torrent.
Args:
torrent_hash: Hash of the torrent to resume
Returns:
Status message
"""
client = get_qbt_client()
try:
client.torrents_resume(torrent_hashes=torrent_hash)
return {"status": "success", "message": f"Torrent {torrent_hash} resumed"}
except Exception as e:
return {"status": "error", "message": str(e)}
@mcp.tool()
def delete_torrent(torrent_hash: str, delete_files: bool = False) -> dict[str, str]:
"""
Delete a torrent from qBittorrent.
Args:
torrent_hash: Hash of the torrent to delete
delete_files: Also delete downloaded files (default: False)
Returns:
Status message
"""
client = get_qbt_client()
try:
client.torrents_delete(delete_files=delete_files, torrent_hashes=torrent_hash)
return {
"status": "success",
"message": f"Torrent {torrent_hash} deleted (files deleted: {delete_files})"
}
except Exception as e:
return {"status": "error", "message": str(e)}
if __name__ == "__main__":
mcp.run()