mirror of
https://github.com/gryf/ebook-converter.git
synced 2026-01-01 07:22:26 +01:00
Added missing txt metadata module
This commit is contained in:
41
ebook_converter/ebooks/metadata/txt.py
Normal file
41
ebook_converter/ebooks/metadata/txt.py
Normal file
@@ -0,0 +1,41 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||
|
||||
__license__ = 'GPL v3'
|
||||
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
|
||||
|
||||
'''
|
||||
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<title>.+)[ ]*(\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
|
||||
Reference in New Issue
Block a user