⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addtorrents.py

📁 ABC-win32-v3.1 一个P2P软源代码
💻 PY
📖 第 1 页 / 共 2 页
字号:
                    dialog.Destroy()
                    if(result == wx.ID_NO):
                        return "Error=This torrent is duplicate"
                    else:
                        dotTorrentDuplicate = True
            else:
                return "Error=This torrent is duplicate"
        else:
            # Either:
            # dotTorrentDuplicate was False and the file didn't exist (no change)
            # dotTorrentDuplicate was True before when coming from AddTorrentURL (still need to check the list)
            dotTorrentDuplicate = False

        # No need to copy if we're just copying the file onto itself
        if (filepath != torrentsrc):
            copy2(filepath, torrentsrc)
        success, mesg, torrent = self.addNewProc(torrentsrc, 
                                                 dest = dest,
                                                 forceasklocation = forceasklocation, 
                                                 dotTorrentDuplicate = dotTorrentDuplicate, 
                                                 dontremove = dontremove, 
                                                 caller = caller)
        if success:
            return "OK"
        else:
            return "Error=" + mesg

    #
    # Add a torrent to the list
    # Torrents can be added from 3 sources:
    #   from file
    #   from URL
    #   autoadd (command line)
    #
    def addNewProc(self, src, dest = None, forceasklocation = False, dotTorrentDuplicate = False, dontremove = False, caller = "", doupdate = True):
        #from file, URL maybe down torrent.lst from addProc
        # change at onChooseFile make sure they choose dest
        # dotTorrentDuplicate : To avoid asking the user twice about duplicate (for torrent file name and torrent name)
        #                       True if .torrent is duplicate ; not used if caller==web"

        # Did we succeed in adding the torrent?
        error = None
        ABCTorrentTemp = None
        
        # Check to see the the src file actually exists:
        if not os.access(src, os.R_OK):
            if caller != "web":
                dlg = wx.MessageDialog(None, 
                                       src + '\n' + self.utility.lang.get('failedtorrentmissing'), 
                                       self.utility.lang.get('error'), 
                                       wx.OK|wx.ICON_ERROR)
                result = dlg.ShowModal()
                dlg.Destroy()
                dontremove = True
            error = ".torrent file doesn't exist or can't be read"
        else:
            ABCTorrentTemp = ABCTorrent(self.queue, src, dest = dest, forceasklocation = forceasklocation, caller = caller)       
            
            if ABCTorrentTemp.metainfo is None:
                if caller != "web":
                    dlg = wx.MessageDialog(None, 
                                           src + '\n' + \
                                           self.utility.lang.get('failedinvalidtorrent') + '\n' + \
                                           self.utility.lang.get('removetorrent'), 
                                           self.utility.lang.get('error'), 
                                           wx.YES_NO|wx.ICON_ERROR)
                    result = dlg.ShowModal()
                    dlg.Destroy()
                    if (result == wx.ID_NO):
                        dontremove = True
                error = "Invalid torrent file"
                    
            # If the torrent doesn't have anywhere to save to, return with an error
            elif ABCTorrentTemp.files.dest is None:
                error = "No destination to save to"
    
            # Search for duplicate torrent name (inside .torrent file) and hash info
            # only if the .torrent is not already a duplicate
            elif not dotTorrentDuplicate:
                torrentInList = self.checkForDuplicateInList(ABCTorrentTemp.infohash, ABCTorrentTemp.src)
                if torrentInList:
                    self.dupFileInList(src, caller)
#                    if caller != "web":
#                        message = src + '\n\n' + self.utility.lang.get('duplicatetorrentinlist')
#                        dlg = wx.MessageDialog(None, 
#                                               message, 
#                                               self.utility.lang.get('duplicatetorrent'), 
#                                               wx.OK|wx.ICON_ERROR)
#                        dlg.ShowModal()
#                        dlg.Destroy()
                    error = "Duplicate torrent"

        # We encountered an error somewhere in the process
        if error is not None:
            # Don't remove if the torrent file is already being used by an existing process
            # Removing will cause problems with the other process
            if not dontremove:
                try:
                    os.remove(src)
                except:
                    pass
            ABCTorrentTemp = None
            return False, error, ABCTorrentTemp
       
        if doupdate and ABCTorrentTemp is not None:
            ABCTorrentTemp.postInitTasks()
            
            # Update torrent.list
            ABCTorrentTemp.torrentconfig.writeSrc(False)
            self.utility.torrentconfig.Flush()
            
            self.queue.updateAndInvoke()
        
        return True, self.utility.lang.get('ok'), ABCTorrentTemp
        
    #
    # Add a torrent that's already been loaded into the list
    #
    def addOldProc(self, src):       
        # Torrent information
        filename = os.path.join(self.utility.getConfigPath(), "torrent", src)
        
        success, error, ABCTorrentTemp = self.addNewProc(filename, dest = None, doupdate = False)
        
        if not success:
            # Didn't get a valid ABCTorrent object
            return False
        
        ABCTorrentTemp.postInitTasks()
        
        return True
             
    #
    # Load torrents from the torrent.list file
    #
    def readTorrentList(self):
        # Convert list in older format if necessary
        convertOldList(self.utility)
        
        numbackups = 3
        
        # Manage backups
        filenames = [ os.path.join(self.utility.getConfigPath(), "torrent.list") ]
        for i in range(1, numbackups + 1):
            filenames.append(filenames[0] + ".backup" + str(i))
            
        for i in range (numbackups, 0, -1):
            if os.access(filenames[i-1], os.R_OK):
                copy2(filenames[i-1], filenames[i])
        
        oldprocs = []
        for index, src in self.utility.torrentconfig.Items():
            try:
                index = int(index)
                oldprocs.append((index, src))
            except:
                pass
        oldprocs.sort()
        
        for index, src in oldprocs:
            self.addOldProc(src)
            
        self.queue.updateAndInvoke()

    #
    # Compare two torrents to see if they are the same
    #
    def isSameTorrent(self, torrent1src, torrent2src):
        # Same location
        if torrent1src == torrent2src:
            return True

        metainfo1 = self.utility.getMetainfo(torrent1src)
        if metainfo1 is None:
            return False
        
        metainfo2 = self.utility.getMetainfo(torrent2src)
        if metainfo2 is None:
            return False
        
        metainfo_hash1 = sha(bencode(metainfo1)).hexdigest()
        metainfo_hash2 = sha(bencode(metainfo2)).hexdigest()
        
        # Hash values for both torrents are the same
        if metainfo_hash1 == metainfo_hash2:
            return True
            
        return False

    #
    # Duplicate file error
    #
    def dupFileInList(self, src, caller = ""):
        if caller != "web":
            message = src + '\n\n' + self.utility.lang.get('duplicatetorrentinlist')
            dlg = wx.MessageDialog(None, 
                                   message, 
                                   self.utility.lang.get('duplicatetorrent'), 
                                   wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            dlg.Destroy()

    #
    # See if a torrent already in the list has the same infohash
    #
    def checkForDuplicateInList(self, infohash = None, src = None):
        for torrent in self.utility.torrents["all"]:
            if (src is not None and src == torrent.src) or \
               (infohash is not None and infohash == torrent.infohash):
                return True
        return False

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -