Beispiel #1
0
    def move_cover_from_tmp(self, filename):
        if self.cover:
            self.remove_cover()

        if filename:
            self.cover = filename
            src = tmp_cover_upload_set.path(self.cover)
            dest_path = cover_upload_set.config.destination

            if os.path.exists(os.path.join(dest_path, self.cover)):
                self.cover = cover_upload_set.resolve_conflict(dest_path, self.cover)

            shutil.move(src, cover_upload_set.path(self.cover))
Beispiel #2
0
def add_book_bulk(batch, root, book_file, cover_file, metadata):
    if 'title' not in metadata or not metadata['title'].strip():
        raise ValueError("Title must not be blank: " + book_file)

    ids = {'isbn': metadata.get('id_isbn'),
           'calibre': metadata.get('id_calibre'),
           'amazon': metadata.get('id_amazon'),
           'google': metadata.get('id_google')}

    for id_type, identifer in ids.iteritems():
        if identifer:
            if get_book_by_identifier(id_type, identifer):
                raise RuntimeError("There is already a book with identifier [" + id_type + ": " + identifer + "]!")

    book = Book()
    book.batch = batch
    book.title = metadata['title']
    book.title_sort = metadata['title_sort'] if 'title_sort' in metadata else metadata['title']
    book.description = metadata['description'] if 'description' in metadata else None
    book.series_seq = int(metadata['series_index']) if 'series_index' in metadata else None
    book.public = True
    book.user = get_user('devin')
    book.created_at = datetime.now()

    book.id_isbn = ids['isbn']
    book.id_amazon = ids['amazon']
    book.id_google = ids['google']
    book.id_calibre = ids['calibre']

    taxonomies = {}

    if 'publisher' in metadata:
        taxonomies['publisher'] = [metadata['publisher']]
    if 'series' in metadata:
        taxonomies['series'] = [metadata['series']]

    if 'creator' in metadata and 'creator_sort' in metadata:
        if isinstance(metadata['creator'], list):
            taxonomies['author'] = []
            for i, c in enumerate(metadata['creator']):
                taxonomies['author'].append((c, metadata['creator_sort'][i]))
        else:
            taxonomies['author'] = [ (metadata['creator'], metadata['creator_sort'])]
    elif 'creator' in metadata:
        if isinstance(metadata['creator'], list):
            taxonomies['author'] = []
            for i, c in enumerate(metadata['creator']):
                taxonomies['author'].append((c, c))
        else:
            taxonomies['author'] = [ (metadata['creator'], metadata['creator'])]

    if 'subject' in metadata:
        tags = []
        if not isinstance(metadata['subject'], list):
            metadata['subject'] = [metadata['subject']]

        for subject in metadata['subject']:
            tags.append(subject.lower())

        if len(tags) > 0:
            taxonomies['tag'] = tags

    book.update_taxonomies(taxonomies)

    cover_dest_path = cover_upload_set.config.destination
    new_cover_file = re.sub('[^0-9a-zA-Z\._\-]+', '', cover_file.replace(" ", "_"))

    if os.path.exists(os.path.join(cover_dest_path, new_cover_file)):
        new_cover_file = cover_upload_set.resolve_conflict(cover_dest_path, new_cover_file)
        
    shutil.copy(os.path.join(root, cover_file), cover_upload_set.path(new_cover_file))
    
    book_dest_path = book_upload_set.config.destination
    new_book_file = re.sub('[^0-9a-zA-Z\._\-]+', '', book_file.replace(" ", "_"))

    if os.path.exists(os.path.join(book_dest_path, new_book_file)):
        new_book_file = book_upload_set.resolve_conflict(book_dest_path, new_book_file)
        
    shutil.copy(os.path.join(root, book_file), book_upload_set.path(new_book_file))

    book.cover = new_cover_file
    book.filename = new_book_file

    db.session.add(book)

    if 'rating' in metadata and metadata['rating'] > 0:
        book.user.get_book_meta(book.id).rating = metadata['rating']

    db.session.commit()

    if app.config['WRITE_META_ON_SAVE']:
       book.write_meta()

    book.update_word_count()

    return True