Exemplo n.º 1
0
def searchmagazines(mags=None):
    maglist = []
    myDB = database.DBConnection()
    searchlist = []

    threading.currentThread().name = "SEARCHMAGS"

    if mags is None:
        searchmags = myDB.select(
            'SELECT Title, Frequency, LastAcquired, IssueDate from magazines WHERE Status="Active"'
        )
    else:
        searchmags = []
        for magazine in mags:
            searchmags_temp = myDB.select(
                'SELECT Title, Frequency, LastAcquired, IssueDate from magazines WHERE Title=? AND Status="Active"',
                [magazine['bookid']])
            for terms in searchmags_temp:
                searchmags.append(terms)

    for searchmag in searchmags:
        bookid = searchmag[0]
        searchterm = searchmag[0]
        frequency = searchmag[1]
        last_acquired = searchmag[2]
        issue_date = searchmag[3]

        dic = {
            '...': '',
            ' & ': ' ',
            ' = ': ' ',
            '?': '',
            '$': 's',
            ' + ': ' ',
            '"': '',
            ',': '',
            '*': ''
        }

        searchterm = formatter.latinToAscii(
            formatter.replace_all(searchterm, dic))
        searchterm = re.sub('[\.\-\/]', ' ', searchterm).encode('utf-8')
        searchlist.append({"bookid": bookid, "searchterm": searchterm})

    if not lazylibrarian.SAB_HOST and not lazylibrarian.BLACKHOLE:
        logger.info('No download method is set, use SABnzbd or blackhole')

    if not lazylibrarian.NEWZNAB and not lazylibrarian.NEWZNAB2 and not lazylibrarian.USENETCRAWLER:
        logger.info('No providers are set. try use NEWZNAB.')

    if searchlist == []:
        logger.info(
            'There is nothing to search for.  Mark some magazines as active.')

    for book in searchlist:
        resultlist = []
        if lazylibrarian.NEWZNAB:
            logger.debug('Searching NZB\'s at provider %s ...' %
                         lazylibrarian.NEWZNAB_HOST)
            resultlist = providers.NewzNab(book, "1")

        if lazylibrarian.NEWZNAB2:
            logger.debug('Searching NZB\'s at provider %s ...' %
                         lazylibrarian.NEWZNAB_HOST2)
            resultlist += providers.NewzNab(book, "2")

        if lazylibrarian.USENETCRAWLER:
            logger.info('Searching NZB\'s at provider UsenetCrawler ...')
            resultlist += providers.UsenetCrawler(book, 'mag')

            #AHHH pass the book not the search book - bloody names the same, so wrong keys passing over

        if not resultlist:
            logger.debug("Adding book %s to queue." % book['searchterm'])

        else:
            bad_regex = 0
            old_date = 0
            total_nzbs = 0
            new_date = 0
            for nzb in resultlist:
                total_nzbs = total_nzbs + 1
                bookid = nzb['bookid']
                nzbtitle = nzb['nzbtitle']
                nzburl = nzb['nzburl']
                nzbprov = nzb['nzbprov']
                nzbdate_temp = nzb['nzbdate']
                nzbsize_temp = nzb['nzbsize']
                nzbsize = str(round(float(nzbsize_temp) / 1048576, 2)) + ' MB'
                nzbdate = formatter.nzbdate2format(nzbdate_temp)

                checkifmag = myDB.select(
                    'SELECT * from magazines WHERE Title=?', [bookid])
                if checkifmag:
                    for results in checkifmag:
                        control_date = results['IssueDate']
                        frequency = results['Frequency']
                        regex = results['Regex']

                    nzbtitle_formatted = nzb['nzbtitle'].replace(
                        '.', ' ').replace('/', ' ').replace('+', ' ').replace(
                            '_', ' ').replace('(', '').replace(')', '')
                    #Need to make sure that substrings of magazine titles don't get found (e.g. Maxim USA will find Maximum PC USA)
                    keyword_check = nzbtitle_formatted.replace(bookid, '')
                    #remove extra spaces if they're in a row
                    nzbtitle_exploded_temp = " ".join(
                        nzbtitle_formatted.split())
                    nzbtitle_exploded = nzbtitle_exploded_temp.split(' ')

                    bookid_exploded = bookid.split(' ')

                    #Make sure that NZB contains exact magazine phrase, and that NZB title begins with magazine title
                    #logger.debug('[%s] !=[%s] & [%s] == [%s]' %(keyword_check.lower(),nzbtitle_formatted.lower(),nzbtitle_exploded[0].lower(),bookid_exploded[0].lower()))
                    if keyword_check.lower() != nzbtitle_formatted.lower(
                    ) and nzbtitle_exploded[0].lower(
                    ) == bookid_exploded[0].lower():

                        if len(nzbtitle_exploded) > 1:
                            #regexA = DD MonthName YYYY OR MonthName YYYY
                            regexA_year = nzbtitle_exploded[
                                len(nzbtitle_exploded) - 1]
                            regexA_month_temp = nzbtitle_exploded[
                                len(nzbtitle_exploded) - 2]
                            regexA_month = formatter.month2num(
                                regexA_month_temp)

                            if frequency == "Weekly" or frequency == "BiWeekly":
                                regexA_day = nzbtitle_exploded[
                                    len(nzbtitle_exploded) - 3].zfill(2)
                            else:
                                regexA_day = '01'
                            newdatish_regexA = regexA_year + regexA_month + regexA_day

                            try:
                                int(newdatish_regexA)
                                newdatish = regexA_year + '-' + regexA_month + '-' + regexA_day
                            except:
                                #regexB = MonthName DD YYYY
                                regexB_year = nzbtitle_exploded[
                                    len(nzbtitle_exploded) - 1]
                                regexB_day = nzbtitle_exploded[
                                    len(nzbtitle_exploded) - 2].zfill(2)
                                regexB_month_temp = nzbtitle_exploded[
                                    len(nzbtitle_exploded) - 3]
                                regexB_month = formatter.month2num(
                                    regexB_month_temp)
                                newdatish_regexB = regexB_year + regexB_month + regexB_day

                                try:
                                    int(newdatish_regexB)
                                    newdatish = regexB_year + '-' + regexB_month + '-' + regexB_day
                                except:
                                    #regexC = YYYY-MM
                                    regexC_last = nzbtitle_exploded[
                                        len(nzbtitle_exploded) - 1]
                                    regexC_exploded = regexC_last.split('-')
                                    if len(regexC_exploded) == 2:
                                        regexC_year = regexC_exploded[0]
                                        regexC_month = regexC_exploded[
                                            1].zfill(2)
                                        regexC_day = '01'
                                        newdatish_regexC = regexC_year + regexC_month + regexC_day
                                    elif len(regexC_exploded) == 3:
                                        regexC_year = regexC_exploded[0]
                                        regexC_month = regexC_exploded[
                                            1].zfill(2)
                                        regexC_day = regexC_exploded[2].zfill(
                                            2)
                                        newdatish_regexC = regexC_year + regexC_month + regexC_day
                                    else:
                                        newdatish_regexC = 'Invalid'

                                    try:
                                        int(newdatish_regexC)
                                        newdatish = regexC_year + '-' + regexC_month + '-' + regexC_day
                                    except:
                                        logger.debug(
                                            'NZB %s not in proper date format.'
                                            % nzbtitle_formatted)
                                        bad_regex = bad_regex + 1
                                        continue

                        else:
                            continue

                        #Don't want to overwrite status = Skipped for NZBs that have been previously found
                        wanted_status = myDB.select(
                            'SELECT * from wanted WHERE NZBtitle=?',
                            [nzbtitle])
                        if wanted_status:
                            for results in wanted_status:
                                status = results['Status']
                        else:
                            status = "Skipped"

                        controlValueDict = {"NZBurl": nzburl}
                        newValueDict = {
                            "NZBprov": nzbprov,
                            "BookID": bookid,
                            "NZBdate": nzbdate,
                            "NZBtitle": nzbtitle,
                            "AuxInfo": newdatish,
                            "Status": status,
                            "NZBsize": nzbsize
                        }
                        myDB.upsert("wanted", newValueDict, controlValueDict)
                        #print nzbtitle_formatted
                        #print newdatish

                        if control_date is None:
                            myDB.upsert("magazines", {
                                "LastAcquired": nzbdate,
                                "IssueDate": newdatish
                            }, {"Title": bookid})
                            maglist.append({
                                'bookid': bookid,
                                'nzbprov': nzbprov,
                                'nzbtitle': nzbtitle,
                                'nzburl': nzburl
                            })
                            new_date = new_date + 1
                        else:
                            comp_date = formatter.datecompare(
                                newdatish, control_date)
                            if comp_date > 0:
                                myDB.upsert(
                                    "magazines", {
                                        "LastAcquired": nzbdate,
                                        "IssueDate": newdatish
                                    }, {"Title": bookid})
                                maglist.append({
                                    'bookid': bookid,
                                    'nzbprov': nzbprov,
                                    'nzbtitle': nzbtitle,
                                    'nzburl': nzburl
                                })
                                new_date = new_date + 1
                            else:
                                logger.debug(
                                    'This issue of %s is old; skipping.' %
                                    nzbtitle_formatted)
                                old_date = old_date + 1
                    else:
                        logger.debug(
                            'NZB [%s] does not completely match search term [%s].'
                            % (nzbtitle, bookid))
                        bad_regex = bad_regex + 1

            logger.info(
                'Found %s NZBs for %s.  %s are new, %s are old, and %s have bad date formatting'
                % (total_nzbs, bookid, new_date, old_date, bad_regex))
    return maglist
