示例#1
0
def DCC_deamonthread(c, server, nickname, hist, down, size=0):
    """
    If a server connection fails, there is no point in checking the queue for further downloads;
    There, we try to reconnect as soon as the connection timed out.
    To be able to interrupt the connection attempt loop, we use the signal listener and pass the server connection
    """
    botqueue = main.queuedict[server+c.bot] = Queue()
    i=1    
    while True:
        try:
            c.connect(server, 6667, nickname)
            c.start()
            break
        except irc.client.ServerConnectionError as x:
            log("error (%s)" % i + str(x)).write()
            hist.status, hist.time, hist.sizeraw = "Error during connection (%d)" % i, utcnow(), size
            hist.save()
            down.status = "Connecting to server... (%d)" % (i+1)
            down.save()
            i+=1
            if not botqueue.empty():
                mystring = botqueue.get()
                if "cancel" in mystring:
                    views.manage_queue(down)
                    break
示例#2
0
    def process(self, c, e, resume=False):
        #"When receiving a file with DCC, accept it"
        downloads_dir = directory()
        bot = e.source.nick
        try:
            connection = self.dcc_connect(self.ip, self.port, "raw")
        except irc.client.DCCConnectionError as e:
            log('Couldn\'t connect to DCC peer: %s' % e).write()
            self.dict[bot]["hist"].status, self.dict[bot]["hist"].time = "Peer connection error", utcnow()
            self.dict[bot]["hist"].save()            
            views.manage_queue(self.dict[bot]["down"])
            return ""
        if resume:
            self.dict[bot]["file"] = open(os.path.join(downloads_dir, self.dict[bot]["filename"]), 'ab')
            self.dict[bot]["down"].filename, self.dict[bot]["down"].status = (
                self.dict[bot]["filename"], "Resuming download...")
            log('Resuming download of %s' % self.dict[bot]["filename"]).write()
        else:
            self.dict[bot]["file"] = open(os.path.join(downloads_dir, self.dict[bot]["filename"]), 'wb')
            self.dict[bot]["down"].filename, self.dict[bot]["down"].status, self.dict[bot]["down"].sizeraw = (
                self.dict[bot]["filename"], "Starting download...", self.dict[bot]["size"])            
            log('Starting download of %s' % self.dict[bot]["filename"]).write()

        self.DCCconnections_dict[bot] = connection
        self.dict[bot]["hist"].time = utcnow()
        self.dict[bot]["down"].save()
        t = threading.Timer(0, monitoring, [self, connection, bot, int(self.dict[bot]["position"])])
        t.start()
        s=threading.Timer(0, self.retry_pack, [connection, bot])
        s.daemon=True
        s.start()
示例#3
0
 def on_privnotice(self, c, e):
     bot = e.source.nick
     a = e.arguments[0].split(":", 1)
     log(a).write()
     if "invalid pack number" in str(a).lower():
         self.DCCconnections_dict[bot].disconnect()
         self.dict[bot]["hist"].status, self.dict[bot]["hist"].time = "Dead link from the search engine", utcnow()
         self.dict[bot]["down"].status, self.dict[bot]["down"].active = "Invalid pack number", False
         self.dict[bot]["down"].save()
         self.dict[bot]["hist"].save()            
         log("Discarding download due to invalid pack number, proceeding to next item in queue if existing.").write()
         print ("3")
         views.manage_queue(self.dict[bot]["down"])
