def _create_and_reseed(self, session): # 1. Create a 500K randomdata file storagepath = os.path.join(self.getDestDir(), "output_file") with open(storagepath, 'wb') as fout: fout.write(os.urandom(512000)) # 2. Create the SwiftDef sdef = SwiftDef() sdef.set_tracker("127.0.0.1:%d" % session.get_swift_dht_listen_port()) sdef.add_content(storagepath) sdef.finalize(session.get_swift_path(), destdir=self.getDestDir()) # 3. Save swift files to metadata dir metadir = session.get_swift_meta_dir() metapath = os.path.join(metadir, "output_file") try: move(storagepath + '.mhash', metapath + '.mhash') move(storagepath + '.mbinmap', metapath + '.mbinmap') except: print_exc() # 4. Start seeding this file defaultDLConfig = DefaultDownloadStartupConfig.getInstance() dscfg = defaultDLConfig.copy() dscfg.set_dest_dir(storagepath) d = session.start_download(sdef, dscfg) d.set_state_callback(self.seeder_state_callback) return sdef.get_id()
def _write_to_collected(self, filename): #calculate root-hash sdef = SwiftDef() sdef.add_content(filename) sdef.finalize(self.session.get_swift_path(), destdir = self.session.get_torrent_collecting_dir()) mfpath = os.path.join(self.session.get_torrent_collecting_dir(),sdef.get_roothash_as_hex()) if not os.path.exists(mfpath): if os.path.exists(mfpath + ".mhash"): #indicating active swift download download = self.session.get_download(sdef.get_roothash()) if download: self.session.remove_download(download, removestate = True) sleep(1) else: os.remove(mfpath + ".mhash") try: shutil.copy(filename, mfpath) shutil.move(filename+'.mhash', mfpath+'.mhash') shutil.move(filename+'.mbinmap', mfpath+'.mbinmap') except: print_exc() return sdef, mfpath
def _write_to_collected(self, filename): #calculate root-hash sdef = SwiftDef() sdef.add_content(filename) sdef.finalize(self.session.get_swift_path(), destdir=self.session.get_torrent_collecting_dir()) mfpath = os.path.join(self.session.get_torrent_collecting_dir(), sdef.get_roothash_as_hex()) if not os.path.exists(mfpath): download = self.session.get_download(sdef.get_roothash()) if download: self.session.remove_download(download, removestate=True) sleep(1) elif os.path.exists(mfpath + ".mhash"): #indicating failed swift download os.remove(mfpath + ".mhash") try: shutil.copy(filename, mfpath) shutil.move(filename + '.mhash', mfpath + '.mhash') shutil.move(filename + '.mbinmap', mfpath + '.mbinmap') except: print_exc() return sdef, mfpath
def __torrentdef_retrieved(self, tdef): infohash = tdef.get_infohash() if DEBUG: print >>sys.stderr, "magnetrequester: received torrent", bin2str(infohash) # remove from requested list if infohash in self.requestedInfohashes: self.requestedInfohashes.remove(infohash) # save torrent torrent = self.torrent_db.getTorrent(infohash, ["torrent_file_name"], include_mypref=False) if torrent and torrent.get("torrent_file_name", False) and not os.path.isabs(torrent["torrent_file_name"]): torrent_filename = os.path.join(self.metadatahandler.torrent_dir, torrent["torrent_file_name"]) else: torrent_filename = os.path.join( self.metadatahandler.torrent_dir, get_collected_torrent_filename(infohash) ) tdef.save(torrent_filename) # calculate root-hash sdef = SwiftDef() sdef.add_content(torrent_filename) sdef.finalize(self.session.get_swift_path()) # add this new torrent to db self.torrent_db.addExternalTorrent(tdef, extra_info={"swift_torrent_hash": bin2str(sdef.get_roothash())}) # notify all self.remoteTorrentHandler.metadatahandler_got_torrent(infohash, tdef, torrent_filename) self.overlay_bridge.add_task(self.__requestMagnet, self.REQUEST_INTERVAL)
def __torrentdef_retrieved(self, tdef): infohash = tdef.get_infohash() if DEBUG: print >> sys.stderr, 'magnetrequester: received torrent', bin2str( infohash) #remove from requested list if infohash in self.requestedInfohashes: self.requestedInfohashes.remove(infohash) #save torrent torrent = self.torrent_db.getTorrent(infohash, ['torrent_file_name'], include_mypref=False) if torrent and torrent.get( 'torrent_file_name', False) and not os.path.isabs(torrent['torrent_file_name']): torrent_filename = os.path.join( self.metadatahandler.torrent_dir, torrent['torrent_file_name']) else: torrent_filename = os.path.join( self.metadatahandler.torrent_dir, get_collected_torrent_filename(infohash)) tdef.save(torrent_filename) #calculate root-hash sdef = SwiftDef() sdef.add_content(torrent_filename) sdef.finalize(self.session.get_swift_path()) #add this new torrent to db self.torrent_db.addExternalTorrent(tdef, extra_info={ 'swift_torrent_hash': bin2str(sdef.get_roothash()) }) #notify all self.remoteTorrentHandler.metadatahandler_got_torrent( infohash, tdef, torrent_filename) self.overlay_bridge.add_task(self.__requestMagnet, self.REQUEST_INTERVAL)
def _create_and_seed_metadata(self, videofile, torrent): from Tribler.Main.vwxGUI.GuiUtility import GUIUtility if prctlimported: prctl.set_name("Tribler" + currentThread().getName()) self.guiutility = GUIUtility.getInstance() self.session = self.guiutility.utility.session videoanalyser = self.session.get_video_analyser_path() torcoldir = self.session.get_torrent_collecting_dir() rel_thumbdir = 'thumbs-' + binascii.hexlify(torrent.infohash) abs_thumbdir = os.path.join(torcoldir, rel_thumbdir) videoname = os.path.basename(videofile) if os.path.exists(abs_thumbdir): if DEBUG: print >> sys.stderr, 'create_and_seed_metadata: already downloaded thumbnails for torrent', torrent.name return if DEBUG: print >> sys.stderr, 'create_and_seed_metadata: going to seed metadata for torrent', torrent.name duration, bitrate, resolution = get_videoinfo(videofile, videoanalyser) video_info = {'duration': duration, \ 'bitrate': bitrate, \ 'resolution': resolution} if DEBUG: print >> sys.stderr, 'create_and_seed_metadata: FFMPEG - duration = %d, bitrate = %d, resolution = %s' % ( duration, bitrate, resolution) if not os.path.exists(abs_thumbdir): os.makedirs(abs_thumbdir) thumb_filenames = [ os.path.join(abs_thumbdir, videoname + postfix) for postfix in ["-thumb%d.jpg" % i for i in range(1, 5)] ] thumb_resolutions = [(1280, 720), (320, 240), (320, 240), (320, 240)] thumb_timecodes = preferred_timecodes(videofile, duration, limit_resolution( resolution, (100, 100)), videoanalyser, k=4) for filename, max_res, timecode in zip(thumb_filenames, thumb_resolutions, thumb_timecodes): thumb_res = limit_resolution(resolution, max_res) get_thumbnail(videofile, filename, thumb_res, videoanalyser, timecode) if DEBUG: path_exists = os.path.exists(filename) print >> sys.stderr, 'create_and_seed_metadata: FFMPEG - thumbnail created = %s, timecode = %d' % ( path_exists, timecode) sdef = SwiftDef() sdef.set_tracker("127.0.0.1:9999") for thumbfile in thumb_filenames: if os.path.exists(thumbfile): xi = os.path.relpath(thumbfile, torcoldir) if sys.platform == "win32": xi = xi.replace("\\", "/") si = xi.encode("UTF-8") sdef.add_content(thumbfile, si) specpn = sdef.finalize(self.session.get_swift_path(), destdir=torcoldir) hex_roothash = sdef.get_roothash_as_hex() try: swift_filename = os.path.join(torcoldir, hex_roothash) shutil.move(specpn, swift_filename) shutil.move(specpn + '.mhash', swift_filename + '.mhash') shutil.move(specpn + '.mbinmap', swift_filename + '.mbinmap') except: if DEBUG: print_exc() modifications = {'swift-thumbnails': json.dumps((thumb_timecodes, sdef.get_roothash_as_hex())), \ 'video-info': json.dumps(video_info)} if DEBUG: print >> sys.stderr, 'create_and_seed_metadata: modifications =', modifications self.channelsearch_manager.modifyTorrent(torrent.channel.id, torrent.channeltorrent_id, modifications)
def _create_and_seed_metadata(self, videofile, torrent): from Tribler.Main.vwxGUI.GuiUtility import GUIUtility if prctlimported: prctl.set_name("Tribler"+currentThread().getName()) self.guiutility = GUIUtility.getInstance() self.session = self.guiutility.utility.session videoanalyser = self.session.get_video_analyser_path() torcoldir = self.session.get_torrent_collecting_dir() rel_thumbdir = 'thumbs-'+binascii.hexlify(torrent.infohash) abs_thumbdir = os.path.join(torcoldir, rel_thumbdir) videoname = os.path.basename(videofile) if os.path.exists(abs_thumbdir): if DEBUG: print >> sys.stderr, 'create_and_seed_metadata: already downloaded thumbnails for torrent', torrent.name return if DEBUG: print >> sys.stderr, 'create_and_seed_metadata: going to seed metadata for torrent', torrent.name duration, bitrate, resolution = get_videoinfo(videofile, videoanalyser) video_info = {'duration': duration, \ 'bitrate': bitrate, \ 'resolution': resolution} if DEBUG: print >> sys.stderr, 'create_and_seed_metadata: FFMPEG - duration = %d, bitrate = %d, resolution = %s' % (duration, bitrate, resolution) if not os.path.exists(abs_thumbdir): os.makedirs(abs_thumbdir) thumb_filenames = [os.path.join(abs_thumbdir, videoname + postfix) for postfix in ["-thumb%d.jpg" % i for i in range(1,5)]] thumb_resolutions = [(1280, 720), (320, 240), (320, 240), (320, 240)] thumb_timecodes = preferred_timecodes(videofile, duration, limit_resolution(resolution, (100, 100)), videoanalyser, k = 4) for filename, max_res, timecode in zip(thumb_filenames, thumb_resolutions, thumb_timecodes): thumb_res = limit_resolution(resolution, max_res) get_thumbnail(videofile, filename, thumb_res, videoanalyser, timecode) if DEBUG: path_exists = os.path.exists(filename) print >> sys.stderr, 'create_and_seed_metadata: FFMPEG - thumbnail created = %s, timecode = %d' % (path_exists, timecode) sdef = SwiftDef() sdef.set_tracker("127.0.0.1:9999") for thumbfile in thumb_filenames: if os.path.exists(thumbfile): xi = os.path.relpath(thumbfile, torcoldir) if sys.platform == "win32": xi = xi.replace("\\","/") si = xi.encode("UTF-8") sdef.add_content(thumbfile, si) specpn = sdef.finalize(self.session.get_swift_path(), destdir = torcoldir) hex_roothash = sdef.get_roothash_as_hex() try: swift_filename = os.path.join(torcoldir, hex_roothash) shutil.move(specpn, swift_filename) shutil.move(specpn+'.mhash', swift_filename+'.mhash') shutil.move(specpn+'.mbinmap', swift_filename+'.mbinmap') except: if DEBUG: print_exc() modifications = {'swift-thumbnails': json.dumps((thumb_timecodes, sdef.get_roothash_as_hex())), \ 'video-info': json.dumps(video_info)} if DEBUG: print >> sys.stderr, 'create_and_seed_metadata: modifications =', modifications self.channelsearch_manager.modifyTorrent(torrent.channel.id, torrent.channeltorrent_id, modifications)