Exemplo n.º 2
0
def searchbook(books=None):

    # rename this thread
    threading.currentThread().name = "SEARCHBOOKS"
    myDB = database.DBConnection()
    searchlist = []
    searchlist1 = []

    if books is None:
        searchbooks = myDB.select('SELECT BookID, AuthorName, Bookname from books WHERE Status="Wanted"')
    else:
        searchbooks = []
        for book in books:
            searchbook = myDB.select('SELECT BookID, AuthorName, BookName from books WHERE BookID=? AND Status="Wanted"', [book['bookid']])
            for terms in searchbook:
                searchbooks.append(terms)

    for searchbook in searchbooks:
        bookid = searchbook[0]
        author = searchbook[1]
        book = searchbook[2]

        dic = {'...':'', ' & ':' ', ' = ': ' ', '?':'', '$':'s', ' + ':' ', '"':'', ',':'', '*':'', ':':''}
        dicSearchFormatting = {' ':' +', '.':' +', ' + ':' '}
        dicSearchFormatting1 = {' + ':' '}

        author = formatter.latinToAscii(formatter.replace_all(author, dic))
        book = formatter.latinToAscii(formatter.replace_all(book, dic))
		
		#OLD SEARCH TERM
        searchterm = author + ' ' + book + ' ' + lazylibrarian.EBOOK_TYPE
        searchterm = re.sub('[\.\-\/]', ' ', searchterm).encode('utf-8')
        searchterm = re.sub(r"\s\s+" , " ", searchterm) # strip any double white space
        searchlist.append({"bookid": bookid, "bookName":searchbook[2], "authorName":searchbook[1], "searchterm": searchterm.strip()})

        # TRY A SECCOND SEARCH TERM just using author name and book type
        author = formatter.latinToAscii(formatter.replace_all(author, dicSearchFormatting))
        author = formatter.latinToAscii(formatter.replace_all(author, dicSearchFormatting1))
        searchterm1 = '+' + author + ' +' + lazylibrarian.EBOOK_TYPE 
        searchterm1 = re.sub('[\.\-\/]', ' ', searchterm1).encode('utf-8')
        searchterm1 = re.sub(r'\(.*?\)', '', searchterm1).encode('utf-8')
        searchterm1 = re.sub(r"\s\s+" , " ", searchterm1) # strip any double white space
        searchlist.append({"bookid": bookid, "bookName":searchbook[2], "authorName":searchbook[1], "searchterm": searchterm1.strip()})

    if not lazylibrarian.SAB_HOST and not lazylibrarian.BLACKHOLE:
        logger.info('No download method is set, use SABnzbd or blackhole')

    if not lazylibrarian.NEWZNAB and not lazylibrarian.NZBMATRIX:
        logger.info('No providers are set. use NEWZNAB or NZBMATRIX')

    counter = 0
    for book in searchlist:
        resultlist = []
        if lazylibrarian.NEWZNAB and not resultlist:
            logger.debug('Searching NZB\'s at provider %s ...' % lazylibrarian.NEWZNAB_HOST)
            resultlist = providers.NewzNab(book)

        if lazylibrarian.NZBMATRIX and not resultlist:
            logger.debug('Searching NZB at provider NZBMatrix ...')
            resultlist = providers.NZBMatrix(book)

        if not resultlist:
            logger.debug("Adding book %s to queue." % book['searchterm'])

        else:
            dictrepl = {'...':'', ' & ':' ', ' = ': ' ', '?':'', '$':'s', ' + ':' ', '"':'', ',':'', '*':'', '(':'', ')':'', '[':'', ']':'', '#':'', '0':'', '1':'', '2':'', '3':'', '4':'', '5':'', '6':'', '7':'', '8':'' , '9':'', '\'':'', ':':'', '\s\s':' ' }
            bookName = book['bookName']
            bookName = re.sub('[\.\-\/]', ' ', bookName)
            bookName = re.sub(r'\(.*?\)', '', bookName)
            bookName = formatter.latinToAscii(formatter.replace_all(bookName, dictrepl)).strip()
            logger.debug(u'bookName %s' % bookName)
            addedCounter = 0

            for nzb in resultlist:
				nzbTitle = formatter.latinToAscii(formatter.replace_all(nzb['nzbtitle'], dictrepl)).strip()
				logger.debug(u'nzbName %s' % nzbTitle)
				nameFound = 0
				
				bookNameList = bookName.split()

				for word in bookNameList:
					if nzbTitle.lower().find(word.lower()) == -1:
						nameFound = -1
						
				if nameFound == 0:
					logger.debug(u'FOUND %s' % nzbTitle.lower())
					addedCounter = addedCounter + 1
					bookid = nzb['bookid']
					nzbTitle = (book["authorName"] + ' ' + bookName).strip()
					nzburl = nzb['nzburl']
					nzbprov = nzb['nzbprov']

					controlValueDict = {"NZBurl": nzburl}
					newValueDict = {
                        "NZBprov": nzbprov,
                        "BookID": bookid,
                        "NZBdate": formatter.today(),
                        "NZBtitle": nzbTitle,
                        "Status": "Skipped"
					}
					myDB.upsert("wanted", newValueDict, controlValueDict)

					snatchedbooks = myDB.action('SELECT * from books WHERE BookID=? and Status="Snatched"', [bookid]).fetchone()
					if not snatchedbooks:
						snatch = DownloadMethod(bookid, nzbprov, nzbTitle, nzburl)
					time.sleep(1)
            if addedCounter == 0:
            	logger.info("No nzb's found for " + (book["authorName"] + ' ' + bookName).strip() + ". Adding book to queue.")
        counter = counter + 1