示例#4
0
 def on_dcc_disconnect(self, connection, event, bot=""):
     for key, value in self.DCCconnections_dict.items():
         if value == connection:
             bot = key
             break
     try:
         self.dict[bot]["file"].close()
     except:
         bot = bot
     if self.dict[bot]["stop"]:
         return ""        
     if self.dict[bot]["cancel"] == False:
         (self.dict[bot]["down"].status, self.dict[bot]["down"].progress, self.dict[bot]["down"].speed, 
             self.dict[bot]["down"].progress, self.dict[bot]["down"].eta) = "Extracting...", None, None, None, None
         self.dict[bot]["down"].save()
         tot = self.dict[bot]["received_bytes"]+self.dict[bot]["position"]
         log("Received file (%d bytes)." % tot  ).write()
         log("Size (%d bytes)." % int(self.dict[bot]["size"]) ).write()
         percdone = tot/int(self.dict[bot]["size"])
         duration = (utcnow() - self.dict[bot]["hist"].time)
         average = int(self.dict[bot]["size"])/1024/duration.total_seconds()
         (self.dict[bot]["hist"].filename, self.dict[bot]["hist"].status, self.dict[bot]["hist"].average, 
             self.dict[bot]["hist"].duration, self.dict[bot]["hist"].time, self.dict[bot]["hist"].sizeraw) = (self.dict[bot]["filename"],
             "Downloaded", round(average, 0), duration, utcnow(), self.dict[bot]["size"])
         self.dict[bot]["hist"].save()
         if os.path.exists(os.path.join(directory(), self.dict[bot]["filename"])):
             #added to prevent extracting incomplete files (internet connection interrupted)
             if percdone > 99/100:
                 untar(os.path.join(directory(), self.dict[bot]["filename"]), 
                     self.dict[bot]["filename"], self.dict[bot]["down"], self.dict[bot]["hist"])
                 return ""
         
         self.dict[bot]["down"].status = "Error during file transfer"
         self.dict[bot]["down"].save()
         self.dict[bot]["hist"].status, self.dict[bot]["hist"].time = "Error during file transfer", utcnow()
         self.dict[bot]["hist"].save()
         log("Error during file transfer. Completed percentage: %d" % int(percdone*100) ).write()
         print ("2")
         views.manage_queue(self.dict[bot]["down"])
         return ""
     else:
         self.dict[bot]["cancel"] = False
         views.manage_queue(self.dict[bot]["down"])
         return ""
示例#5
0
def untar(completefile, filename, down, hist):
    log("Extracting the file...").write()
    log(completefile + ' ' + filename).write()
    # tar file to extract
    theTarFile = completefile

    # tar file path to extract
    extractTarPath = os.path.join(directory(), '_UNPACK_' + splitext(filename)[0])
    if tarfile.is_tarfile(theTarFile):
        # extract all contents
        try:
            tfile = tarfile.open(theTarFile)
            tfile.extractall(extractTarPath)
            tfile.close()
            os.remove(theTarFile)
            #extract rar files
            x=0
            completefile = False
            #this test is added in case some txt is extracted with the folder
            while not os.path.isdir(os.path.join(extractTarPath, os.listdir(extractTarPath)[x])):
                #sometimes the file extracted is the final file, no futher rar extraction needed; in this case, test for it's size and finish
                if os.path.getsize(os.path.join(extractTarPath, os.listdir(extractTarPath)[x])) > 90/100*hist.sizeraw:
                    completefile = True
                    break
                x+=1
            if not completefile:
                origin = os.path.join(extractTarPath, os.listdir(extractTarPath)[x])
                log(origin).write()
                for fl in os.listdir(origin):
                    shutil.move(os.path.join(origin, fl), extractTarPath)
                shutil.rmtree(origin)
                for archivefile in os.listdir(extractTarPath):
                    if ".rar" in archivefile:
                        arch_ref = rarfile.RarFile(os.path.join(extractTarPath, archivefile),'r')
                        arch_ref.extractall(extractTarPath)
                #remove rest
                for fl in os.listdir(extractTarPath):
                    ext = splitext(fl)[1].lower()
                    pathToDelete = os.path.join(extractTarPath, fl)
                    if os.path.isdir(pathToDelete):
                    # Do not delete non-empty dirs, could contain useful files.
                        try:
                            os.rmdir(pathToDelete);
                        except OSError as ex:
                            if ex.errno == errno.ENOTEMPTY:
                                log("Did not delete non-empty directory : %s" %  pathToDelete).write()
                            else:
                                log("An error occurred deleting directory : %s" % pathToDelete).write()
                    else:
                        if ext[:2] == '.r' or ext in ['.sfv', '.nfo', '.png', '.jpg'] or 'sample' in fl:
                            os.remove(os.path.join(extractTarPath, fl))
            #remove UNPACK from name when done
            os.rename(extractTarPath, os.path.join(directory(), splitext(filename)[0]))
        except Exception as e:
            log("An error occured during file extraction : %s" % e).write()
            hist.status, hist.time = "Error during file extraction", utcnow()
            down.status, down.sizeraw, down.active = "Error during file extraction", None, False
            down.save()
            hist.save()
            print ("4")
            views.manage_queue(down)
            return ""

    else:
        log(theTarFile + " is not a tarfile.").write()


    down.status, down.sizeraw, down.active = "Downloaded and extracted properly", None, False
    hist.status, hist.time = "Downloaded and extracted properly", utcnow()
    hist.save()
    down.save()
    print ("5")
    views.manage_queue(down)
    return ""