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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>qBittorrent Magnet Link Manager</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        header {
            text-align: center;
            color: white;
            margin-bottom: 30px;
        }

        header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
        }

        header p {
            font-size: 1.1em;
            opacity: 0.9;
        }

        .status-bar {
            background: white;
            padding: 15px 20px;
            border-radius: 8px;
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .status-bar .status {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .status-indicator {
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background-color: #4caf50;
            animation: pulse 2s infinite;
        }

        @keyframes pulse {
            0%, 100% {
                opacity: 1;
            }
            50% {
                opacity: 0.5;
            }
        }

        .input-section {
            background: white;
            padding: 30px;
            border-radius: 8px;
            margin-bottom: 30px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .input-section h2 {
            margin-bottom: 20px;
            color: #333;
        }

        .input-group {
            display: flex;
            gap: 10px;
        }

        input[type="text"] {
            flex: 1;
            padding: 12px 15px;
            border: 2px solid #e0e0e0;
            border-radius: 5px;
            font-size: 1em;
            transition: border-color 0.3s;
        }

        input[type="text"]:focus {
            outline: none;
            border-color: #667eea;
        }

        button {
            padding: 12px 30px;
            background-color: #667eea;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 1em;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #5568d3;
        }

        button:active {
            transform: scale(0.98);
        }

        .torrents-section {
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .torrents-section h2 {
            margin-bottom: 20px;
            color: #333;
        }

        .torrent-list {
            display: flex;
            flex-direction: column;
            gap: 15px;
        }

        .torrent-item {
            background: #f9f9f9;
            padding: 20px;
            border-radius: 8px;
            border-left: 4px solid #667eea;
            transition: transform 0.2s, box-shadow 0.2s;
        }

        .torrent-item:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }

        .torrent-header {
            display: flex;
            justify-content: space-between;
            align-items: start;
            margin-bottom: 15px;
        }

        .torrent-name {
            font-weight: 600;
            color: #333;
            word-break: break-word;
            flex: 1;
        }

        .torrent-state {
            padding: 5px 10px;
            border-radius: 20px;
            font-size: 0.85em;
            font-weight: 500;
            text-transform: uppercase;
            margin-left: 10px;
        }

        .state-downloading {
            background-color: #e3f2fd;
            color: #1976d2;
        }

        .state-completed {
            background-color: #e8f5e9;
            color: #388e3c;
        }

        .state-paused {
            background-color: #fff3e0;
            color: #f57c00;
        }

        .state-error {
            background-color: #ffebee;
            color: #c62828;
        }

        .torrent-progress {
            margin-bottom: 10px;
        }

        .progress-bar {
            width: 100%;
            height: 8px;
            background-color: #e0e0e0;
            border-radius: 4px;
            overflow: hidden;
            margin-bottom: 5px;
        }

        .progress-fill {
            height: 100%;
            background: linear-gradient(90deg, #667eea, #764ba2);
            transition: width 0.3s;
        }

        .progress-text {
            font-size: 0.9em;
            color: #666;
            display: flex;
            justify-content: space-between;
        }

        .torrent-stats {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
            gap: 15px;
            margin-bottom: 15px;
            font-size: 0.9em;
        }

        .stat {
            display: flex;
            flex-direction: column;
        }

        .stat-label {
            color: #999;
            font-size: 0.85em;
            text-transform: uppercase;
            margin-bottom: 3px;
        }

        .stat-value {
            color: #333;
            font-weight: 600;
        }

        .torrent-actions {
            display: flex;
            gap: 10px;
        }

        .action-btn {
            padding: 8px 15px;
            font-size: 0.9em;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .pause-btn {
            background-color: #ff9800;
            color: white;
        }

        .pause-btn:hover {
            background-color: #f57c00;
        }

        .resume-btn {
            background-color: #4caf50;
            color: white;
        }

        .resume-btn:hover {
            background-color: #45a049;
        }

        .delete-btn {
            background-color: #f44336;
            color: white;
        }

        .delete-btn:hover {
            background-color: #da190b;
        }

        .empty-state {
            text-align: center;
            padding: 40px 20px;
            color: #999;
        }

        .empty-state-icon {
            font-size: 3em;
            margin-bottom: 10px;
        }

        .loading {
            text-align: center;
            padding: 20px;
            color: #667eea;
        }

        .spinner {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #667eea;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            animation: spin 1s linear infinite;
            margin: 0 auto 10px;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .alert {
            padding: 15px 20px;
            border-radius: 5px;
            margin-bottom: 20px;
            display: none;
        }

        .alert.show {
            display: block;
        }

        .alert-success {
            background-color: #e8f5e9;
            color: #2e7d32;
            border-left: 4px solid #4caf50;
        }

        .alert-error {
            background-color: #ffebee;
            color: #c62828;
            border-left: 4px solid #f44336;
        }

        @media (max-width: 768px) {
            header h1 {
                font-size: 1.8em;
            }

            .input-group {
                flex-direction: column;
            }

            .torrent-stats {
                grid-template-columns: repeat(2, 1fr);
            }

            .torrent-actions {
                flex-wrap: wrap;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>πŸš€ qBittorrent Magnet Link Manager</h1>
            <p>Add magnet links and track your downloads in real-time</p>
        </header>

        <div class="status-bar">
            <div class="status">
                <div class="status-indicator"></div>
                <span id="status-text">Connecting to qBittorrent...</span>
            </div>
            <div id="refresh-info" style="font-size: 0.9em; color: #666;"></div>
        </div>

        <div id="alert" class="alert"></div>

        <div class="input-section">
            <h2>Add Magnet Link</h2>
            <div class="input-group">
                <input 
                    type="text" 
                    id="magnet-input" 
                    placeholder="Paste your magnet link here (e.g., magnet:?xt=urn:btih:...)"
                    autocomplete="off"
                >
                <button onclick="addTorrent()">Add Torrent</button>
            </div>
        </div>

        <div class="torrents-section">
            <h2>Active Downloads</h2>
            <div id="torrents-container" class="loading">
                <div class="spinner"></div>
                <p>Loading torrents...</p>
            </div>
        </div>
    </div>

    <script>
        const API_BASE = '';
        const REFRESH_INTERVAL = 2000; // Refresh every 2 seconds
        let refreshTimer = null;

        // Format bytes to human-readable format
        function formatBytes(bytes) {
            if (bytes === 0) return '0 B';
            const k = 1024;
            const sizes = ['B', 'KB', 'MB', 'GB'];
            const i = Math.floor(Math.log(bytes) / Math.log(k));
            return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i];
        }

        // Format seconds to human-readable time
        function formatTime(seconds) {
            if (seconds < 0 || seconds === Infinity) return '∞';
            const hours = Math.floor(seconds / 3600);
            const minutes = Math.floor((seconds % 3600) / 60);
            const secs = Math.floor(seconds % 60);
            
            if (hours > 0) return `${hours}h ${minutes}m`;
            if (minutes > 0) return `${minutes}m ${secs}s`;
            return `${secs}s`;
        }

        // Show alert message
        function showAlert(message, type = 'success') {
            const alertEl = document.getElementById('alert');
            alertEl.textContent = message;
            alertEl.className = `alert show alert-${type}`;
            setTimeout(() => {
                alertEl.classList.remove('show');
            }, 5000);
        }

        // Check health and update status
        async function checkHealth() {
            try {
                const response = await fetch(`${API_BASE}/health`);
                if (response.ok) {
                    document.getElementById('status-text').textContent = 'βœ“ Connected to qBittorrent';
                } else {
                    document.getElementById('status-text').textContent = 'βœ— Connection error';
                }
            } catch (error) {
                document.getElementById('status-text').textContent = 'βœ— Connection error';
            }
        }

        // Fetch and display torrents
        async function fetchTorrents() {
            try {
                const response = await fetch(`${API_BASE}/api/torrents`);
                if (!response.ok) throw new Error('Failed to fetch torrents');
                
                const data = await response.json();
                displayTorrents(data.torrents);
            } catch (error) {
                console.error('Error fetching torrents:', error);
                document.getElementById('torrents-container').innerHTML = `
                    <div class="empty-state">
                        <div class="empty-state-icon">⚠️</div>
                        <p>Error loading torrents. Please check your connection.</p>
                    </div>
                `;
            }
        }

        // Display torrents in the UI
        function displayTorrents(torrents) {
            const container = document.getElementById('torrents-container');
            
            if (torrents.length === 0) {
                container.innerHTML = `
                    <div class="empty-state">
                        <div class="empty-state-icon">πŸ“­</div>
                        <p>No torrents yet. Add a magnet link to get started!</p>
                    </div>
                `;
                return;
            }

            container.innerHTML = '<div class="torrent-list">' + torrents.map(torrent => {
                const stateClass = `state-${torrent.state.toLowerCase()}`;
                const isPaused = torrent.state.toLowerCase() === 'paused';
                const isCompleted = torrent.state.toLowerCase() === 'uploading' || torrent.progress === 100;
                
                return `
                    <div class="torrent-item">
                        <div class="torrent-header">
                            <div class="torrent-name">${escapeHtml(torrent.name)}</div>
                            <div class="torrent-state ${stateClass}">${torrent.state}</div>
                        </div>
                        
                        <div class="torrent-progress">
                            <div class="progress-bar">
                                <div class="progress-fill" style="width: ${torrent.progress}%"></div>
                            </div>
                            <div class="progress-text">
                                <span>${torrent.progress.toFixed(1)}%</span>
                                <span>${formatBytes(torrent.downloaded)} / ${formatBytes(torrent.total_size)}</span>
                            </div>
                        </div>
                        
                        <div class="torrent-stats">
                            <div class="stat">
                                <div class="stat-label">Download Speed</div>
                                <div class="stat-value">${formatBytes(torrent.download_speed)}/s</div>
                            </div>
                            <div class="stat">
                                <div class="stat-label">Upload Speed</div>
                                <div class="stat-value">${formatBytes(torrent.upload_speed)}/s</div>
                            </div>
                            <div class="stat">
                                <div class="stat-label">ETA</div>
                                <div class="stat-value">${formatTime(torrent.eta)}</div>
                            </div>
                            <div class="stat">
                                <div class="stat-label">Seeds / Peers</div>
                                <div class="stat-value">${torrent.num_seeds} / ${torrent.num_leechs}</div>
                            </div>
                        </div>
                        
                        <div class="torrent-actions">
                            ${isPaused ? 
                                `<button class="action-btn resume-btn" onclick="resumeTorrent('${torrent.hash}')">Resume</button>` :
                                `<button class="action-btn pause-btn" onclick="pauseTorrent('${torrent.hash}')">Pause</button>`
                            }
                            <button class="action-btn delete-btn" onclick="deleteTorrent('${torrent.hash}')">Delete</button>
                        </div>
                    </div>
                `;
            }).join('') + '</div>';
        }

        // Escape HTML to prevent XSS
        function escapeHtml(text) {
            const map = {
                '&': '&amp;',
                '<': '&lt;',
                '>': '&gt;',
                '"': '&quot;',
                "'": '&#039;'
            };
            return text.replace(/[&<>"']/g, m => map[m]);
        }

        // Add a new torrent
        async function addTorrent() {
            const input = document.getElementById('magnet-input');
            const magnetLink = input.value.trim();
            
            if (!magnetLink) {
                showAlert('Please enter a magnet link', 'error');
                return;
            }
            
            try {
                const response = await fetch(`${API_BASE}/api/add_torrent`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ magnet_link: magnetLink })
                });
                
                if (!response.ok) {
                    const error = await response.json();
                    throw new Error(error.detail || 'Failed to add torrent');
                }
                
                showAlert('Torrent added successfully!', 'success');
                input.value = '';
                await fetchTorrents();
            } catch (error) {
                showAlert(`Error: ${error.message}`, 'error');
            }
        }

        // Pause a torrent
        async function pauseTorrent(hash) {
            try {
                const response = await fetch(`${API_BASE}/api/torrents/${hash}/pause`, {
                    method: 'POST'
                });
                
                if (!response.ok) throw new Error('Failed to pause torrent');
                
                showAlert('Torrent paused', 'success');
                await fetchTorrents();
            } catch (error) {
                showAlert(`Error: ${error.message}`, 'error');
            }
        }

        // Resume a torrent
        async function resumeTorrent(hash) {
            try {
                const response = await fetch(`${API_BASE}/api/torrents/${hash}/resume`, {
                    method: 'POST'
                });
                
                if (!response.ok) throw new Error('Failed to resume torrent');
                
                showAlert('Torrent resumed', 'success');
                await fetchTorrents();
            } catch (error) {
                showAlert(`Error: ${error.message}`, 'error');
            }
        }

        // Delete a torrent
        async function deleteTorrent(hash) {
            if (!confirm('Are you sure you want to delete this torrent?')) return;
            
            try {
                const response = await fetch(`${API_BASE}/api/torrents/${hash}`, {
                    method: 'DELETE'
                });
                
                if (!response.ok) throw new Error('Failed to delete torrent');
                
                showAlert('Torrent deleted', 'success');
                await fetchTorrents();
            } catch (error) {
                showAlert(`Error: ${error.message}`, 'error');
            }
        }

        // Allow Enter key to add torrent
        document.getElementById('magnet-input').addEventListener('keypress', function(event) {
            if (event.key === 'Enter') {
                addTorrent();
            }
        });

        // Initialize
        window.addEventListener('load', async () => {
            await checkHealth();
            await fetchTorrents();
            
            // Set up auto-refresh
            refreshTimer = setInterval(async () => {
                await fetchTorrents();
            }, REFRESH_INTERVAL);
        });

        // Clean up on page unload
        window.addEventListener('beforeunload', () => {
            if (refreshTimer) clearInterval(refreshTimer);
        });
    </script>
</body>
</html>