Exemplo n.º 3
0
def searchbook(books=None):

    # rename this thread
    threading.currentThread().name = "SEARCHBOOKS"
    myDB = database.DBConnection()
    searchlist = []

    if books is None:
        searchbooks = myDB.select(
            'SELECT BookID, AuthorName, Bookname from books WHERE Status="Wanted"'
        )
    else:
        searchbooks = []
        for book in books:
            searchbook = myDB.select(
                'SELECT BookID, AuthorName, BookName from books WHERE BookID=? AND Status="Wanted"',
                [book['bookid']])
            for terms in searchbook:
                searchbooks.append(terms)

    for searchbook in searchbooks:
        bookid = searchbook[0]
        author = searchbook[1]
        book = searchbook[2]

        dic = {
            '...': '',
            ' & ': ' ',
            ' = ': ' ',
            '?': '',
            '$': 's',
            ' + ': ' ',
            '"': '',
            ',': '',
            '*': ''
        }

        author = formatter.latinToAscii(formatter.replace_all(author, dic))
        book = formatter.latinToAscii(formatter.replace_all(book, dic))

        searchterm = author + ' ' + book
        searchterm = re.sub('[\.\-\/]', ' ', searchterm).encode('utf-8')
        searchlist.append({"bookid": bookid, "searchterm": searchterm})

    if not lazylibrarian.SAB_HOST and not lazylibrarian.BLACKHOLE:
        logger.info('No downloadmethod is set, use SABnzbd or blackhole')

    if not lazylibrarian.NEWZNAB:
        logger.info('No providers are set.')

    for book in searchlist:
        resultlist = []
        if lazylibrarian.NEWZNAB and not resultlist:
            logger.info('Searching NZB\'s at provider %s ...' %
                        lazylibrarian.NEWZNAB_HOST)
            resultlist = providers.NewzNab(book)

        if lazylibrarian.NZBMATRIX and not resultlist:
            logger.info('Searching NZB at provider NZBMatrix ...')
            resultlist = providers.NZBMatrix(book)

        if not resultlist:
            logger.info(
                "Search didn't have results. Adding book %s to queue." %
                book['searchterm'])

        else:
            for nzb in resultlist:
                bookid = nzb['bookid']
                nzbtitle = nzb['nzbtitle']
                nzburl = nzb['nzburl']
                nzbprov = nzb['nzbprov']

                controlValueDict = {"NZBurl": nzburl}
                newValueDict = {
                    "NZBprov": nzbprov,
                    "BookID": bookid,
                    "NZBdate": formatter.today(),
                    "NZBtitle": nzbtitle,
                    "Status": "Skipped"
                }
                myDB.upsert("wanted", newValueDict, controlValueDict)

                snatchedbooks = myDB.action(
                    'SELECT * from books WHERE BookID=? and Status="Snatched"',
                    [bookid]).fetchone()
                if not snatchedbooks:
                    snatch = DownloadMethod(bookid, nzbprov, nzbtitle, nzburl)
                time.sleep(1)
