本文整理汇总了Python中BaseLib.Core.TorrentDef.TorrentDef.save方法的典型用法代码示例。如果您正苦于以下问题:Python TorrentDef.save方法的具体用法?Python TorrentDef.save怎么用?Python TorrentDef.save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BaseLib.Core.TorrentDef.TorrentDef
的用法示例。
在下文中一共展示了TorrentDef.save方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_torrent
# 需要导入模块: from BaseLib.Core.TorrentDef import TorrentDef [as 别名]
# 或者: from BaseLib.Core.TorrentDef.TorrentDef import save [as 别名]
def create_torrent(file, port, try_vod = True):
'''
Creates a torrent for the given file. Returns a string representing the
path to the torrent file.
If try_vod is True try to add the playtime to the torrent making it "streamable".
'''
# generate torrent
tdef = TorrentDef()
url = "http://localhost:%s/announce/" % port
tdef.set_tracker(url)
#tdef.set_create_merkle_torrent(True)
#tdef.set_piece_length(self._pieceSize)
if try_vod:
torrent_name = FileUtils.get_relative_filename(file) + constants.TORRENT_VOD_EXT
# set playtime
playtime = videostats_via_ffmpeg(file)['playtime']
print "playtime", playtime
else:
torrent_name = FileUtils.get_relative_filename(file) + constants.TORRENT_DOWNLOAD_EXT
playtime = None
print "Create a non-streamable torrent"
tdef.add_content(file, playtime=playtime)
tdef.finalize()
torrent_dir = os.getcwd()
torrent = os.path.join(torrent_dir, torrent_name)
tdef.save(torrent)
print "created torrent file: %s" % torrent
print "Tracker uses the announce URL: %s" % tdef.get_tracker()
return tdef
示例2: testECreateRichTorrents
# 需要导入模块: from BaseLib.Core.TorrentDef import TorrentDef [as 别名]
# 或者: from BaseLib.Core.TorrentDef.TorrentDef import save [as 别名]
def testECreateRichTorrents(self):
_log.info("Create rich metadata torrents ------------")
if not TestMetaGenerator.run_test:
_log.debug("Test avoided.")
return
data = None
self.assertTrue(TestMetaGenerator.data != None, "Test E depends on data gathered in test D, set testD attribute to True")
if TestMetaGenerator.data != None:
data = TestMetaGenerator.data
else:
return
rmg = RichMetadataGenerator.getInstance()
torrents = []
for k, v in data.items():
if v.content_base != None:
# Choose only a small subset of content to download
if v.content_base == "http://www.rtvslo.si/podcasts/gori_doli_naokoli.xml":
mm_file = tempfile.NamedTemporaryFile()
try:
urllib.urlretrieve(v.id, mm_file.name)
torrent_def = TorrentDef()
torrent_def.add_content(mm_file.name)
torrent_def.set_tracker("http://wanabe.a.tracker.com:8080")
torrent_def.set_metadata(k)
torrent_def.finalize()
torrent_def.save(mm_file.name + ".torrent")
torrents.append(mm_file.name + ".torrent")
except Exception, e:
_log.error("Failed to generate torrent file, reason: " + str(e))
示例3: TestVideoOnDemand
# 需要导入模块: from BaseLib.Core.TorrentDef import TorrentDef [as 别名]
# 或者: from BaseLib.Core.TorrentDef.TorrentDef import save [as 别名]
class TestVideoOnDemand(TestAsServer):
"""
Testing Merkle hashpiece messages for both:
* Merkle BEP style
* old Tribler <= 4.5.2 that did not use the Extention protocol (BEP 10).
See BitTornado/BT1/Connecter.py
"""
def setUp(self):
""" override TestAsServer """
TestAsServer.setUp(self)
self.vodstarted = False
def setUpPreSession(self):
""" override TestAsServer """
TestAsServer.setUpPreSession(self)
self.config.set_overlay(False)
self.config.set_megacache(False)
def setUpPostSession(self):
""" override TestAsServer """
TestAsServer.setUpPostSession(self)
def tearDown(self):
print >> sys.stderr, time.asctime(), "-", "Test: Sleep before tear down"
time.sleep(10)
TestAsServer.tearDown(self)
def create_torrent(self):
[srchandle, self.sourcefn] = mkstemp()
self.content = Rand.rand_bytes(self.contentlen)
os.write(srchandle, self.content)
os.close(srchandle)
self.tdef = TorrentDef()
self.tdef.add_content(self.sourcefn)
self.tdef.set_piece_length(self.piecelen)
self.tdef.set_tracker("http://127.0.0.1:12/announce")
self.tdef.finalize()
self.torrentfn = os.path.join(self.session.get_state_dir(), "gen.torrent")
self.tdef.save(self.torrentfn)
dscfg = DownloadStartupConfig()
destdir = os.path.dirname(self.sourcefn)
dscfg.set_dest_dir(destdir)
dscfg.set_video_event_callback(self.sesscb_vod_event_callback)
self.session.set_download_states_callback(self.states_callback)
self.session.start_download(self.tdef, dscfg)
def states_callback(self, dslist):
ds = dslist[0]
d = ds.get_download()
# print >>sys.stderr,time.asctime(),'-', `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, time.asctime(), "-", "%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),
)
return (1.0, False)
def sesscb_vod_event_callback(self, d, event, params):
if self.vodstarted:
return
self.vodstarted = True
print >> sys.stderr, time.asctime(), "-", "Test: vod_event_callback", event, params
if event == VODEVENT_START:
stream = params["stream"]
# Read last piece
lastpieceoff = ((self.contentlen - 1) / self.piecelen) * self.piecelen
lastpiecesize = self.contentlen - lastpieceoff
print >> sys.stderr, time.asctime(), "-", "Test: stream: lastpieceoff", lastpieceoff, lastpiecesize
self.stream_read(stream, lastpieceoff, lastpiecesize, self.piecelen)
# Read second,3rd,4th byte, only
secoff = 1
secsize = 3
blocksize = 3
self.stream_read(stream, secoff, secsize, blocksize)
# Read last byte
lastoff = self.contentlen - 1
lastsize = 1
self.stream_read(stream, lastoff, lastsize, self.piecelen)
print >> sys.stderr, time.asctime(), "-", "Test: stream: Passed?"
def stream_read(self, stream, off, size, blocksize):
stream.seek(off)
#.........这里部分代码省略.........
示例4: TestMerkleMessage
# 需要导入模块: from BaseLib.Core.TorrentDef import TorrentDef [as 别名]
# 或者: from BaseLib.Core.TorrentDef.TorrentDef import save [as 别名]
class TestMerkleMessage(TestAsServer):
"""
Testing Merkle hashpiece messages for both:
* Merkle BEP style
* old Tribler <= 4.5.2 that did not use the Extention protocol (BEP 10).
See BitTornado/BT1/Connecter.py
"""
def setUp(self):
""" override TestAsServer """
TestAsServer.setUp(self)
print >>sys.stderr,"test: Giving Session time to startup"
time.sleep(5)
print >>sys.stderr,"test: Session should have started up"
def setUpPreSession(self):
""" override TestAsServer """
TestAsServer.setUpPreSession(self)
self.config.set_overlay(False)
self.config.set_megacache(False)
def setUpPostSession(self):
""" override TestAsServer """
TestAsServer.setUpPostSession(self)
# Let Tribler start downloading an non-functioning torrent, so
# we can talk to a normal download engine.
self.tdef = TorrentDef()
self.sourcefn = os.path.join(os.getcwd(),"API","file2.wmv")
self.tdef.add_content(self.sourcefn)
self.tdef.set_create_merkle_torrent(True)
self.tdef.set_tracker("http://127.0.0.1:12/announce")
self.tdef.finalize()
self.torrentfn = os.path.join(self.session.get_state_dir(),"gen.torrent")
self.tdef.save(self.torrentfn)
dscfg = self.setUpDownloadConfig()
self.session.start_download(self.tdef,dscfg)
self.infohash = self.tdef.get_infohash()
self.mylistenport = 4810
self.numpieces = (self.tdef.get_length()+self.tdef.get_piece_length()-1) / self.tdef.get_piece_length()
b = Bitfield(self.numpieces)
for i in range(self.numpieces):
b[i] = True
self.assert_(b.complete())
self.seederbitfieldstr = b.tostring()
#piece_hashes = ['\x01\x02\x03\x04\x05\x06\x07\x08\x07\x06\x05\x04\x03\x02\x01\x00\x01\x02\x03\x04' ] * npieces
# Construct Merkle tree
tdef2 = TorrentDef()
tdef2.add_content(self.sourcefn)
tdef2.set_create_merkle_torrent(False)
tdef2.set_tracker("http://127.0.0.1:12/announce")
tdef2.set_piece_length(self.tdef.get_piece_length())
tdef2.finalize()
metainfo = tdef2.get_metainfo()
piecesstr = metainfo['info']['pieces']
print >>sys.stderr,"test: pieces has len",len(piecesstr)
piece_hashes = []
for i in range(0,len(piecesstr),20):
hash = piecesstr[i:i+20]
print >>sys.stderr,"test: piece",i/20,"hash",`hash`
piece_hashes.append(hash)
print >>sys.stderr,"test: Putting",len(piece_hashes),"into MerkleTree, size",self.tdef.get_piece_length(),tdef2.get_piece_length()
self.tree = MerkleTree(self.tdef.get_piece_length(),self.tdef.get_length(),None,piece_hashes)
f = open(self.sourcefn,"rb")
piece1 = f.read(2 ** 18)
piece2 = f.read(2 ** 18)
print >>sys.stderr,"read piece1",len(piece1)
print >>sys.stderr,"read piece2",len(piece2)
f.close()
hash1 = sha(piece1).digest()
hash2 = sha(piece2).digest()
print >>sys.stderr,"hash piece1",`hash1`
print >>sys.stderr,"hash piece2",`hash2`
f2 = open("piece1.bin","wb")
f2.write(piece2)
f2.close()
def setUpDownloadConfig(self):
dscfg = DownloadStartupConfig()
print >>sys.stderr,"test: Downloading to",self.config_path
dscfg.set_dest_dir(self.config_path)
dscfg.set_breakup_seed_bitfield(False)
return dscfg
def tearDown(self):
#.........这里部分代码省略.........