-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcTorrentDownload.py
275 lines (241 loc) · 10.3 KB
/
dcTorrentDownload.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
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
# Written by Bram Cohen
# see LICENSE.txt for license information
from BitTornado import PSYCO
if PSYCO.psyco:
try:
import psyco
assert psyco.__version__ >= 0x010100f0
psyco.full()
except:
pass
from BitTornado.download_bt1 import BT1Download, defaults, parse_params, get_usage, get_response
from BitTornado.RawServer import RawServer, UPnP_ERROR
from random import seed
from socket import error as socketerror
from BitTornado.bencode import bencode
from BitTornado.natpunch import UPnP_test
from threading import Event, Timer
from os.path import abspath
import sys
from sha import sha
from time import strftime, gmtime, time
from BitTornado.clock import clock
from BitTornado import createPeerID, version
from BitTornado.ConfigDir import ConfigDir
from dcTorrentDefaults import adjustDownloader
import logging
assert sys.version >= '2', "Install Python 2.0 or greater"
try:
True
except:
True = 1
False = 0
PROFILER = False
def hours(n):
if n == 0:
return 'complete!'
try:
n = int(n)
assert n >= 0 and n < 5184000 # 60 days
except:
return '<unknown>'
m, s = divmod(n, 60)
h, m = divmod(m, 60)
if h > 0:
return '%d hour %02d min %02d sec' % (h, m, s)
else:
return '%d min %02d sec' % (m, s)
def isotime(secs = None):
if secs == None:
secs = time()
return strftime('%Y-%m-%d %H:%M UTC', gmtime(secs))
class HeadlessDownloader:
def __init__(self):
self.done = False
self.file = ''
self.percentDone = ''
self.timeEst = ''
self.downloadTo = ''
self.downRate = ''
self.upRate = ''
self.shareRating = ''
self.seedStatus = ''
self.peerStatus = ''
self.errors = []
self.last_update_time = -1
self.doneflag = Event()
self.isDownloader = False
self.torrent = ''
self.averageDownSpeed = 0
self.averageUpSpeed = 0
self.callbackCount = 0
self.downloads = ''
def shutdown(self):
self.doneflag.set()
def finished(self):
self.done = True
self.percentDone = '100'
self.timeEst = 'Download Succeeded!'
self.downRate = ''
self.display()
self.logger.info('download {0} complete, down {1:.1f} kB/s, up {2:.1f} kB/s'.format(self.downloadTo, self.averageDownSpeed / (1<<10), self.averageUpSpeed /(1<<10)))
if self.isDownloader == True:
t = Timer(adjustDownloader['seed_after_finish'], self.shutdown)
t.start()
def failed(self):
self.done = True
self.percentDone = '0'
self.timeEst = 'Download Failed!'
self.downRate = ''
self.display()
self.doneflag.set()
def error(self, errormsg):
self.errors.append(errormsg)
self.display()
self.doneflag.set()
def displaySlim(self, dpflag = Event(), fractionDone = None, timeEst = None,
downRate = None, upRate = None, activity = None,
statistics = None, **kws):
dpflag.set()
def display(self, dpflag = Event(), fractionDone = None, timeEst = None,
downRate = None, upRate = None, activity = None,
statistics = None, **kws):
if self.last_update_time + 0.1 > clock() and fractionDone not in (0.0, 1.0) and activity is not None:
return
self.last_update_time = clock()
self.callbackCount +=1
if fractionDone is not None:
self.percentDone = str(float(int(fractionDone * 1000)) / 10)
if timeEst is not None:
self.timeEst = hours(timeEst)
if activity is not None and not self.done:
self.timeEst = activity
if downRate is not None:
self.downRate = '%.1f kB/s' % (float(downRate) / (1 << 10))
self.averageDownSpeed += (float(downRate) - self.averageDownSpeed)/self.callbackCount
if upRate is not None:
self.upRate = '%.1f kB/s' % (float(upRate) / (1 << 10))
self.averageUpSpeed += (float(upRate)-self.averageUpSpeed)/self.callbackCount
if statistics is not None:
if (statistics.shareRating < 0) or (statistics.shareRating > 100):
self.shareRating = 'oo (%.1f MB up / %.1f MB down)' % (float(statistics.upTotal) / (1<<20), float(statistics.downTotal) / (1<<20))
else:
self.shareRating = '%.3f (%.1f MB up / %.1f MB down)' % (statistics.shareRating, float(statistics.upTotal) / (1<<20), float(statistics.downTotal) / (1<<20))
if not self.done:
self.seedStatus = '%d seen now, plus %.3f distributed copies' % (statistics.numSeeds,0.001*int(1000*statistics.numCopies))
else:
self.seedStatus = '%d seen recently, plus %.3f distributed copies' % (statistics.numOldSeeds,0.001*int(1000*statistics.numCopies))
self.peerStatus = '%d seen now, %.1f%% done at %.1f kB/s' % (statistics.numPeers,statistics.percentDone,float(statistics.torrentRate) / (1 << 10))
self.downloads = ' '.join(statistics.downloads)
self.logger.debug( '\n\n\n\n')
for err in self.errors:
self.logger.error( 'ERROR:\n' + err + '\n')
self.logger.debug( 'saving: '+ self.file)
self.logger.debug( 'percent done: '+ self.percentDone)
self.logger.debug( 'time left: '+ self.timeEst)
self.logger.debug( 'download to: '+ self.downloadTo)
self.logger.debug( 'download rate: '+ self.downRate)
self.logger.debug( 'upload rate: '+ self.upRate)
self.logger.debug( 'share rating: '+ self.shareRating)
self.logger.debug( 'seed status: '+ self.seedStatus)
self.logger.debug( 'peer status: '+ self.peerStatus)
self.logger.debug( 'all downloads: '+ self.downloads)
self.logger.debug( 'average downRate: {0:.1f} kB/s'.format(self.averageDownSpeed/(1<<10)))
self.logger.debug( 'average upRate: {0:.1f} kB/s'.format(self.averageUpSpeed/(1<<10)))
self.logger.debug( 'call count: {0}'.format(self.callbackCount))
dpflag.set()
def chooseFile(self, default, size, saveas, dir):
self.file = '%s (%.1f MB)' % (default, float(size) / (1 << 20))
if saveas != '':
default = saveas
self.downloadTo = abspath(default)
return default
def newpath(self, path):
self.downloadTo = path
def download(self, params):
role = params[0]
if role == 'download':
self.isDownloader = True
self.logger = logging.getLogger(role)
params.remove(role)
while True:
configdir = ConfigDir('downloadheadless')
defaultsToIgnore = ['responsefile', 'url', 'priority']
configdir.setDefaults(defaults,defaultsToIgnore)
configdefaults = configdir.loadConfig()
defaults.append(('save_options',0,
"whether to save the current options as the new default configuration " +
"(only for btdownloadheadless.py)"))
try:
config = parse_params(params, configdefaults)
except ValueError, e:
print 'error: ' + str(e) + '\nrun with no args for parameter explanations'
break
if not config:
print get_usage(defaults, 80, configdefaults)
break
if config['save_options']:
configdir.saveConfig(config)
configdir.deleteOldCacheData(config['expire_cache_data'])
if config['url']:
self.torrent = config['url']
elif config['responsefile']:
self.torrent = config['responsefile']
for k in adjustDownloader:
config[k] = adjustDownloader[k]
myid = createPeerID()
seed(myid)
def disp_exception(text):
self.logger.info( text)
rawserver = RawServer(self.doneflag, config['timeout_check_interval'],
config['timeout'], ipv6_enable = config['ipv6_enabled'],
failfunc = self.failed, errorfunc = disp_exception)
upnp_type = UPnP_test(config['upnp_nat_access'])
while True:
try:
listen_port = rawserver.find_and_bind(config['minport'], config['maxport'],
config['bind'], ipv6_socket_style = config['ipv6_binds_v4'],
upnp = upnp_type, randomizer = config['random_port'])
break
except socketerror, e:
if upnp_type and e == UPnP_ERROR:
self.logger.info( 'WARNING: COULD NOT FORWARD VIA UPnP')
upnp_type = 0
continue
self.logger.info( "error: Couldn't listen - " + str(e))
self.failed()
return "error: Couldn't listen - " + str(e)
response = get_response(config['responsefile'], config['url'], self.error)
if not response:
break
infohash = sha(bencode(response['info'])).digest()
# seeder log doesn't need to be detailed
statusfunc = self.display if self.isDownloader else self.displaySlim
dow = BT1Download(statusfunc, self.finished, self.error, disp_exception, self.doneflag,
config, response, infohash, myid, rawserver, listen_port,
configdir)
if not dow.saveAs(self.chooseFile, self.newpath):
break
if not dow.initFiles(old_style = True):
break
if not dow.startEngine():
dow.shutdown()
break
dow.startRerequester()
dow.autoStats()
if not dow.am_I_finished():
self.display(activity = 'connecting to peers')
rawserver.listen_forever(dow.getPortHandler())
self.display(activity = 'shutting down')
dow.shutdown()
break
try:
rawserver.shutdown()
except:
pass
if not self.done:
self.failed()
return
if __name__ == '__main__':
h = HeadlessDownloader()
h.download(sys.argv[1:])