Exemplo n.º 4
0
def searchbook(bookid=None):

    myDB = database.DBConnection()

    if bookid:
        searchbooks = myDB.select(
            'SELECT AuthorName, BookName from books WHERE BookID=? AND Status="Wanted"',
            [bookid])
    else:
        searchbooks = myDB.select(
            'SELECT AuthorName, Bookname from books WHERE Status="Wanted"')

    for searchbook in searchbooks:
        author = searchbook[0]
        book = searchbook[1]
        logger.info('Searching for %s - %s.' % (author, book))

        dic = {
            '...': '',
            ' & ': ' ',
            ' = ': ' ',
            '?': '',
            '$': 's',
            ' + ': ' ',
            '"': '',
            ',': '',
            '*': ''
        }

        author = formatter.latinToAscii(formatter.replace_all(author, dic))
        book = formatter.latinToAscii(formatter.replace_all(book, dic))

        searchterm = author + ' ' + book
        searchterm = re.sub('[\.\-\/]', ' ', searchterm).encode('utf-8')

        resultlist = []

        if not lazylibrarian.SAB_HOST and not lazylibrarian.BLACKHOLE:
            logger.info('No downloadmethod is set, use SABnzbd or blackhole')

        if not lazylibrarian.NEWZNAB:
            logger.info('No providers are set.')

        if lazylibrarian.NEWZNAB:
            logger.info('Searching NZB at provider %s ...' %
                        lazylibrarian.NEWZNAB_HOST)
            resultlist = providers.NewzNab(searchterm, resultlist)


