forked from Tribler/tribler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swifttest.py
186 lines (151 loc) · 5.49 KB
/
swifttest.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
# Written by Arno Bakker, George Milescu
# see LICENSE.txt for license information
#
# Razvan Deaconescu, 2008:
# * corrected problem when running in background
# * added usage and print_version functions
# * uses getopt for command line argument parsing
import sys
import shutil
import time
import tempfile
import random
import os
import getopt
from traceback import print_exc
from Tribler.Core.API import *
from Tribler.Core.__init__ import version, report_email
STATUS_REPORT_INTERVAL = 3.0
# Print usage message
def usage():
print "Usage: python cmdlinedl.py [options] torrentfile_or_url"
print "Options:"
print "\t--port <port>"
print "\t-p <port>\t\tuse <port> to listen for connections"
print "\t\t\t\t(default is random value)"
print "\t--output <output-dir>"
print "\t-o <output-dir>\t\tuse <output-dir> for storing downloaded data"
print "\t\t\t\t(default is current directory)"
print "\t--version"
print "\t-v\t\t\tprint version and exit"
print "\t--help"
print "\t-h\t\t\tprint this help screen"
print
print "Report bugs to <" + report_email + ">"
# Print version information
def print_version():
print version, "<" + report_email + ">"
def states_callback(dslist):
for ds in dslist:
state_callback(ds)
return (STATUS_REPORT_INTERVAL, False)
# Print torrent statistics
def state_callback(ds):
d = ds.get_download()
# print >>sys.stderr,`d.get_def().get_name()`,dlstatus_strings[ds.get_status()],ds.get_progress(),"%",ds.get_error(),"up",ds.get_current_speed(UPLOAD),"down",ds.get_current_speed(DOWNLOAD)
print >>sys.stderr, '%s %s %5.2f%% %s up %8.2fKB/s down %8.2fKB/s' % \
(d.get_def().get_name(), \
dlstatus_strings[ds.get_status()], \
ds.get_progress() * 100, \
ds.get_error(), \
ds.get_current_speed(UPLOAD), \
ds.get_current_speed(DOWNLOAD))
"""
print >>sys.stderr, '\n\n%s %s peers %d leech %d' % \
(d.get_def().get_name(), \
dlstatus_strings[ds.get_status()], \
ds.get_num_peers(), \
ds.get_num_nonseeds() )
"""
return (STATUS_REPORT_INTERVAL, False)
def start_download(s,torrentfile_or_url,output_dir):
# setup and start download
dscfg = DownloadStartupConfig()
dscfg.set_dest_dir(output_dir);
#dscfg.set_max_speed( UPLOAD, 10 )
#dscfg.set_max_speed( DOWNLOAD, 512 )
# SWIFTPROC
if torrentfile_or_url.startswith("http") or torrentfile_or_url.startswith(P2PURL_SCHEME):
cdef = TorrentDef.load_from_url(torrentfile_or_url)
elif torrentfile_or_url.startswith(SWIFT_URL_SCHEME):
cdef = SwiftDef.load_from_url(torrentfile_or_url)
else:
cdef = TorrentDef.load(torrentfile_or_url)
if cdef.get_def_type() == "torrent" and cdef.get_live():
raise ValueError("cmdlinedl does not support live torrents")
d = s.start_download(cdef, dscfg)
d.set_state_callback(state_callback, getpeerlist=False)
return d
def main():
try:
# opts = a list of (option, value) pairs
# args = the list of program arguments left after the option list was stripped
opts, args = getopt.getopt(sys.argv[1:], "hvo:p:", ["help", "version", "output-dir", "port"])
except getopt.GetoptError, err:
print str(err)
usage()
sys.exit(2)
# init to default values
output_dir = os.getcwd()
port = random.randint(10000, 65535)
for o, a in opts:
if o in ("-h", "--help"):
usage()
sys.exit(0)
elif o in ("-o", "--output-dir"):
output_dir = a
elif o in ("-p", "--port"):
port = int(a)
elif o in ("-v", "--version"):
print_version()
sys.exit(0)
else:
assert False, "unhandled option"
if len(args) == 0:
usage()
sys.exit(2)
if len(args) > 1:
print "Too many arguments"
usage()
sys.exit(2)
torrentfile_or_url = args[0]
print "Press Ctrl-C to stop the download"
# setup session
sscfg = SessionStartupConfig()
statedir = tempfile.mkdtemp()
#statedir = '.test'
sscfg.set_state_dir(statedir)
sscfg.set_listen_port(port)
sscfg.set_megacache(False)
sscfg.set_overlay(False)
sscfg.set_dispersy(False)
sscfg.set_dialback(True)
sscfg.set_internal_tracker(False)
s = Session(sscfg)
time.sleep(1.0)
if True:
#torrentfile_or_url = 'tswift://127.0.0.1:20001/bc14d80f89cbbc812b5be93247e4c92ab8963b94'
#d = start_download(s,torrentfile_or_url,output_dir)
# Bunny
# url2 = 'tswift://127.0.0.1:20002/7c462ad1d980ba44ab4b819e29004eb0bf6e6d5f'
# d2 = start_download(s,url2,output_dir)
url3 = args[0]
d3 = start_download(s,url3,output_dir)
time.sleep(10)
s.checkpoint()
time.sleep(3600)
# Run for 20 s
#time.sleep(20)
#s.remove_download(d,removecontent=True)
#d.stop()
#time.sleep(10)
#d.restart()
else:
s.set_download_states_callback(states_callback)
s.load_checkpoint()
time.sleep(3600)
s.shutdown()
time.sleep(30)
#shutil.rmtree(statedir)
if __name__ == "__main__":
main()