Code:
import os.path
import textwrap
import os
import glob
from calibre.customize import FileTypePlugin, MetadataReaderPlugin, MetadataWriterPlugin
from calibre.constants import numeric_version
class FileDirMetadataReader(MetadataReaderPlugin):
name = 'FileDir metadata'
file_types = set(['pdf', 'doc', 'txt', 'htm', 'rtf', 'mobi', 'djvu', 'prc'])
description = ('Extract author and book name from folder/filename')
version = numeric_version
def get_metadata(self, stream, ftype):
from calibre.ebooks.metadata import MetaInformation
path = stream.name
author = os.path.split( os.path.split(path)[0] )[1]
title = os.path.splitext( os.path.basename( path) )[0]
author = author.replace("_"," ")
title = title.replace("_"," ")
return MetaInformation( title, [author] )