# FUTURE-CODE
#        if lazylibrarian.NEWZBIN:
#            logger.info('Searching NZB at provider %s ...' % lazylibrarian.NEWZBIN)
#            resultlist = providers.Newzbin(searchterm, resultlist)

#        if lazylibrarian.NZBMATRIX:
#            logger.info('Searching NZB at provider %s ...' % lazylibrarian.NZBMATRIX)
#            resultlist = providers.NZBMatrix(searchterm, resultlist)

#        if lazylibrarian.NZBSORG:
#            logger.info('Searching NZB at provider %s ...' % lazylibrarian.NZBSORG)
#            resultlist = providers.NZBsorg(searchterm, resultlist)

        if resultlist is None:
            logger.info(
                "Search didn't have results. Adding book %s - %s to queue." %
                (author, book))

        else:
            for nzb in resultlist:
                nzbtitle = nzb['nzbtitle']
                nzburl = nzb['nzburl']
                nzbprov = nzb['nzbprov']

                #save nzb's to database for later use
                snatchedbooks = myDB.action(
                    'SELECT * from wanted WHERE BookID=? and Status="Snatched"',
                    [bookid]).fetchone()
                if snatchedbooks:
                    "Book with BookID %s allready snatched, skipped this NZB."
                    controlValueDict = {"BookID": bookid}
                    newValueDict = {
                        "NZBprov": nzbprov,
                        "NZBdate": formatter.today(),
                        "NZBurl": nzburl,
                        "NZBtitle": nzbtitle,
                        "Status": "Skipped"
                    }
                    myDB.upsert("wanted", newValueDict, controlValueDict)

                else:
                    snatch = DownloadMethod(bookid, nzbprov, nzbtitle, nzburl)
