def AddTorrentSkipHashCheck(self, logger, torrentPath, downloadPath): logger.info( "Adding torrent '%s' without hash checking to rTorrent to '%s'." % (torrentPath, downloadPath)) sourceDirectory, sourceFilename = os.path.split(torrentPath) sourceFilename = "fast resume " + sourceFilename destinationTorrentPath = os.path.join(sourceDirectory, sourceFilename) if os.path.exists(destinationTorrentPath): raise PtpUploaderException( "Can't create fast resume torrent because path '%s' already exists." % destinationTorrentPath) shutil.copyfile(torrentPath, destinationTorrentPath) metainfo = bencode.bread(destinationTorrentPath) metafile.add_fast_resume(metainfo, downloadPath.encode('utf-8')) bencode.bwrite(destinationTorrentPath, metainfo) infoHash = "" try: infoHash = self.AddTorrent(logger, destinationTorrentPath, downloadPath) finally: # We always remove the fast resume torrent regardless of result of adding the torrent to rTorrent. # This ensures that even if adding to rTorent fails, then resuming the job will work. os.remove(destinationTorrentPath) return infoHash
def AddTorrent(self, logger, torrentPath, downloadPath): logger.info( "Initiating the download of torrent '%s' with rTorrent to '%s'." % (torrentPath, downloadPath)) file = open(torrentPath, "rb") contents = xmlrpclib.Binary(file.read()) file.close() torrentData = bencode.bread(torrentPath) metafile.check_meta(torrentData) infoHash = metafile.info_hash(torrentData) self.proxy.load_raw(contents) # If load_raw is slow then set_directory_base throws an exception (Fault: <Fault -501: 'Could not find info-hash.'>), # so we retry adding the torrent some delay. maximumTries = 15 while True: try: self.proxy.d.set_directory_base(infoHash, downloadPath) self.proxy.d.start(infoHash) break except Exception: if maximumTries > 1: maximumTries -= 1 time.sleep(2) # Two seconds. else: raise return infoHash
def AddTorrentSkipHashCheck(self, logger, torrentPath, downloadPath): logger.info( "Adding torrent '%s' without hash checking to rTorrent to '%s'." % ( torrentPath, downloadPath ) ); sourceDirectory, sourceFilename = os.path.split( torrentPath ); sourceFilename = "fast resume " + sourceFilename; destinationTorrentPath = os.path.join( sourceDirectory, sourceFilename ); if os.path.exists( destinationTorrentPath ): raise PtpUploaderException( "Can't create fast resume torrent because path '%s' already exists." % destinationTorrentPath ) shutil.copyfile( torrentPath, destinationTorrentPath ); metainfo = bencode.bread( destinationTorrentPath ) metafile.add_fast_resume( metainfo, downloadPath.encode( 'utf-8' ) ) bencode.bwrite( destinationTorrentPath, metainfo ) infoHash = "" try: infoHash = self.AddTorrent( logger, destinationTorrentPath, downloadPath ) finally: # We always remove the fast resume torrent regardless of result of adding the torrent to rTorrent. # This ensures that even if adding to rTorent fails, then resuming the job will work. os.remove( destinationTorrentPath ) return infoHash
def AddTorrent(self, logger, torrentPath, downloadPath): logger.info( "Initiating the download of torrent '%s' with rTorrent to '%s'." % ( torrentPath, downloadPath ) ); file = open( torrentPath, "rb" ); contents = xmlrpclib.Binary( file.read() ); file.close(); torrentData = bencode.bread( torrentPath ); metafile.check_meta( torrentData ); infoHash = metafile.info_hash( torrentData ); self.proxy.load_raw( contents ); # If load_raw is slow then set_directory_base throws an exception (Fault: <Fault -501: 'Could not find info-hash.'>), # so we retry adding the torrent some delay. maximumTries = 15 while True: try: self.proxy.d.set_directory_base( infoHash, downloadPath ); self.proxy.d.start( infoHash ); break except Exception: if maximumTries > 1: maximumTries -= 1 time.sleep( 2 ) # Two seconds. else: raise return infoHash;
def GetSuggestedReleaseNameAndSizeFromTorrentFile( torrentPath ): data = bencode.bread( torrentPath ) name = data[ "info" ].get( "name", None ) files = data[ "info" ].get( "files", None ) if files is None: # It is a single file torrent, remove the extension. name, extension = os.path.splitext( name ) size = data[ "info" ][ "length" ] return name, size else: size = 0 for file in files: size += file[ "length" ] return name, size
def GetFileListFromTorrent(torrentPath): data = bencode.bread( torrentPath ) name = data[ "info" ].get( "name", None ) files = data[ "info" ].get( "files", None ) if files is None: return [ name ] else: fileList = [] for fileInfo in files: path = "/".join( fileInfo[ "path" ] ) path = path.decode( "utf-8", "ignore" ) fileList.append( path ) return fileList
def GetSuggestedReleaseNameAndSizeFromTorrentFile(torrentPath): data = bencode.bread(torrentPath) name = data["info"].get("name", None) files = data["info"].get("files", None) if files is None: # It is a single file torrent, remove the extension. name, extension = os.path.splitext(name) size = data["info"]["length"] return name, size else: size = 0 for file in files: size += file["length"] return name, size
def GetFileListFromTorrent(torrentPath): data = bencode.bread(torrentPath) name = data["info"].get("name", None) files = data["info"].get("files", None) if files is None: return [name] else: fileList = [] for fileInfo in files: path = "/".join(fileInfo["path"]) path = path.decode("utf-8", "ignore") fileList.append(path) return fileList
def ValidateTorrentFile(torrentPath): try: torrentData = bencode.bread( torrentPath ) except Exception: raise PtpUploaderException( "File '%s' is not a valid torrent." % torrentPath )
def ValidateTorrentFile(torrentPath): try: torrentData = bencode.bread(torrentPath) except Exception: raise PtpUploaderException("File '%s' is not a valid torrent." % torrentPath)
def CleanTorrentFile(self, logger, torrentPath): logger.info("Cleaning torrent file '%s'." % torrentPath) metainfo = bencode.bread(torrentPath) metafile.clean_meta(metainfo, including_info=False, logger=logger.info) bencode.bwrite(torrentPath, metainfo)
def CleanTorrentFile(self, logger, torrentPath): logger.info( "Cleaning torrent file '%s'." % torrentPath ) metainfo = bencode.bread( torrentPath ) metafile.clean_meta( metainfo, including_info = False, logger = logger.info ) bencode.bwrite( torrentPath, metainfo )