-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyBTclient.py
executable file
·311 lines (251 loc) · 11.6 KB
/
pyBTclient.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# **********
# Filename: pyBTclient.py
# Description: A torrent client
# Author: Marc Vieira Cardinal
# Creation Date: July 16, 2014
# Revision Date: August 04, 2014
# Dependencies:
# apt-get install python-libtorrent
# Resources:
# http://www.rasterbar.com/products/libtorrent/python_binding.html
# http://www.rasterbar.com/products/libtorrent/reference-Core.html#torrent_handle
# **********
# External imports
import sys
import time
import urllib
import requests
import tempfile
import pyinotify
import libtorrent as lt
from argparse import ArgumentParser
# Application imports
import LogUtils
import Torrent
#############
# ActionMKTorrent
###
def ActionMKTorrent(logger, args):
torrent = Torrent.Torrent(logger)
torrent.WriteTorrentFile(args["destFile"],
args["sourceFile"],
args["trackerAnnUri"],
comment = "Created by pyBTclient")
#############
# ActionDNLDTorrent
###
def ActionDNLDTorrent(logger, args):
ses = lt.session()
ses.listen_on(args["portStart"],
args["portEnd"])
info = lt.torrent_info(args["torrentFile"])
h = ses.add_torrent({'ti': info,
'save_path': args["destPath"],
'storage_mode': (lt.storage_mode_t.storage_mode_allocate
if args["allocateStorage"]
else lt.storage_mode_t.storage_mode_sparse)
})
logger.info("Starting [%s]" % h.name())
while (not h.is_seed()):
s = h.status()
state_str = ['queued', 'checking', 'downloading metadata', \
'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume']
print '\r%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % \
(s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
s.num_peers, state_str[s.state]),
sys.stdout.flush()
time.sleep(1)
logger.info("Completed [%s]" % h.name())
#############
# ActionDNLDFromKey
###
def ActionDNLDFromKey(logger, args):
# Download the torrent identified by fileKey to a temp location
tempFile = tempfile.NamedTemporaryFile(delete = false)
logger.info("Retrieving key [%s] to [%s]" % (args["fileKey"], tempFile))
tempFile.write(urllib.urlopen(args["trackerGetUri"] + "?key=" + args["fileKey"]).read())
tempFile.close()
args["torrentFile"] = tempFile.name
# Pass the processing along to the regular
# torrent download routine
ActionDNLDTorrent(logger, args)
# Cleanup the temp file
tempFile.unlink(tempFile.name)
#############
# ActionPushTorrent
###
def ActionPushTorrent(logger, args):
requests.post(args["trackerPushUri"],
files = { "torrentFile": (args["fileKey"], open(args["torrentFile"], 'rb')) },
)
#############
# ActionAutoIndexer
###
def ActionAutoIndexer(logger, args):
logger.info("Watching [%s] and pushing to [%s]" % (args["trackerPushUri"], args["watchPaths"]))
eventHandler = ActionAutoIndexerEvents(logger, args)
watchManager = pyinotify.WatchManager()
for path in args["watchPaths"]:
watchManager.add_watch(path, pyinotify.IN_CLOSE_WRITE, rec = True)
notifier = pyinotify.Notifier(watchManager, eventHandler)
logger.info("Main loop starting...")
notifier.loop()
#############
# ActionAutoIndexerEvents
###
class ActionAutoIndexerEvents(pyinotify.ProcessEvent):
def __init__(self, logger, args):
self.logger = logger.getChild(self.__class__.__name__)
self.args = args
def process_IN_CLOSE_WRITE(self, event):
path = os.path.join(event.path, event.name)
self.logger("process_IN_CLOSE_WRITE -> %s" % path)
tempFile = tempfile.NamedTemporaryFile(delete = false)
tempFile.close()
ActionMKTorrent(logger, { "destFile": tempFile.name,
"sourceFile": path,
"trackerAnnUri": self.args["trackerAnnUri"] })
ActionPushTorrent(logger, { "fileKey": event.name,
"torrentFile": tempFile.name,
"trackerPushUri": self.args["trackerPushUri"] })
tempFile.unlink(tempFile.name)
#############
# ActionTests
###
def ActionTests(logger, args):
if args["testName"] == "tsize":
torrent = Torrent.Torrent(logger)
torrent.TestPieceSize()
#############
# Main
###
if __name__ == "__main__":
# Parse the command line arguments
# Define the main parser (top-level)
argParser = ArgumentParser()
argParser.add_argument("-f", "--foreground",
dest = "foreground",
action = "store_true",
default = False,
help = "Run in foreground")
argParser.add_argument("-l", "--loglevel",
dest = "logLevel",
choices = ["notset", "debug", "info",
"warning", "error", "critical"],
default = "notset",
help = "Logging level")
argParser.add_argument("-o", "--logfile",
dest = "logFile",
action = "store",
default = "/tmp/pyBTclient.log",
help = "Log file")
subParsers = argParser.add_subparsers(help = "sub-command help")
# Define the mktorrent sub-parser
mktorrentParser = subParsers.add_parser("mktorrent", help = "mktorrent help")
mktorrentParser.add_argument("sourceFile",
action = "store",
help = "Source file for the torrent")
mktorrentParser.add_argument("destFile",
action = "store",
help = "Filename of the torrent to be created")
mktorrentParser.add_argument("trackerAnnUri",
action = "store",
help = "The address of the announce endpoint")
mktorrentParser.set_defaults(func = ActionMKTorrent)
# Define the dnldtorrent sub-parser
dnldtorrentParser = subParsers.add_parser("dnldtorrent", help = "dnldtorrent help")
dnldtorrentParser.add_argument("torrentFile",
action = "store",
help = "Source torrent file")
dnldtorrentParser.add_argument("destPath",
action = "store",
help = "Location of the resulting file(s)")
dnldtorrentParser.add_argument("--port-start",
dest = "portStart",
action = "store",
type = int,
default = 6881,
help = "Location of the resulting file(s)")
dnldtorrentParser.add_argument("--port-end",
dest = "portEnd",
action = "store",
type = int,
default = 6981,
help = "Location of the resulting file(s)")
dnldtorrentParser.add_argument("--allocate-storage",
dest = "allocateStorage",
action = "store_true",
help = "This option does a full storage allocation")
dnldtorrentParser.set_defaults(func = ActionDNLDTorrent)
# Define the dnldfromkey sub-parser
dnldfromkeyParser = subParsers.add_parser("dnldfromkey", help = "dnldfromkey help")
dnldfromkeyParser.add_argument("fileKey",
action = "store",
help = "Key for the torrent definition")
dnldfromkeyParser.add_argument("destPath",
action = "store",
help = "Location of the resulting file(s)")
dnldfromkeyParser.add_argument("trackerGetUri",
action = "store",
help = "The address of the get endpoint")
dnldfromkeyParser.add_argument("--port-start",
dest = "portStart",
action = "store",
type = int,
default = 6881,
help = "Location of the resulting file(s)")
dnldfromkeyParser.add_argument("--port-end",
dest = "portEnd",
action = "store",
type = int,
default = 6981,
help = "Location of the resulting file(s)")
dnldfromkeyParser.add_argument("--allocate-storage",
dest = "allocateStorage",
action = "store_true",
help = "This option does a full storage allocation")
dnldfromkeyParser.set_defaults(func = ActionDNLDFromKey)
# Define the pushtorrent sub-parser
pushtorrentParser = subParsers.add_parser("pushtorrent", help = "pushtorrent help")
pushtorrentParser.add_argument("torrentFile",
action = "store",
help = "Source torrent file")
pushtorrentParser.add_argument("fileKey",
action = "store",
help = "Key for the torrent definition")
pushtorrentParser.add_argument("trackerPushUri",
action = "store",
help = "The address of the push endpoint")
pushtorrentParser.set_defaults(func = ActionPushTorrent)
# Define the autoindexer sub-parser
autoindexerParser = subParsers.add_parser("autoindexer", help = "autoindexer help")
autoindexerParser.add_argument("trackerAnnUri",
action = "store",
help = "The address of the announce endpoint")
autoindexerParser.add_argument("trackerPushUri",
action = "store",
help = "The address of the push endpoint")
autoindexerParser.add_argument("watchPaths",
action = "store",
nargs = argparse.REMAINDER,
help = "The address of the push endpoint")
autoindexerParser.set_defaults(func = ActionAutoIndexer)
# Define the tests sub-parser
testsParser = subParsers.add_parser("tests", help = "tests help")
testsParser.add_argument("testName",
choices = ["tsize"],
help = "Name of the test to run")
testsParser.set_defaults(func = ActionTests)
# Parse the command line arguments
argsObj = argParser.parse_args()
argsDict = vars(argParser.parse_args())
# Start the logging facilities
logger = LogUtils.RotatingFile(__name__,
argsDict["logLevel"],
argsDict["logFile"],
argsDict["foreground"])
logger.info("Started with arguments: " + str(argsDict))
# Run the action function
argsObj.func(logger, argsDict)