Exemplo n.º 5
0
def searchbook(books=None):

    # rename this thread
    threading.currentThread().name = "SEARCHBOOKS"
    myDB = database.DBConnection()
    searchlist = []
    searchlist1 = []

    if books is None:
        # We are performing a backlog search
        searchbooks = myDB.select(
            'SELECT BookID, AuthorName, Bookname from books WHERE Status="Wanted"'
        )

        # Clear cache
        if os.path.exists(".ProviderCache"):
            for f in os.listdir(".ProviderCache"):
                os.unlink("%s/%s" % (".ProviderCache", f))

        # Clearing throttling timeouts
        t = SimpleCache.ThrottlingProcessor()
        t.lastRequestTime.clear()
    else:
        # The user has added a new book
        searchbooks = []
        for book in books:
            searchbook = myDB.select(
                'SELECT BookID, AuthorName, BookName from books WHERE BookID=? AND Status="Wanted"',
                [book['bookid']])
            for terms in searchbook:
                searchbooks.append(terms)

    for searchbook in searchbooks:
        bookid = searchbook[0]
        author = searchbook[1]
        book = searchbook[2]

        dic = {
            '...': '',
            ' & ': ' ',
            ' = ': ' ',
            '?': '',
            '$': 's',
            ' + ': ' ',
            '"': '',
            ',': '',
            '*': '',
            ':': '',
            ';': ''
        }
        dicSearchFormatting = {'.': ' +', ' + ': ' '}

        author = formatter.latinToAscii(formatter.replace_all(author, dic))
        book = formatter.latinToAscii(formatter.replace_all(book, dic))

        # TRY SEARCH TERM just using author name and book type
        author = formatter.latinToAscii(
            formatter.replace_all(author, dicSearchFormatting))
        searchterm1 = author  # + ' ' + lazylibrarian.EBOOK_TYPE
        searchterm1 = re.sub('[\.\-\/]', ' ', searchterm1).encode('utf-8')
        searchterm1 = re.sub(r'\(.*?\)', '', searchterm1).encode('utf-8')
        searchterm1 = re.sub(r"\s\s+", " ",
                             searchterm1)  # strip any double white space
        searchlist.append({
            "bookid": bookid,
            "bookName": searchbook[2],
            "authorName": searchbook[1],
            "searchterm": searchterm1.strip()
        })

    if not lazylibrarian.SAB_HOST and not lazylibrarian.BLACKHOLE:
        logger.info('No download method is set, use SABnzbd or blackhole')

    if not lazylibrarian.NEWZNAB and not lazylibrarian.NEWZNAB2:
        logger.info('No providers are set. use NEWZNAB.')

    counter = 0
    for book in searchlist:
        resultlist = []
        if lazylibrarian.NEWZNAB:
            logger.debug('Searching NZB\'s at provider %s ...' %
                         lazylibrarian.NEWZNAB_HOST)
            resultlist = providers.NewzNab(book, "1")

        if lazylibrarian.NEWZNAB2:
            logger.debug('Searching NZB\'s at provider %s ...' %
                         lazylibrarian.NEWZNAB_HOST2)
            resultlist += providers.NewzNab(book, "2")

        if not resultlist:
            logger.debug("Adding book %s to queue." % book['searchterm'])

        else:
            dictrepl = {
                '...': '',
                ' & ': ' ',
                ' = ': ' ',
                '?': '',
                '$': 's',
                ' + ': ' ',
                '"': '',
                ',': '',
                '*': '',
                '(': '',
                ')': '',
                '[': '',
                ']': '',
                '#': '',
                '0': '',
                '1': '',
                '2': '',
                '3': '',
                '4': '',
                '5': '',
                '6': '',
                '7': '',
                '8': '',
                '9': '',
                '\'': '',
                ':': '',
                '!': '',
                '-': '',
                '\s\s': ' ',
                ' the ': ' ',
                ' a ': ' ',
                ' and ': ' ',
                ' to ': ' ',
                ' of ': ' ',
                ' for ': ' ',
                ' my ': ' ',
                ' in ': ' ',
                ' at ': ' ',
                ' with ': ' '
            }
            bookName = book['bookName']
            bookID = book['bookid']
            bookName = re.sub('[\.\-\/]', ' ', bookName)
            bookName = re.sub(r'\(.*?\)', '', bookName)
            bookName = formatter.latinToAscii(
                formatter.replace_all(bookName.lower(), dictrepl)).strip()
            logger.debug(u'bookName %s' % bookName)
            addedCounter = 0

            for nzb in resultlist:
                nzbTitle = formatter.latinToAscii(
                    formatter.replace_all(
                        str(nzb['nzbtitle']).lower(), dictrepl)).strip()
                logger.debug(u'nzbName %s' % nzbTitle)
                logger.debug("NZB Match %: " +
                             str(fuzz.partial_ratio(bookName, nzbTitle)))
                if (fuzz.partial_ratio(bookName, nzbTitle) > 80):
                    logger.debug(u'FOUND %s' % nzbTitle.lower())
                    addedCounter = addedCounter + 1
                    bookid = nzb['bookid']
                    nzbTitle = (book["authorName"] + ' - ' + book['bookName'] +
                                ' LL.(' + bookID + ')').strip()
                    nzburl = nzb['nzburl']
                    nzbprov = nzb['nzbprov']

                    controlValueDict = {"NZBurl": nzburl}
                    newValueDict = {
                        "NZBprov": nzbprov,
                        "BookID": bookid,
                        "NZBdate": formatter.today(),
                        "NZBtitle": nzbTitle,
                        "Status": "Skipped"
                    }
                    myDB.upsert("wanted", newValueDict, controlValueDict)

                    snatchedbooks = myDB.action(
                        'SELECT * from books WHERE BookID=? and Status="Snatched"',
                        [bookid]).fetchone()
                    if not snatchedbooks:
                        snatch = DownloadMethod(bookid, nzbprov, nzbTitle,
                                                nzburl)
                    break
            if addedCounter == 0:
                logger.info("No nzb's found for " +
                            (book["authorName"] + ' ' + bookName).strip() +
                            ". Adding book to queue.")
        counter = counter + 1
