/
transmission.py
83 lines (64 loc) · 2.65 KB
/
transmission.py
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
# Author: Geoffrey Huntley <ghuntley@ghuntley.com>
from flask import Flask, jsonify, render_template
import transmissionrpc
from datetime import timedelta
from maraschino.tools import *
from maraschino import app, logger
def app_link():
transmission_ip = get_setting_value('transmission_ip')
transmission_port = get_setting_value('transmission_ip')
return 'http://%s:%s/' % (transmission_ip, transmission_port)
def log_exception(e):
logger.log('Transmission :: EXCEPTION -- %s' % e, 'DEBUG')
@app.route('/xhr/transmission')
@app.route('/xhr/transmission/')
@requires_auth
def xhr_transmission():
# initialize empty list, which will be later populated with listing
# of active torrents [of transmissionrpc.Client().info()]
transmission = list()
seeding = list()
# initialize empty datetime, which will be later incremented
# to hold the total time remaining for all active downloads
eta = timedelta()
# transmissionrpc connection params
params = {
'address' : get_setting_value('transmission_ip'),
'port' : get_setting_value('transmission_port'),
'user' : get_setting_value('transmission_user') or None,
'password' : get_setting_value('transmission_password') or None
}
connection = False
try:
client = transmissionrpc.Client(**params)
stats = client.session_stats()
# return list of running jobs:
# {1: <Torrent 1 "Hello">, 2: <Torrent 2 "World">}
torrents = client.list()
if client is not None:
connection = True
# loop through each job, add any active (downloading) torrents to the transmission list()
for i in torrents:
torrent = client.info(i)[i]
if torrent.status == 'downloading':
transmission.append(torrent)
# take the ETA for the current torrent, add it to the total ETA remaining
if torrent.eta is not None:
eta = eta + torrent.eta
# if torrent is seeding instead, note as such
elif torrent.status == 'seeding':
seeding.append(torrent)
except Exception as e:
log_exception(e)
return render_template('transmission.html',
connection = connection,
applink=applink(),
show_empty = get_setting_value('transmission_show_empty') == '1',
transmission = transmission,
seeding = seeding,
upload = "%.1f" % (stats.uploadSpeed / 1024.0),
download = "%.1f" % (stats.downloadSpeed / 1024.0),
eta = eta,
seeds = len(seeding),
down = len(transmission),
)