From 59f420c72521665d3b566da9e6dab88e051724f5 Mon Sep 17 00:00:00 2001 From: Ilya Sapunov Date: Thu, 2 May 2024 16:03:21 +0300 Subject: [PATCH] Added method for downloading reports in PDF, added packaging tools. --- kontur_focus/focus.py | 28 +++++++++++++++++++++++++++- requirements.txt | 4 +++- setup.py | 19 +++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 setup.py diff --git a/kontur_focus/focus.py b/kontur_focus/focus.py index 6fcd61c..5702da2 100644 --- a/kontur_focus/focus.py +++ b/kontur_focus/focus.py @@ -9,6 +9,7 @@ class Focus: _access_key = None _inn = None _orgn = None + _basedir = os.path.abspath(os.path.dirname(__file__)) def __init__(self, inn: str, ogrn: str = None): load_dotenv() @@ -21,7 +22,15 @@ class Focus: full_url = f'{self._base_url}{path}' payload = {'key': self._access_key, 'inn': self._inn, 'ogrn': self._orgn} - return requests.get(url=full_url, params=payload).json() + try: + response = requests.get(url=full_url, params=payload) + + if response.headers['Content-Type'] == 'application/pdf': + return response + else: + return response.json() + except Exception: + return 'Request error!' def base_info(self): return self._r_get('/req') @@ -29,6 +38,23 @@ class Focus: def advanced_info(self): return self._r_get('/egrDetails') + def excerpt(self, path=None): + response = self._r_get('/excerpt') + filename = f'{self._inn}_report.pdf' + + if not path: + file_path = os.path.join(self._basedir, filename) + else: + file_path = os.path.join(path, filename) + + try: + with open(file_path, mode='wb') as file: + file.write(response.content) + + return 'File saved' + except Exception: + return 'Saving error!' + def founders_history(self): return self._r_get('/foundersHistory') diff --git a/requirements.txt b/requirements.txt index d44fe44..a57ddfd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,4 @@ requests -python-dotenv \ No newline at end of file +python-dotenv +twine +wheel \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..5b7e8ac --- /dev/null +++ b/setup.py @@ -0,0 +1,19 @@ +from setuptools import setup, find_packages + +setup( + name="kontur_focus", + version="0.1.0", + author="Ilya Sapunov", + author_email="sapunov@selectel.ru", + description="Библиотека-обертка для взаимодействия с REST API Контур.Фокус", + url="https://git.selectel.org/is/public/kontur-focus-lib", + license="MIT", + packages=find_packages(), + install_requires=["requests", "python-dotenv"], + classifiers=[ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", + ], + python_requires='>=3.9', +) \ No newline at end of file