Exemple #1
0
def addBookToDB(rc, bookUrl, siteFormat):

    bookText = getFromStorage(bookUrl)

    bookData = {
        'bookUrl': bookUrl,
        'bookText': bookText
    }

    if siteFormat == 'gutenberg':
        parseGutenberg(bookData)

    addNewBook(rc, bookData)
Exemple #2
0
def getLocationsOfWordInBook(rc, wordId, bookId, wordText):

    cursor = rc["db"].cursor()

    selectQuery = 'SELECT *' \
                  'FROM sadnadb.wordsInBooks wb, sadnadb.books b ' \
                  'WHERE wb.wordId = %s and wb.bookId = b.id and b.id = %s '
    cursor.execute(selectQuery, (wordId, bookId))
    result = cursor.fetchall()

    wordLocations = []
    bookFileLocation = result[0][15]
    for r in result:
        wordLocations += [{
            'lineNumber': r[2],
            'wordNumber': r[3],
            'charLocation': r[4],
            'sentenceNumber': r[5],
            'paragraphNumber': r[6],
            'wordNumberInLine': r[7]
        }]

    bookText = getFromStorage(bookFileLocation)

    bookLines = bookText.split('\r\n')

    for temp in wordLocations:
        ln = temp['lineNumber']

        tempLine = bookLines[ln].lower()
        tempPos = tempLine.find(wordText)

        if tempPos != -1:
            parts = [bookLines[ln][:tempPos], bookLines[ln][tempPos + len(wordText):]]

            temp['cite1'] = bookLines[ln - 1].decode('utf-8') + '\r\n' + parts[0].decode('utf-8')

            temp['cite2'] = parts[1].decode('utf-8') + '\r\n' + bookLines[ln + 1].decode('utf-8')

            temp['originalWordText'] = bookLines[ln][tempPos:tempPos + len(wordText)].decode('utf-8')
        else:
            temp['cite'] = bookLines[ln - 1] + '\r\n' + \
                           bookLines[ln] + '\r\n' + \
                           bookLines[ln + 1]

            temp['cite'] = temp['cite'].decode('utf-8')

    return wordLocations