office meta plugin

This commit is contained in:
randogoth 2023-06-09 01:23:14 +03:00
parent 2c239bc07e
commit 0e7862ee4a
11 changed files with 236 additions and 27 deletions

View file

@ -5,7 +5,7 @@ from pathlib import Path
from rich.console import Console
from rich.table import Table
from orvani import OrvaniTag, CONTEXT_FILE, CACHE_FILE, CONTEXTS
from plugins import frontmatter, hashdoubleslash, id3
from plugins import frontmatter, hashdoubleslash, id3, office
def mktable(query: str, index: dict):
console = Console()
@ -89,6 +89,7 @@ def main():
frontmatter.reader(o)
hashdoubleslash.reader(o)
id3.reader(o)
office.reader(o)
# orvani.metadata()

82
orvani/plugins/office.py Normal file
View file

@ -0,0 +1,82 @@
import re
import mimetypes
import shutil
from pathlib import Path
from olefile import OleFileIO
NAME = 'office'
TYPES = ['.doc', '.xls', '.ppt', '.docx', '.xlsx', '.pptx', '.odt', '.ods', '.odp']
RE_ODT_CTX = r"<meta:keyword>((.*))<\/meta:keyword>"
RE_ODT_NOTE = r"<dc:description>((.*))<\/dc:description>"
# Office Document processing inspired by
# https://github.com/kappapiana/anonymize/blob/master/anonymize.py
def unzip_file(orig_file, tmp_dir):
type = mimetypes.guess_type(orig_file)
filetype = {
"application/vnd.oasis.opendocument.text" : "odt",
"application/vnd.oasis.opendocument.spreadsheet" : "ods",
"application/vnd.oasis.opendocument.presentation" : "odp",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document" : "docx",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" : "xlsx",
"application/vnd.openxmlformats-officedocument.presentationml.presentation" : "pptx",
}
if type[0] in filetype:
shutil.unpack_archive(orig_file, tmp_dir, 'zip')
return filetype[type[0]]
else:
return type[0]
def reader(o):
file = Path(o.path)
suffixes = TYPES
if NAME in o.config:
if 'types' in o.config[NAME]:
suffixes = o.config[NAME]['types']
if file.suffix in suffixes:
tmp_folder = Path('unzip__' + file.stem)
file_type = unzip_file(o.path, tmp_folder)
tags = note = []
# OpenOffice / LibreOffice / Open Document Format
if file_type in ['odt', 'ods', 'odp']:
meta_file = tmp_folder / 'meta.xml'
meta_content = meta_file.read_text()
tagsearch = re.findall(RE_ODT_CTX, meta_content)
if tagsearch:
tags = list(set(tagsearch[0]))
notesearch = re.findall(RE_ODT_NOTE, meta_content)
if notesearch:
note = list(set(notesearch[0]))
argv = [x for x in ' '.join(tags).split(' ') + note if x != '']
context = o.context(argv)
for context_key in context.keys():
o.update(o.path, context, context_key)
shutil.rmtree(tmp_folder)
# Microsoft Office Format
elif file_type in ['docx', 'xlsx', 'pptx']:
meta_file = tmp_folder / 'docProps' / 'core.xml'
meta_content = meta_file.read_text()
tagsearch = re.findall(RE_ODT_CTX, meta_content)
if tagsearch:
tags = list(set(tagsearch[0]))
notesearch = re.findall(RE_ODT_NOTE, meta_content)
if notesearch:
note = list(set(notesearch[0]))
argv = [x for x in ' '.join(tags).split(' ') + note if x != '']
context = o.context(argv)
for context_key in context.keys():
o.update(o.path, context, context_key)
shutil.rmtree(tmp_folder)
elif file_type in ['application/msword', 'application/vnd.ms-excel', 'application/vnd.ms-powerpoint']:
ole = OleFileIO(o.path)
meta = ole.get_metadata()
ole.close()
if meta.keywords:
tags = meta.keywords.decode('UTF-8').split(' ')
if meta.comments:
note = [meta.comments.decode('UTF-8')]
argv = [x for x in tags + note if x != '']
context = o.context(argv)
for context_key in context.keys():
o.update(o.path, context, context_key)