-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbtdownloadpsn
165 lines (150 loc) · 4.76 KB
/
btdownloadpsn
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
#! /usr/bin/python
# Written by Bram Cohen
# see LICENSE.txt for license information
from BitTorrent.download import download
from threading import Event
from os.path import abspath
import os, socket
from sys import argv, stdout
from cStringIO import StringIO
from time import time
from datetime import datetime
def hours(n):
if n == -1:
return '<unknown>'
if n == 0:
return 'complete!'
n = long(n)
h, r = divmod(n, 60 * 60)
m, sec = divmod(r, 60)
if h > 1000000:
return '<unknown>'
if h > 0:
return '%d hour %02d min %02d sec' % (h, m, sec)
else:
return '%d min %02d sec' % (m, sec)
class HeadlessDisplayer:
def __init__(self):
self.done = False
self.alldone = False
self.file = ''
self.percentDone = ''
self.timeEst = ''
self.downloadTo = ''
self.downRate = ''
self.upRate = ''
self.downTotal = ''
self.upTotal = ''
self.errors = []
self.started = None
self.last_update_time = 0
def finished(self):
self.done = True
self.percentDone = '100'
self.timeEst = 'Download Succeeded!'
self.downRate = ''
self.display({})
def failed(self):
self.done = True
self.percentDone = '0'
self.timeEst = 'Download Failed!'
self.downRate = ''
self.display({})
def error(self, errormsg):
self.errors.append(errormsg)
if len(self.errors) > 5:
self.errors = self.errors[-5:]
self.display({})
def display(self, dict):
if self.last_update_time + 0.1 > time() and dict.get('fractionDone') not in (0.0, 1.0) and not dict.has_key('activity'):
return
self.last_update_time = time()
if not self.started:
self.started = datetime.now()
if dict.has_key('spew'):
print_spew(dict['spew'])
if dict.has_key('fractionDone'):
self.percentDone = str(float(int(dict['fractionDone'] * 1000)) / 10)
if dict.has_key('timeEst'):
self.timeEst = hours(dict['timeEst'])
if dict.has_key('activity') and not self.done:
self.timeEst = dict['activity']
if dict.has_key('downRate'):
self.downRate = '%.2f K/s' % (float(dict['downRate']) / (1 << 10))
if dict.has_key('upRate'):
self.upRate = '%.2f K/s' % (float(dict['upRate']) / (1 << 10))
if dict.has_key('upTotal'):
self.upTotal = '%.1f M' % (dict['upTotal'])
if dict.has_key('downTotal'):
self.downTotal = '%.1f M' % (dict['downTotal'])
if not self.done and self.started:
print '\n\ndatetime::downrate::uprate'
str = datetime.now() + "::" + self.downRate + "::" + self.upRate
stdout.flush()
elif not self.alldone and self.started:
print datetime.now()
print 'time_spent ' + str(datetime.now() - self.started)
stdout.flush()
self.alldone = True
Popen([os.path.join(abspath, "callbackpsn.exp"), socket.gethostname(), os.environ["EXPERIMENT"]]).wait()
def chooseFile(self, default, size, saveas, dir):
self.file = '%s (%.1f M)' % (default, float(size) / (1 << 20))
if saveas != '':
default = saveas
self.downloadTo = abspath(default)
return default
def newpath(self, path):
self.downloadTo = path
def print_spew(spew):
s = StringIO()
s.write('\n\n\n')
for c in spew:
s.write('%20s ' % c['ip'])
if c['initiation'] == 'local':
s.write('l')
else:
s.write('r')
rate, interested, choked = c['upload']
s.write(' %10s ' % str(int(rate)))
if c['is_optimistic_unchoke']:
s.write('*')
else:
s.write(' ')
if interested:
s.write('i')
else:
s.write(' ')
if choked:
s.write('c')
else:
s.write(' ')
rate, interested, choked, snubbed = c['download']
s.write(' %10s ' % str(int(rate)))
if interested:
s.write('i')
else:
s.write(' ')
if choked:
s.write('c')
else:
s.write(' ')
if snubbed:
s.write('s')
else:
s.write(' ')
s.write('\n')
print s.getvalue()
def run(params):
try:
import curses
curses.initscr()
cols = curses.COLS
curses.endwin()
except:
cols = 80
h = HeadlessDisplayer()
download(params, h.chooseFile, h.display, h.finished, h.error, Event(), cols, h.newpath)
if not h.done:
h.failed()
if __name__ == '__main__':
run(argv[1:])