Configuration modifcation and parser photo saver

This commit is contained in:
tema 2023-09-05 13:05:40 +03:00
parent a2477521fa
commit 32b23273bd
Signed by: tema
GPG Key ID: 21FDB6D162488F6F
3 changed files with 41 additions and 16 deletions

View File

@ -1,15 +1,25 @@
from configparser import ConfigParser from configparser import ConfigParser
from easydict import EasyDict as edict from typing import Any
CONFIG_FILE = 'config.ini' CONFIG_FILE = 'config.ini'
class Configure: class Cfg:
def __getattr__(self, name: str) -> Any:
for key in self.data.keys():
if name not in self.data[key]:
continue
return self.data[key][name]
raise NameError("Config options not found!")
class Configure(Cfg):
def __init__(self): def __init__(self):
config = ConfigParser() config = ConfigParser()
config.read(CONFIG_FILE) config.read(CONFIG_FILE)
self.config = config
self.data = dict() self.data = dict()
for section in config.sections(): for section in config.sections():
@ -18,17 +28,20 @@ class Configure:
for key, value in config.items(section): for key, value in config.items(section):
self.data[section][key] = value self.data[section][key] = value
config_folder = config.get("Docs_Settings", "Config_folder").rstrip("/") self.config_folder = config.get("Docs_Settings", "Config_folder").rstrip("/")
self.data["documentid"] = config.get("Docs_Settings", 'Document_ID')
self.data["data_file"] = config_folder + "/" + config.get("Docs_Settings", "data_file")
self.data["credentials_file"] = config_folder + "/" + config.get("Docs_Settings", "credentials_file")
self.data["token_file"] = self.config_folder + "/" + self.data['Docs_Settings']['token_file']
def __getattr__(self, name): @property
if name in ["documentid", "data_file", "credentials_file", "token_file"]: def documentid(self):
return self.data[name] return self.config.get("Docs_Settings", 'Document_ID')
for key in self.data.keys():
if name not in self.data[key]: @property
continue def data_file(self):
return self.data[key][name] return self.config_folder + "/" + self.config.get("Docs_Settings", "data_file")
# raise NameError("Config options not found!")
@property
def credentials_file(self):
return self.config_folder + "/" + self.config.get("Docs_Settings", "credentials_file")
@property
def token_file(self):
return self.config_folder + "/" + self.data['Docs_Settings']['token_file']

View File

@ -46,6 +46,9 @@ def docs_parse() -> None:
if os.path.exists(config.data_file): if os.path.exists(config.data_file):
os.remove(config.data_file) os.remove(config.data_file)
with open("configs/temp.file", 'w') as f:
f.write("1")
with open(config.data_file, 'w') as f: with open(config.data_file, 'w') as f:
json.dump(document, f, ensure_ascii=False) json.dump(document, f, ensure_ascii=False)
f.close() f.close()

View File

@ -193,13 +193,22 @@ class Helper():
@classmethod @classmethod
def get_link_and_download(cls, id_doc, document): def get_link_and_download(cls, id_doc, document):
with open("configs/temp.file") as f:
data = f.read()
if data == "0":
return open("configs/photo.base64", 'rb').read()
if "inlineObjects" in document: if "inlineObjects" in document:
if id_doc in document['inlineObjects']: if id_doc in document['inlineObjects']:
link = (document link = (document
['inlineObjects'][id_doc]['inlineObjectProperties'] ['inlineObjects'][id_doc]['inlineObjectProperties']
['embeddedObject']['imageProperties']['contentUri']) ['embeddedObject']['imageProperties']['contentUri'])
r = requests.get(link, stream=True) r = requests.get(link, stream=True)
return base64.b64encode(r.content).decode('utf-8') photo = base64.b64encode(r.content).decode('utf-8')
with open("configs/photo.base64", 'w') as f:
f.write(photo)
with open("configs/temp.file", 'w') as f:
f.write("0")
return photo
@classmethod @classmethod
def find_image(cls, document): def find_image(cls, document):