Exemplo n.º 6
0
def searchbook(books=None):

    # rename this thread
    threading.currentThread().name = "SEARCHBOOKS"
    myDB = database.DBConnection()
    searchlist = []

    if books is None:
        logger.debug('Searching for all books with status WANTED')
        searchbooks = myDB.select(
            'SELECT BookID, AuthorName, Bookname from books WHERE Status="Wanted"'
        )
    else:
        searchbooks = []
        for book in books:
            logger.debug('Looking for BookID %s ' % book['bookid'])
            searchbook = myDB.select(
                'SELECT BookID, AuthorName, BookName from books WHERE BookID=? AND Status="Wanted"',
                [book['bookid']])
            for terms in searchbook:
                searchbooks.append(terms)

    for searchbook in searchbooks:
        bookid = searchbook[0]
        author = searchbook[1]
        book = searchbook[2]

        author = MakeSearchTermWebSafe(author)
        book = MakeSearchTermWebSafe(book)
        searchterm = author + ' ' + book
        searchlist.append({
            "bookid": bookid,
            "searchterm": searchterm,
            "author": author,
            "title": book
        })

    if not lazylibrarian.SAB_HOST and not lazylibrarian.BLACKHOLE:
        logger.info(
            'No download method is set, use SABnzbd or blackhole. Now your wasting your time. Check Config page'
        )
        return

    if not lazylibrarian.NEWZNAB and not lazylibrarian.NZBMATRIX and not lazylibrarian.USENETCRAWLER:
        logger.info(
            'No providers are set. Use Config settings and enable at least 1')
        return

    for book in searchlist:
        resultlist = []
        if lazylibrarian.NEWZNAB and not resultlist:
            logger.info('Searching NZB\'s at provider %s ...' %
                        lazylibrarian.NEWZNAB_HOST)
            resultlist = providers.NewzNab(book)

        if lazylibrarian.NZBMATRIX and not resultlist:
            logger.info('Searching NZB at provider NZBMatrix ...')
            resultlist = providers.NZBMatrix(book)

        if lazylibrarian.USENETCRAWLER and not resultlist:
            logger.info('Searching NZB\'s at provider UsenetCrawler ...')
            resultlist = providers.UsenetCrawler(book)

        if not resultlist:
            logger.info(
                "Search didn't have results. Adding book %s to queue." %
                book['searchterm'])

        else:
            for nzb in resultlist:
                bookid = nzb['bookid']
                nzbtitle = nzb['nzbtitle']
                nzburl = nzb['nzburl']
                nzbprov = nzb['nzbprov']

                controlValueDict = {"NZBurl": nzburl}
                newValueDict = {
                    "NZBprov": nzbprov,
                    "BookID": bookid,
                    "NZBdate": formatter.today(),
                    "NZBtitle": nzbtitle,
                    "Status": "Skipped"
                }
                myDB.upsert("wanted", newValueDict, controlValueDict)

                snatchedbooks = myDB.action(
                    'SELECT * from books WHERE BookID=? and Status="Snatched"',
                    [bookid]).fetchone()
                if not snatchedbooks:
                    snatch = DownloadMethod(bookid, nzbprov, nzbtitle, nzburl)
                time.sleep(1)