From 01e617f20686d353cd3fa922376b65c8f2339fd5 Mon Sep 17 00:00:00 2001 From: gryf Date: Sun, 12 Apr 2020 12:06:56 +0200 Subject: [PATCH] Added missing txt metadata module --- ebook_converter/ebooks/metadata/txt.py | 41 ++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 ebook_converter/ebooks/metadata/txt.py diff --git a/ebook_converter/ebooks/metadata/txt.py b/ebook_converter/ebooks/metadata/txt.py new file mode 100644 index 0000000..07b2a2a --- /dev/null +++ b/ebook_converter/ebooks/metadata/txt.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import, division, print_function, unicode_literals + +__license__ = 'GPL v3' +__copyright__ = '2009, John Schember ' + +''' +Read meta information from TXT files +''' + +import re, os + +from ebook_converter.ebooks.metadata import MetaInformation + + +def get_metadata(stream, extract_cover=True): + ''' + Return metadata as a L{MetaInfo} object + ''' + name = getattr(stream, 'name', '').rpartition('.')[0] + if name: + name = os.path.basename(name) + mi = MetaInformation(name or _('Unknown'), [_('Unknown')]) + stream.seek(0) + + mdata = '' + for x in range(0, 4): + line = stream.readline().decode('utf-8', 'replace') + if not line: + break + else: + mdata += line + + mdata = mdata[:1024] + + mo = re.search('(?u)^[ ]*(?P.+)[ ]*(\n{3}|(\r\n){3}|\r{3})[ ]*(?P<author>.+)[ ]*(\n|\r\n|\r)$', mdata) + if mo is not None: + mi.title = mo.group('title') + mi.authors = mo.group('author').split(',') + + return mi