Example #1
0
def scan_folder_of_chapters(book_folder, output_folder):
    '''Given a folder look for text/txt files.
        The name should be of the form,

        e.g.
          chapter 01 - In the begining.txt
          chapter 02 - A little bit later.text

        and also some cover art
          cover.jpg

    '''

    book = Epub()
    book.output_folder = output_folder

    # extract book title and author form folder name
    title_author = book_folder[6:].split('-')
    if len(title_author) < 2:
        print "Warning: '%s' isn't for the form 'title - author'," +\
            " will skip this folder"
        return False

    book.title = title_author[0].strip()
    book.primary_author = "-".join(title_author[1:]).strip()

    chapters = []

    # file list, sorted
    filelist = glob("%s/*.*" % book_folder)
    filelist.sort(key=natural_number_sorting)

    for chapter in filelist:

        matched = re.match(u'.*\.[tT][xX][tT]$|.*\.[tT][eE][xX][tT]$', chapter)

        if matched:

            title = chapter.replace("%s/" % book_folder, '')
            title = "-".join(title.split('-')[1:])
            title = title.replace('.text', '')
            title = title.replace('.txt', '')

            chapters.append((title, open(chapter, 'r').read()))

    book.chapters = chapters

    # add an image file
    for image_name in ('jpg', 'JPG', 'jpeg', 'JPEG', 'Jpeg', 'Jpg'):
        image_name = '%s/cover.%s' % (book_folder, image_name)
        if path.exists(image_name):
            book.cover_art = image_name
            break
    else:
        print 'Warning: The cover art file, "%s/cover.jpg", wasn\'t found' %\
                                                                    book_folder
        book.cover_art = None

    book.save()

    return True
Example #2
0
def scan_folder_of_chapters(book_folder, output_folder):
    '''Given a folder look for text/txt files.
        The name should be of the form,

        e.g.
          chapter 01 - In the begining.txt
          chapter 02 - A little bit later.text

        and also some cover art
          cover.jpg

    '''

    book = Epub()
    book.output_folder = output_folder

    # extract book title and author form folder name
    title_author = book_folder[6:].split('-')
    if len(title_author) < 2:
        print "Warning: '%s' isn't for the form 'title - author'," +\
            " will skip this folder"
        return False

    book.title = title_author[0].strip()
    book.primary_author = "-".join(title_author[1:]).strip()

    chapters = []

    # file list, sorted
    filelist = glob("%s/*.*" % book_folder)
    filelist.sort(key=natural_number_sorting)

    for chapter in filelist:

        matched = re.match(u'.*\.[tT][xX][tT]$|.*\.[tT][eE][xX][tT]$', chapter)

        if matched:

            title = chapter.replace("%s/" % book_folder, '')
            title = "-".join(title.split('-')[1:])
            title = title.replace('.text', '')
            title = title.replace('.txt', '')

            chapters.append((title, open(chapter, 'r').read()))

    book.chapters = chapters

    # add an image file
    for image_name in ('jpg', 'JPG', 'jpeg', 'JPEG', 'Jpeg', 'Jpg'):
        image_name = '%s/cover.%s' % (book_folder, image_name)
        if path.exists(image_name):
            book.cover_art = image_name
            break
    else:
        print 'Warning: The cover art file, "%s/cover.jpg", wasn\'t found' %\
                                                                    book_folder
        book.cover_art = None

    book.save()

    return True