From 32b23273bd90a44ac7b2b964e923fdce56bd509f Mon Sep 17 00:00:00 2001 From: tema Date: Tue, 5 Sep 2023 13:05:40 +0300 Subject: [PATCH] Configuration modifcation and parser photo saver --- configs/configure.py | 43 ++++++++++++++++++++++++++++--------------- parser/parser.py | 3 +++ parser/utils.py | 11 ++++++++++- 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/configs/configure.py b/configs/configure.py index 37f296c..0fb4916 100644 --- a/configs/configure.py +++ b/configs/configure.py @@ -1,15 +1,25 @@ from configparser import ConfigParser -from easydict import EasyDict as edict +from typing import Any 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): config = ConfigParser() config.read(CONFIG_FILE) + self.config = config self.data = dict() for section in config.sections(): @@ -18,17 +28,20 @@ class Configure: for key, value in config.items(section): self.data[section][key] = value - 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'] + self.config_folder = config.get("Docs_Settings", "Config_folder").rstrip("/") - def __getattr__(self, name): - if name in ["documentid", "data_file", "credentials_file", "token_file"]: - return self.data[name] - 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!") + @property + def documentid(self): + return self.config.get("Docs_Settings", 'Document_ID') + + @property + def data_file(self): + return self.config_folder + "/" + self.config.get("Docs_Settings", "data_file") + + @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'] \ No newline at end of file diff --git a/parser/parser.py b/parser/parser.py index e10656e..1d86b2d 100644 --- a/parser/parser.py +++ b/parser/parser.py @@ -45,6 +45,9 @@ def docs_parse() -> None: document = service.documents().get(documentId=config.documentid).execute() if os.path.exists(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: json.dump(document, f, ensure_ascii=False) diff --git a/parser/utils.py b/parser/utils.py index e5ddfb3..cb59a8e 100644 --- a/parser/utils.py +++ b/parser/utils.py @@ -193,13 +193,22 @@ class Helper(): @classmethod 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 id_doc in document['inlineObjects']: link = (document ['inlineObjects'][id_doc]['inlineObjectProperties'] ['embeddedObject']['imageProperties']['contentUri']) 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 def find_image(cls, document):