Logo Search packages:      
Sourcecode: hellanzb version File versions  Download package

def Hellanzb::NZBQueue::scanQueueDir (   firstRun = False,
  justScan = False 
)

Find new/resume old NZB download sessions 

Definition at line 125 of file NZBQueue.py.

00125                                                     :
    """ Find new/resume old NZB download sessions """
    #t = time.time()

    from Hellanzb.NZBLeecher.NZBModel import NZB
    currentNZBs = []
    for file in os.listdir(Hellanzb.CURRENT_DIR):
        if Hellanzb.NZB_FILE_RE.search(file):
            currentNZBs.append(os.path.join(Hellanzb.CURRENT_DIR, file))

    # See if we're resuming a nzb fetch
    resuming = False
    displayNotification = False
    newNZBs = []
    queuedMap = {}
    for nzb in Hellanzb.nzbQueue:
        queuedMap[os.path.normpath(nzb.nzbFileName)] = nzb

    for file in os.listdir(Hellanzb.QUEUE_DIR):
        if Hellanzb.NZB_FILE_RE.search(file):
            if os.path.normpath(os.path.join(Hellanzb.QUEUE_DIR, file)) not in queuedMap:
                # Delay enqueueing recently modified NZBs
                if not isOldEnough(os.path.join(Hellanzb.QUEUE_DIR, file)):
                    continue
                
                newNZBs.append(os.path.join(Hellanzb.QUEUE_DIR, file))

            elif os.path.normpath(os.path.join(Hellanzb.QUEUE_DIR, file)) in queuedMap:
                queuedMap.pop(os.path.normpath(os.path.join(Hellanzb.QUEUE_DIR, file)))

        elif file.lower().endswith('.zip'):
            filepath = os.path.join(Hellanzb.QUEUE_DIR, file)
            # Delay unzipping recently modified zips
            if not isOldEnough(filepath):
                continue
            
            try:
                z = zipfile.ZipFile(filepath)
            except zipfile.BadZipFile, bzf:
                error('Error reading ZipFile: "%s"' % file, bzf)
                continue

            info('Extracting NZBs from %s...' % (file))
            
            zipfiles = z.namelist()
            nzbcount = 0
            for zipname in zipfiles:
                if Hellanzb.NZB_FILE_RE.search(zipname):
                    zippath = os.path.join(Hellanzb.QUEUE_DIR, zipname)
                    if os.path.exists(zippath):
                        debug('Not extracting "%s" from "%s": file already exists.' % \
                              (zipname, file))
                        continue
                    newNZB = open(zippath, 'w')
                    newNZB.write(z.read(zipname))
                    newNZB.close()
                    enqueueNZBs(zippath)
                    nzbcount += 1
            
            if nzbcount > 0:
                os.remove(filepath)
            
            z.close()

    # Remove anything no longer in the queue directory
    for nzb in queuedMap.itervalues():
        Hellanzb.nzbQueue.remove(nzb)

    if firstRun:
        # enqueueNZBs() will delete the recovered state. Save it beforehand for sorting
        queuedRecoveredState = Hellanzb.recoveredState.queued.copy()

    enqueueNZBs(newNZBs, writeQueue = not firstRun)
            
    if firstRun:
        sortQueueFromRecoveredState(queuedRecoveredState)

    #e = time.time() - t
    if justScan:
        # Done scanning -- don't bother loading a new NZB
        #debug('Ziplick scanQueueDir (justScan): ' + Hellanzb.QUEUE_DIR + ' TOOK: ' + str(e))
        #debug('Ziplick scanQueueDir (justScan): ' + Hellanzb.QUEUE_DIR)
        Hellanzb.downloadScannerID = reactor.callLater(7, scanQueueDir, False, True)
        return
    #else:
    #    debug('Ziplick scanQueueDir: ' + Hellanzb.QUEUE_DIR)

    if not currentNZBs:
        if not Hellanzb.nzbQueue:
            if firstRun:
                writeStateXML()

            # Nothing to do, lets wait 5 seconds and start over
            Hellanzb.downloadScannerID = reactor.callLater(5, scanQueueDir)

            if not firstRun and not justScan and not Hellanzb.loggedIdleMessage:
                growlNotify('Queue', 'hellanzb', 'No more nzbs left to download', False)
                Hellanzb.loggedIdleMessage = True
            return

        # Start the next download
        nzb = Hellanzb.nzbQueue[0]
        nzbfilename = os.path.basename(nzb.nzbFileName)
        del Hellanzb.nzbQueue[0]
    
        # nzbfile will always be a absolute filename 
        nzbfile = os.path.join(Hellanzb.QUEUE_DIR, nzbfilename)
        move(nzbfile, Hellanzb.CURRENT_DIR)

        if not (len(newNZBs) == 1 and len(Hellanzb.nzbQueue) == 0):
            # Show what's going to be downloaded next, unless the queue was empty, and we
            # only found one nzb (The 'Found new nzb' message is enough in that case)
            displayNotification = True
    else:
        # Resume the NZB in the CURRENT_DIR
        nzbfilename = currentNZBs[0]
        nzb = NZB.fromStateXML('downloading', nzbfilename)
        
        nzbfilename = os.path.basename(nzb.nzbFileName)
        displayNotification = True
        del currentNZBs[0]
        resuming = True

    nzbfile = os.path.join(Hellanzb.CURRENT_DIR, nzbfilename)
    nzb.nzbFileName = nzbfile

    if firstRun:
        writeStateXML()

    if resuming:
        if nzb.isParRecovery:
            msg = 'Resuming par recovery download'
        else:
            msg = 'Resuming'
        parseNZB(nzb, msg)
    elif displayNotification:
        parseNZB(nzb)
    else:
        parseNZB(nzb, quiet = True)

def sortQueueFromRecoveredState(queuedRecoveredState):


Generated by  Doxygen 1.6.0   Back to index