Skip to content

Commit 37bd89f

Browse files
committed
ready more actions
1 parent a2b1ae3 commit 37bd89f

File tree

2 files changed

+87
-26
lines changed

2 files changed

+87
-26
lines changed

main.py

+84-25
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,92 @@
1-
import re
1+
import base64
2+
import json
3+
import os
4+
import time
25

6+
import requests
7+
from dotenv import load_dotenv
8+
from requests import RequestException
39

4-
class TextProcessor:
5-
def __init__(self, text):
6-
self.text = text
10+
load_dotenv()
711

8-
def extract_emails(self):
9-
def extract_email(text):
10-
pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.(ru|com|org|net)'
11-
match = re.search(pattern, text)
12-
if match:
13-
return match.group(0)
14-
return None
12+
ORG_USERNAME = os.environ.get("ORG_USERNAME")
13+
ORG_PASSWORD = os.environ.get("ORG_PASSWORD")
14+
ORG_GRANT_TYPE = os.environ.get("ORG_GRANT_TYPE")
15+
ORG_TOKEN_URL = os.environ.get("ORG_TOKEN_URL")
16+
ORG_CONSUMER_KEY = os.environ.get("ORG_CONSUMER_KEY")
17+
ORG_CONSUMER_SECRET = os.environ.get("ORG_CONSUMER_SECRET")
18+
ORG_COMPANY_URL = os.environ.get("ORG_COMPANY_URL")
1519

16-
emails = []
17-
for t in self.text.split():
18-
email = extract_email(t)
19-
if email:
20-
emails.append(email)
20+
cache = {}
2121

22-
emails = set(emails)
23-
return sorted(emails)
2422

23+
def get_token() -> dict | str:
24+
"""Get authentication token and cache it."""
25+
if "org_token" in cache and cache["org_token"]["expires_at"] > time.time():
26+
return cache["org_token"]["token"]
2527

26-
input_text = input()
27-
processor = TextProcessor(input_text)
28+
payload = {
29+
"grant_type": ORG_GRANT_TYPE,
30+
"username": ORG_USERNAME,
31+
"password": ORG_PASSWORD,
32+
}
33+
auth_str = f"{ORG_CONSUMER_KEY}:{ORG_CONSUMER_SECRET}"
34+
auth_base64 = base64.b64encode(auth_str.encode("utf-8")).decode("utf-8")
2835

29-
found_emails = processor.extract_emails()
30-
if found_emails:
31-
print("\n".join(found_emails))
32-
else:
33-
print("Не найдено")
36+
headers = {
37+
"Content-Type": "application/x-www-form-urlencoded",
38+
"Authorization": f"Basic {auth_base64}",
39+
}
40+
try:
41+
response = requests.post(ORG_TOKEN_URL, headers=headers, data=payload)
42+
response.raise_for_status()
43+
token = response.json().get("access_token")
44+
expires_in = response.json().get("expires_in", 3600) # Standart 1 soat
45+
46+
if token:
47+
cache["org_token"] = {
48+
"token": token,
49+
"expires_at": time.time() + expires_in # Token muddati
50+
}
51+
return token
52+
except RequestException as e:
53+
return {"error": "Failed to obtain token", "details": str(e)}
54+
55+
56+
def get_company(inn: int | str) -> dict:
57+
"""Fetch company details by INN from an external service."""
58+
payload = json.dumps({"tin": str(inn)})
59+
60+
token = get_token()
61+
if isinstance(token, dict):
62+
return token
63+
64+
headers = {
65+
"Content-Type": "application/json",
66+
"Authorization": f"Bearer {token}",
67+
}
68+
69+
try:
70+
response = requests.post(ORG_COMPANY_URL, headers=headers, data=payload)
71+
if response.status_code == 401:
72+
token = get_token()
73+
if isinstance(token, dict):
74+
return token
75+
76+
headers["Authorization"] = f"Bearer {token}"
77+
response = requests.post(ORG_COMPANY_URL, headers=headers, data=payload)
78+
79+
response.raise_for_status()
80+
response_js = response.json()
81+
return {
82+
"name": response_js.get("name"),
83+
"address": response_js.get("address"),
84+
"oked": response_js.get("nc6Code"),
85+
"ns_code": response_js.get("ns10Code"),
86+
}
87+
88+
except RequestException as e:
89+
return {"error": "Failed to fetch company details", "details": str(e)}
90+
91+
92+
print(get_company("200524845"))

requirements.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
black==25.1.0
1+
black==25.1.0
2+
requests==2.32.3
3+
python-dotenv==1.0.1

0 commit comments

Comments
 (0)