Compare commits
6 Commits
ce12f23426
...
features/f
| Author | SHA1 | Date | |
|---|---|---|---|
| 816da1eb16 | |||
| 03aca0ecb9 | |||
| 67b75e531a | |||
| 084dc53baa | |||
| 6bcac057d1 | |||
| 03a64d6263 |
@@ -1,7 +1,9 @@
|
||||
import os
|
||||
import hashlib
|
||||
import requests
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class AbcpProvider:
|
||||
HOST = "https://id23089.public.api.abcp.ru"
|
||||
@@ -11,25 +13,34 @@ class AbcpProvider:
|
||||
"Content-Type": "application/x-www-form-urlencoded"
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, account="SYSTEM"):
|
||||
self.base_url = self.HOST
|
||||
self.login = os.getenv("ABCP_LOGIN")
|
||||
password = os.getenv("ABCP_PASSWORD")
|
||||
self.password = hashlib.md5(password.encode("utf-8")).hexdigest()
|
||||
|
||||
def get_stock(self, order):
|
||||
def get_stock(self, sku, manufacturer, partner="SYSTEM"):
|
||||
method = "GET"
|
||||
path = "/search/articles"
|
||||
|
||||
for position in order.positions:
|
||||
params = {"number": position.sku, "brand": position.manufacturer, "withOutAnalogs": "1"}
|
||||
position.stock = self._execute(path, method, params)
|
||||
params = {"number": sku, "brand": manufacturer, "withOutAnalogs": "1"}
|
||||
return self._execute(partner, path, method, params)
|
||||
|
||||
def _execute(self, partner, path, method="GET", params={}, data=None, ):
|
||||
params["userlogin"] = os.getenv(f"ABCP_LOGIN_{partner}")
|
||||
params["userpsw"] = hashlib.md5(os.getenv(f"ABCP_PASSWORD_{partner}").encode("utf-8")).hexdigest()
|
||||
|
||||
def _execute(self, path, method="GET", params={}, data=None):
|
||||
params["userlogin"] = self.login
|
||||
params["userpsw"] = self.password
|
||||
response = requests.request(method, self.HOST+path, data=data, headers=self.HEADERS, params=params)
|
||||
if response.status_code != 200:
|
||||
raise Exception(response.text)
|
||||
return response.json()
|
||||
payload = response.json()
|
||||
if response.status_code == 200:
|
||||
logger.debug(f"Получены данные об остатках на складе")
|
||||
result = {
|
||||
"success": True,
|
||||
"data": payload
|
||||
}
|
||||
else:
|
||||
logger.warning(f"ошибка получения данных об остатках на складе: {payload}")
|
||||
|
||||
result = {
|
||||
"success": False,
|
||||
"error": payload
|
||||
}
|
||||
return result
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
# Настройки обработки =================================================================
|
||||
|
||||
# Раздел с общими конфигурационными параметрами ===============================
|
||||
update_interval: 10
|
||||
work_interval: 30
|
||||
update_interval: 1
|
||||
work_interval: 60
|
||||
email_dir: "spareparts"
|
||||
|
||||
# Логирование =================================================================
|
||||
@@ -44,7 +44,7 @@ log:
|
||||
loggers:
|
||||
'':
|
||||
handlers: [console, file, telegram]
|
||||
level: INFO
|
||||
level: DEBUG
|
||||
propagate: False
|
||||
|
||||
__main__:
|
||||
|
||||
30
src/mail_order_bot/configs/amtel.club.yml
Normal file
30
src/mail_order_bot/configs/amtel.club.yml
Normal file
@@ -0,0 +1,30 @@
|
||||
pipeline:
|
||||
# Настраиваем парсинг экселя
|
||||
- handler: BasicExcelParser
|
||||
config:
|
||||
sheet_name: 0
|
||||
key_field: "Номер"
|
||||
mapping:
|
||||
article: "Номер"
|
||||
manufacturer: "Фирма"
|
||||
name: "Наименование"
|
||||
price: "Цена"
|
||||
quantity: "Кол-во"
|
||||
total: "Сумма"
|
||||
|
||||
# Определяем логику обработки заказа (в данном случае все с локального склада)
|
||||
- handler: DeliveryPeriodLocalStore
|
||||
|
||||
# Запрос остатков со склада
|
||||
- handler: GetStock
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
pipeline:
|
||||
- handler: "ConfigurableExcelParser"
|
||||
result_section: "positions"
|
||||
- handler: BasicExcelParser
|
||||
config:
|
||||
sheet_name: 0
|
||||
key_field: "Код детали"
|
||||
@@ -12,6 +11,9 @@ pipeline:
|
||||
quantity: "Кол-\nво"
|
||||
total: "Сумма"
|
||||
|
||||
- handler: GetStock
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
78
src/mail_order_bot/context.py
Normal file
78
src/mail_order_bot/context.py
Normal file
@@ -0,0 +1,78 @@
|
||||
import threading
|
||||
from typing import Any, Dict
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger()
|
||||
|
||||
import threading
|
||||
from typing import Any
|
||||
|
||||
class SingletonMeta(type):
|
||||
_instances = {}
|
||||
|
||||
def __call__(cls, *args, **kwargs):
|
||||
if cls not in cls._instances:
|
||||
instance = super().__call__(*args, **kwargs)
|
||||
cls._instances[cls] = instance
|
||||
return cls._instances[cls]
|
||||
|
||||
|
||||
|
||||
class Context2(metaclass=SingletonMeta):
|
||||
def __init__(self):
|
||||
if not hasattr(self, 'initialized'):
|
||||
self.data = {}
|
||||
self.email_client = None
|
||||
self.initialized = True
|
||||
logger.debug(f"Context создан {id}") # опциональный лог
|
||||
|
||||
|
||||
|
||||
# будет вызван только при первом создании
|
||||
|
||||
|
||||
def clear(self):
|
||||
"""Очищает self.context, устанавливая его в None или пустой словарь"""
|
||||
self.data = {}
|
||||
logger.debug("Context очищен") # опциональный лог
|
||||
|
||||
def set(self, new_context: Dict[str, Any]):
|
||||
"""Устанавливает новый контекст (бонусный метод)"""
|
||||
self.data = new_context
|
||||
logger.debug("Новый контекст установлен")
|
||||
|
||||
|
||||
class ThreadSafeSingletonMeta(type):
|
||||
_instances = {}
|
||||
_lock = threading.Lock()
|
||||
|
||||
def __call__(cls, *args: Any, **kwargs: Any) -> Any:
|
||||
if cls not in cls._instances:
|
||||
with cls._lock:
|
||||
if cls not in cls._instances:
|
||||
# Инициализация ТУТ, не в __init__
|
||||
instance = super().__call__(*args, **kwargs)
|
||||
instance.data = {}
|
||||
instance.email_client = None
|
||||
instance._lock = threading.RLock()
|
||||
cls._instances[cls] = instance
|
||||
|
||||
return cls._instances[cls]
|
||||
|
||||
|
||||
class Context(metaclass=ThreadSafeSingletonMeta):
|
||||
def __init__(self):
|
||||
print(f"Context: {id(self)}, поток {threading.get_ident()}")
|
||||
# будет вызван только при первом создании
|
||||
|
||||
def clear(self):
|
||||
"""Очищает self.context, устанавливая его в None или пустой словарь"""
|
||||
with self._lock:
|
||||
self.data = {}
|
||||
logger.debug("Context очищен") # опциональный лог
|
||||
|
||||
def set(self, new_context: Dict[str, Any]):
|
||||
"""Устанавливает новый контекст (бонусный метод)"""
|
||||
with self._lock:
|
||||
self.data = new_context
|
||||
logger.debug("Новый контекст установлен")
|
||||
@@ -11,38 +11,13 @@ from email.header import decode_header
|
||||
import imaplib
|
||||
import smtplib
|
||||
|
||||
from .objects import EmailMessage, EmailAttachment
|
||||
|
||||
# from .objects import EmailMessage, EmailAttachment
|
||||
|
||||
|
||||
class EmailClient:
|
||||
"""
|
||||
Класс для работы с электронной почтой по протоколам IMAP и SMTP.
|
||||
|
||||
Пример использования:
|
||||
client = EmailClient(
|
||||
imap_host='imap.gmail.com',
|
||||
smtp_host='smtp.gmail.com',
|
||||
email='your_email@gmail.com',
|
||||
password='your_password'
|
||||
)
|
||||
|
||||
# Получить новые письма
|
||||
new_emails = client.get_emails()
|
||||
|
||||
# Отправить письмо
|
||||
msg = EmailMessage(
|
||||
from_addr='sender@example.com',
|
||||
subj='Test',
|
||||
dt=datetime.now(),
|
||||
body='Hello!',
|
||||
attachments=[]
|
||||
)
|
||||
client.send_email(msg, to_addr='recipient@example.com')
|
||||
"""
|
||||
|
||||
def __init__(self, imap_host: str, smtp_host: str, email: str, password: str,
|
||||
imap_port: int = 993, smtp_port: int = 587):
|
||||
|
||||
self.imap_host = imap_host
|
||||
self.smtp_host = smtp_host
|
||||
self.email = email
|
||||
@@ -52,13 +27,13 @@ class EmailClient:
|
||||
self.imap_conn = None
|
||||
|
||||
def connect(self):
|
||||
"""Установить IMAP соединение"""
|
||||
"""Установkение IMAP соединения"""
|
||||
if self.imap_conn is None:
|
||||
self.imap_conn = imaplib.IMAP4_SSL(self.imap_host, self.imap_port)
|
||||
self.imap_conn.login(self.email, self.password)
|
||||
|
||||
def disconnect(self):
|
||||
"""Закрыть IMAP соединение"""
|
||||
"""Закрытие IMAP соединения"""
|
||||
if self.imap_conn:
|
||||
try:
|
||||
self.imap_conn.disconnect()
|
||||
@@ -67,7 +42,48 @@ class EmailClient:
|
||||
pass
|
||||
self.imap_conn = None
|
||||
|
||||
def _decode_header(self, header_value: str) -> str:
|
||||
def __enter__(self):
|
||||
"""Поддержка контекстного менеджера"""
|
||||
self.connect()
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
"""Поддержка контекстного менеджера"""
|
||||
self.disconnect()
|
||||
|
||||
def get_emails_id(self, folder: str = "INBOX", only_unseen: bool = True) -> List[int]:
|
||||
"""Получить список новых электронных писем."""
|
||||
self.connect()
|
||||
self.imap_conn.select(folder, readonly=False)
|
||||
# Ищем письма
|
||||
search_criteria = "(UNSEEN)" if only_unseen else "ALL"
|
||||
status, messages = self.imap_conn.search(None, search_criteria)
|
||||
|
||||
# ToDo сделать обработку ошибок, подумать нужна ли она!
|
||||
if status != "OK":
|
||||
return []
|
||||
|
||||
email_ids = messages[0].split()
|
||||
return email_ids
|
||||
|
||||
def get_email(self, email_id, mark_as_read: bool = True):
|
||||
"""Получить список новых электронных писем."""
|
||||
self.connect()
|
||||
status, msg_data = self.imap_conn.fetch(email_id, "(RFC822)")
|
||||
if status != "OK":
|
||||
pass
|
||||
|
||||
# Парсим письмо
|
||||
raw_email = msg_data[0][1]
|
||||
msg = email.message_from_bytes(raw_email)
|
||||
|
||||
# Помечаем письмо как прочитанное
|
||||
if mark_as_read:
|
||||
self.imap_conn.store(email_id, '+FLAGS', '\\Seen')
|
||||
|
||||
return msg
|
||||
|
||||
def decode_header(self, header_value: str) -> str:
|
||||
"""Декодировать заголовок письма."""
|
||||
if header_value is None:
|
||||
return ""
|
||||
@@ -86,213 +102,3 @@ class EmailClient:
|
||||
decoded_parts.append(str(part))
|
||||
|
||||
return ''.join(decoded_parts)
|
||||
|
||||
def _extract_body(self, msg: email.message.Message) -> str:
|
||||
"""Извлечь текст письма из любого типа содержимого, кроме вложений"""
|
||||
body = ""
|
||||
if msg.is_multipart():
|
||||
for part in msg.walk():
|
||||
content_disposition = str(part.get("Content-Disposition", ""))
|
||||
# Пропускаем вложения
|
||||
if "attachment" in content_disposition.lower():
|
||||
continue
|
||||
try:
|
||||
charset = part.get_content_charset() or 'utf-8'
|
||||
payload = part.get_payload(decode=True)
|
||||
if payload:
|
||||
body_piece = payload.decode(charset, errors='ignore')
|
||||
body += body_piece
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
charset = msg.get_content_charset() or 'utf-8'
|
||||
payload = msg.get_payload(decode=True)
|
||||
if payload:
|
||||
body = payload.decode(charset, errors='ignore')
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return body
|
||||
|
||||
def __extract_email(self, text: str) -> str:
|
||||
match = re.search(r'<([^<>]+)>', text)
|
||||
if match:
|
||||
return match.group(1)
|
||||
return None
|
||||
|
||||
def _extract_first_sender(self, body: str):
|
||||
"""Извлекает адреса отправителей из пересылаемого сообщения. Нужно для отладки"""
|
||||
# Ищем email внутри скобок после строки "Пересылаемое сообщение"
|
||||
pattern = r"Пересылаемое сообщение.*?\((.*?)\)"
|
||||
match = re.search(pattern, body, re.DOTALL)
|
||||
if match:
|
||||
return match.group(1)
|
||||
return None
|
||||
|
||||
def _extract_attachments(self, msg: email.message.Message) -> List[EmailAttachment]:
|
||||
"""Извлечь вложения из письма."""
|
||||
attachments = []
|
||||
|
||||
for part in msg.walk():
|
||||
content_disposition = str(part.get("Content-Disposition", ""))
|
||||
|
||||
if "attachment" in content_disposition:
|
||||
filename = part.get_filename()
|
||||
if filename:
|
||||
# Декодируем имя файла
|
||||
filename = self._decode_header(filename)
|
||||
# Получаем содержимое
|
||||
content = part.get_payload(decode=True)
|
||||
if content:
|
||||
attachments.append(EmailAttachment(filename=filename, content=content))
|
||||
return attachments
|
||||
|
||||
def get_emails_id(self, folder: str = "INBOX", only_unseen: bool = True, mark_as_read: bool = True) -> List[
|
||||
EmailMessage]:
|
||||
"""Получить список новых электронных писем."""
|
||||
self.connect()
|
||||
|
||||
# Выбираем папку
|
||||
self.imap_conn.select(folder, readonly=False)
|
||||
|
||||
# Ищем письма
|
||||
search_criteria = "(UNSEEN)" if only_unseen else "ALL"
|
||||
status, messages = self.imap_conn.search(None, search_criteria)
|
||||
|
||||
if status != "OK":
|
||||
return []
|
||||
|
||||
email_ids = messages[0].split()
|
||||
return email_ids
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def get_emails(self, folder: str = "INBOX", only_unseen: bool = True, mark_as_read: bool = True) -> List[EmailMessage]:
|
||||
"""Получить список новых электронных писем."""
|
||||
self.connect()
|
||||
|
||||
# Выбираем папку
|
||||
self.imap_conn.select(folder, readonly=False)
|
||||
|
||||
# Ищем письма
|
||||
search_criteria = "(UNSEEN)" if only_unseen else "ALL"
|
||||
status, messages = self.imap_conn.search(None, search_criteria)
|
||||
|
||||
if status != "OK":
|
||||
return []
|
||||
|
||||
email_ids = messages[0].split()
|
||||
emails = []
|
||||
|
||||
for email_id in email_ids:
|
||||
try:
|
||||
# Получаем письмо
|
||||
status, msg_data = self.imap_conn.fetch(email_id, "(RFC822)")
|
||||
|
||||
if status != "OK":
|
||||
continue
|
||||
|
||||
# Парсим письмо
|
||||
raw_email = msg_data[0][1]
|
||||
msg = email.message_from_bytes(raw_email)
|
||||
|
||||
# Извлекаем данные
|
||||
from_addr = self._decode_header(msg.get("From", ""))
|
||||
subject = self._decode_header(msg.get("Subject", ""))
|
||||
|
||||
from_email = self.__extract_email(from_addr)
|
||||
|
||||
# Получаем дату
|
||||
date_str = msg.get("Date", "")
|
||||
try:
|
||||
date_tuple = email.utils.parsedate_tz(date_str)
|
||||
if date_tuple:
|
||||
timestamp = email.utils.mktime_tz(date_tuple)
|
||||
dt = datetime.fromtimestamp(timestamp)
|
||||
else:
|
||||
dt = datetime.now()
|
||||
except:
|
||||
dt = datetime.now()
|
||||
|
||||
# Извлекаем тело письма
|
||||
body = self._extract_body(msg)
|
||||
|
||||
#print(body)
|
||||
first_sender = self._extract_first_sender(body)
|
||||
|
||||
# Извлекаем вложения
|
||||
attachments = self._extract_attachments(msg)
|
||||
|
||||
# Создаем объект письма
|
||||
email_obj = EmailMessage(
|
||||
from_addr=from_addr,
|
||||
from_email=from_email,
|
||||
subj=subject,
|
||||
dt=dt,
|
||||
body=body,
|
||||
attachments=attachments,
|
||||
first_sender=first_sender
|
||||
)
|
||||
|
||||
emails.append(email_obj)
|
||||
|
||||
# Помечаем письмо как прочитанное
|
||||
if mark_as_read:
|
||||
self.imap_conn.store(email_id, '+FLAGS', '\\Seen')
|
||||
|
||||
except Exception as e:
|
||||
print(f"Ошибка при обработке письма {email_id}: {e}")
|
||||
continue
|
||||
|
||||
return emails
|
||||
|
||||
def send_email(self, message: EmailMessage, to_addr: str, cc: Optional[List[str]] = None, bcc: Optional[List[str]] = None):
|
||||
"""Отправить электронное письмо"""
|
||||
# Создаем multipart сообщение
|
||||
msg = MIMEMultipart()
|
||||
msg['From'] = self.email
|
||||
msg['To'] = to_addr
|
||||
msg['Subject'] = message.subj
|
||||
|
||||
if cc:
|
||||
msg['Cc'] = ', '.join(cc)
|
||||
|
||||
# Добавляем тело письма
|
||||
msg.attach(MIMEText(message.body, 'plain', 'utf-8'))
|
||||
|
||||
# Добавляем вложения
|
||||
for attachment in message.attachments:
|
||||
part = MIMEBase('application', 'octet-stream')
|
||||
part.set_payload(attachment.content)
|
||||
encoders.encode_base64(part)
|
||||
part.add_header(
|
||||
'Content-Disposition',
|
||||
f'attachment; filename= {attachment.filename}'
|
||||
)
|
||||
msg.attach(part)
|
||||
|
||||
# Формируем список всех получателей
|
||||
recipients = [to_addr]
|
||||
if cc:
|
||||
recipients.extend(cc)
|
||||
if bcc:
|
||||
recipients.extend(bcc)
|
||||
|
||||
# Отправляем письмо
|
||||
with smtplib.SMTP(self.smtp_host, self.smtp_port) as server:
|
||||
server.starttls()
|
||||
server.login(self.email, self.password)
|
||||
server.sendmail(self.email, recipients, msg.as_string())
|
||||
|
||||
def __enter__(self):
|
||||
"""Поддержка контекстного менеджера"""
|
||||
self.connect()
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
"""Поддержка контекстного менеджера"""
|
||||
self.disconnect()
|
||||
|
||||
|
||||
106
src/mail_order_bot/email_client/utils.py
Normal file
106
src/mail_order_bot/email_client/utils.py
Normal file
@@ -0,0 +1,106 @@
|
||||
from email.header import decode_header, make_header
|
||||
import re
|
||||
from datetime import datetime
|
||||
from typing import List, Optional
|
||||
from dataclasses import dataclass
|
||||
import email
|
||||
from email import encoders
|
||||
from email.mime.text import MIMEText
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.base import MIMEBase
|
||||
from email.header import decode_header
|
||||
import imaplib
|
||||
import smtplib
|
||||
|
||||
from .objects import EmailMessage, EmailAttachment
|
||||
|
||||
|
||||
class EmailUtils:
|
||||
@staticmethod
|
||||
def extract_header(msg, header_name) -> str:
|
||||
"""Декодировать заголовок письма."""
|
||||
header = msg.get(header_name, "")
|
||||
if header is None:
|
||||
return ""
|
||||
decoded = decode_header(header)
|
||||
return str(make_header(decoded))
|
||||
|
||||
@staticmethod
|
||||
def extract_email(text) -> str:
|
||||
match = re.search(r'<([^<>]+)>', text)
|
||||
if match:
|
||||
return match.group(1)
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def extract_body(msg: email.message.Message) -> str:
|
||||
"""Извлечь текст письма из любого типа содержимого, кроме вложений"""
|
||||
body = ""
|
||||
if msg.is_multipart():
|
||||
for part in msg.walk():
|
||||
content_disposition = str(part.get("Content-Disposition", ""))
|
||||
# Пропускаем вложения
|
||||
if "attachment" in content_disposition.lower():
|
||||
continue
|
||||
try:
|
||||
charset = part.get_content_charset() or 'utf-8'
|
||||
payload = part.get_payload(decode=True)
|
||||
if payload:
|
||||
body_piece = payload.decode(charset, errors='ignore')
|
||||
body += body_piece
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
charset = msg.get_content_charset() or 'utf-8'
|
||||
payload = msg.get_payload(decode=True)
|
||||
if payload:
|
||||
body = payload.decode(charset, errors='ignore')
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return body
|
||||
|
||||
@staticmethod
|
||||
def extract_first_sender(body: str):
|
||||
"""Извлекает адреса отправителей из пересылаемого сообщения. Нужно для отладки"""
|
||||
# Ищем email внутри скобок после строки "Пересылаемое сообщение"
|
||||
pattern = r"Пересылаемое сообщение.*?\((.*?)\)"
|
||||
match = re.search(pattern, body, re.DOTALL)
|
||||
if match:
|
||||
return match.group(1)
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def extract_attachments(msg: email.message.Message) -> List[EmailAttachment]:
|
||||
"""Извлечь вложения из письма."""
|
||||
attachments = []
|
||||
|
||||
for part in msg.walk():
|
||||
content_disposition = str(part.get("Content-Disposition", ""))
|
||||
|
||||
if "attachment" in content_disposition:
|
||||
filename = part.get_filename()
|
||||
if filename:
|
||||
# Декодируем имя файла
|
||||
filename = decode_header(filename)[0]
|
||||
# Получаем содержимое
|
||||
content = part.get_payload(decode=True)
|
||||
if content:
|
||||
#attachments.append(EmailAttachment(filename=filename, content=content))
|
||||
attachments.append({"name": filename, "bytes": content})
|
||||
|
||||
return attachments
|
||||
|
||||
@staticmethod
|
||||
def extract_domain(email_message: str) -> str | None:
|
||||
"""Вернуть домен из email либо None, если формат странный."""
|
||||
if "@" not in email_message:
|
||||
return None
|
||||
# убираем пробелы по краям и берём часть после '@'
|
||||
return email_message.strip().split("@", 1)[1]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
from .email_processor import EmailProcessor
|
||||
@@ -1,31 +0,0 @@
|
||||
import threading
|
||||
from typing import Any, Dict
|
||||
|
||||
class _SingletonMeta(type):
|
||||
_instances = {}
|
||||
_lock = threading.Lock()
|
||||
|
||||
def __call__(cls, *args, **kwargs):
|
||||
if cls not in cls._instances:
|
||||
with cls._lock:
|
||||
if cls not in cls._instances:
|
||||
instance = super().__call__(*args, **kwargs)
|
||||
cls._instances[cls] = instance
|
||||
return cls._instances[cls]
|
||||
|
||||
class Context(metaclass=_SingletonMeta):
|
||||
def __init__(self):
|
||||
# будет вызван только при первом создании
|
||||
self.context = {}
|
||||
|
||||
def clear_context(self):
|
||||
"""Очищает self.context, устанавливая его в None или пустой словарь"""
|
||||
with self._lock: # потокобезопасная очистка
|
||||
self.context = {}
|
||||
print("Context очищен") # опциональный лог
|
||||
|
||||
def set_context(self, new_context: Dict[str, Any]):
|
||||
"""Устанавливает новый контекст (бонусный метод)"""
|
||||
with self._lock:
|
||||
self.context = new_context
|
||||
print("Новый контекст установлен")
|
||||
@@ -1,47 +0,0 @@
|
||||
import os
|
||||
import yaml
|
||||
import logging
|
||||
from typing import Dict, Any
|
||||
from pathlib import Path
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
from .order.auto_part_order import AutoPartOrder
|
||||
from .context import Context
|
||||
from enum import Enum
|
||||
|
||||
from .handlers import *
|
||||
|
||||
|
||||
class RequestStatus(Enum):
|
||||
NEW = "new"
|
||||
IN_PROGRESS = "in progress"
|
||||
FAILED = "failed"
|
||||
EXECUTED = "executed"
|
||||
OPERATOR_REQUIRED = "operator required"
|
||||
INVALID = "invalid"
|
||||
|
||||
|
||||
class EmailProcessor(Context):
|
||||
def __init__(self, configs_path: Path):
|
||||
super().__init__()
|
||||
self.configs_path = configs_path
|
||||
self.status = RequestStatus.NEW
|
||||
|
||||
|
||||
def process(self, email_id):
|
||||
config = self._load_config(client)
|
||||
|
||||
for stage in config["pipeline"]:
|
||||
handler_name = stage["handler"]
|
||||
logger.info(f"Processing handler: {handler_name}")
|
||||
task = globals()[handler_name](stage.get("config", None), self.context)
|
||||
task.do()
|
||||
|
||||
def _load_config(self, client) -> Dict[str, Any]:
|
||||
"""Загружает конфигурацию из YAML или JSON"""
|
||||
path = os.path.join(self.configs_path, client + '.yml')
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
return yaml.safe_load(f)
|
||||
|
||||
def _load_email(self):
|
||||
@@ -1,9 +0,0 @@
|
||||
from .abcp_clients.check_stock import GetStock
|
||||
from .abcp_clients.create_order import InstantOrderTest
|
||||
|
||||
from .excel_parcers.order_parcer_basic import BasicExcelParser
|
||||
|
||||
from .notifications.test_notifier import TestNotifier
|
||||
|
||||
|
||||
from .validators.price_quantity_ckecker import CheckOrder
|
||||
@@ -1,27 +0,0 @@
|
||||
import random
|
||||
import logging
|
||||
|
||||
from mail_order_bot.email_handler.handlers.abstract_task import AbstractTask
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_stock(brand, part_number):
|
||||
return random.randint(0, 10)
|
||||
|
||||
class GetStock(AbstractTask):
|
||||
|
||||
def do(self) -> None:
|
||||
positions = self.order.positions
|
||||
for position in positions:
|
||||
self._update_stock(position)
|
||||
|
||||
def _update_stock(self, position):
|
||||
# Эмулируем получение данных
|
||||
max_stock = self.config.get('max_stock',10)
|
||||
stock = random.randint(0, max_stock)
|
||||
price = position.requested_price
|
||||
|
||||
position.stock_price = price
|
||||
position.stock_quantity = stock
|
||||
@@ -1,50 +0,0 @@
|
||||
import random
|
||||
import logging
|
||||
from mail_order_bot.email_handler.handlers.abstract_task import AbstractTask
|
||||
from mail_order_bot.email_handler.order.auto_part_order import OrderStatus
|
||||
from decimal import Decimal
|
||||
import random
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class CheckOrder(AbstractTask):
|
||||
|
||||
def do(self) -> None:
|
||||
refused = 0
|
||||
positions = self.order.positions
|
||||
for position in positions:
|
||||
self._set_order_price(position)
|
||||
self._set_order_quantity(position)
|
||||
|
||||
if position.order_price == 0 or position.order_quantity == 0:
|
||||
refused += 1
|
||||
|
||||
self._check_refusal_threshold(refused)
|
||||
|
||||
|
||||
def _set_order_price(self, position):
|
||||
# Эмулируем получение данных
|
||||
acceptable_price_reduction = self.config.get("acceptable_price_reduction")
|
||||
acceptable_price = position.stock_price* Decimal(str((1-acceptable_price_reduction/100)))
|
||||
|
||||
if position.requested_price < acceptable_price:
|
||||
position.order_price = 0
|
||||
else:
|
||||
position.order_price = position.requested_price
|
||||
|
||||
def _set_order_quantity(self, position):
|
||||
max_stock = self.config.get("max_stock", 100)
|
||||
min_stock = self.config.get("min_stock", 0)
|
||||
|
||||
stock_quantity = random.randint(min_stock, max_stock)
|
||||
|
||||
position.order_quantity = max(0, min(position.stock_quantity, stock_quantity))
|
||||
|
||||
def _check_refusal_threshold(self, refused):
|
||||
refusal_threshold_limit = self.config.get("refusal_threshold", 1)
|
||||
refusal_level = refused/len(self.order.positions)
|
||||
|
||||
if refusal_level > refusal_threshold_limit:
|
||||
self.order.status = OrderStatus.OPERATOR_REQUIRED
|
||||
self.order.reason = "Превышен порог отказов"
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
from typing import List, Optional
|
||||
from .auto_part_position import AutoPartPosition
|
||||
from enum import Enum
|
||||
|
||||
class OrderStatus(Enum):
|
||||
NEW = "new"
|
||||
IN_PROGRESS = "in progress"
|
||||
FAILED = "failed"
|
||||
COMPLETED = "completed"
|
||||
OPERATOR_REQUIRED = "operator required"
|
||||
INVALID = "invalid"
|
||||
|
||||
|
||||
class AutoPartOrder:
|
||||
def __init__(self):
|
||||
self.positions: List[AutoPartPosition] = []
|
||||
self.status = OrderStatus.NEW
|
||||
self.reason = ""
|
||||
|
||||
def add_position(self, position: AutoPartPosition) -> None:
|
||||
self.positions.append(position)
|
||||
if self.status == OrderStatus.NEW:
|
||||
self.status = OrderStatus.IN_PROGRESS
|
||||
|
||||
def find_positions(self, brand: Optional[str] = None, sku: Optional[str] = None) -> List[AutoPartPosition]:
|
||||
results = self.positions
|
||||
if brand is not None:
|
||||
results = [p for p in results if p.manufacturer == brand]
|
||||
if sku is not None:
|
||||
results = [p for p in results if p.sku == sku]
|
||||
return results
|
||||
|
||||
def __len__(self):
|
||||
return len(self.positions)
|
||||
@@ -1,77 +0,0 @@
|
||||
from typing import List, Optional
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Dict, Any
|
||||
from decimal import Decimal
|
||||
|
||||
|
||||
@dataclass
|
||||
class AutoPartPosition:
|
||||
"""
|
||||
Унифицированная модель позиции для заказа.
|
||||
Все контрагенты приводятся к этой структуре.
|
||||
"""
|
||||
sku: str # Артикул товара
|
||||
manufacturer: str # Производитель
|
||||
requested_quantity: int # Количество
|
||||
total: Decimal = 0 # Общая сумма
|
||||
name: str = "" # Наименование
|
||||
requested_price: Decimal = 0 # Цена за единицу
|
||||
order_quantity: int = 0 # Количество для заказа
|
||||
order_price: Decimal = Decimal('0.0') # Цена в заказе
|
||||
stock: List[Dict[str, Any]] = None
|
||||
additional_attrs: Dict[str, Any] = field(default_factory=dict)
|
||||
|
||||
def __post_init__(self):
|
||||
"""Валидация после инициализации"""
|
||||
if self.requested_quantity < 0:
|
||||
raise ValueError(f"Количество не может быть отрицательным: {self.requested_quantity}")
|
||||
if self.requested_price < 0:
|
||||
raise ValueError(f"Цена не может быть отрицательной: {self.requested_price}")
|
||||
|
||||
|
||||
|
||||
|
||||
class AutoPartPosition2:
|
||||
brand: str
|
||||
sku: str
|
||||
name: str
|
||||
customer_price: float
|
||||
customer_quantity: int
|
||||
supplier_price: float
|
||||
stock_remaining: int
|
||||
|
||||
def __init__(self, brand: str, sku: str, name: str,
|
||||
customer_price: float, customer_quantity: int,
|
||||
supplier_price: float, stock_remaining: int):
|
||||
self.brand = brand
|
||||
self.sku = sku
|
||||
self.name = name
|
||||
self.customer_price = customer_price
|
||||
self.customer_quantity = customer_quantity
|
||||
self.supplier_price = supplier_price
|
||||
self.stock_remaining = stock_remaining
|
||||
|
||||
def customer_cost(self) -> float:
|
||||
return self.customer_price * self.customer_quantity
|
||||
|
||||
def supplier_cost(self) -> float:
|
||||
return self.supplier_price * self.customer_quantity
|
||||
|
||||
def is_available(self) -> bool:
|
||||
return self.stock_remaining >= self.customer_quantity
|
||||
|
||||
def restock(self, amount: int) -> None:
|
||||
if amount < 0:
|
||||
raise ValueError("Restock amount must be non-negative")
|
||||
self.stock_remaining += amount
|
||||
|
||||
def __post_init__(self):
|
||||
if self.customer_price < 0:
|
||||
raise ValueError("Customer price cannot be negative")
|
||||
if self.customer_quantity < 0:
|
||||
raise ValueError("Customer quantity cannot be negative")
|
||||
if self.supplier_price < 0:
|
||||
raise ValueError("Supplier price cannot be negative")
|
||||
if self.stock_remaining < 0:
|
||||
raise ValueError("Stock remaining cannot be negative")
|
||||
|
||||
1
src/mail_order_bot/email_processor/__init__.py
Normal file
1
src/mail_order_bot/email_processor/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .processor import EmailProcessor
|
||||
16
src/mail_order_bot/email_processor/handlers/__init__.py
Normal file
16
src/mail_order_bot/email_processor/handlers/__init__.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from .attachment_handler.attachment_handler import AttachmentHandler
|
||||
from .excel_parcers.order_parcer_basic import BasicExcelParser
|
||||
from .destination_time.local_store import DeliveryPeriodLocalStore
|
||||
|
||||
|
||||
|
||||
from .abcp.api_get_stock import GetStock
|
||||
from .abcp.api_create_order import InstantOrderTest
|
||||
|
||||
|
||||
|
||||
from .notifications.test_notifier import TestNotifier
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import logging
|
||||
import requests
|
||||
from mail_order_bot.email_handler.handlers.abstract_task import AbstractTask
|
||||
from mail_order_bot.email_handler.order.auto_part_order import OrderStatus
|
||||
from mail_order_bot.email_processor.handlers.abstract_task import AbstractTask
|
||||
from mail_order_bot.email_processor.order.auto_part_order import OrderStatus
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
import random
|
||||
import logging
|
||||
|
||||
from mail_order_bot.email_processor.handlers.abstract_task import AbstractTask
|
||||
from mail_order_bot.abcp_api.abcp_provider import AbcpProvider
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class APIGetStock(AbstractTask):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.abcp_provider = AbcpProvider()
|
||||
|
||||
def do(self) -> None:
|
||||
attachments = self.context.data.get("attachments", [])
|
||||
for attachment in attachments:
|
||||
order = attachment["order"]
|
||||
for position in order.positions:
|
||||
stock = self.get_stock(position.sku, position.manufacturer)
|
||||
position.update_stock(stock, order.delivery_period)
|
||||
position.fill_from_stock()
|
||||
logger.info(f"Получены позиции со склада для файла {attachment.get('name', "no name")}")
|
||||
|
||||
def get_stock(self, sku: str, manufacturer: str) -> int:
|
||||
return self.abcp_provider.get_stock(sku, manufacturer)
|
||||
@@ -1,16 +1,16 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Dict, Any
|
||||
|
||||
from mail_order_bot.email_handler.context import Context
|
||||
from mail_order_bot.context import Context
|
||||
|
||||
|
||||
class AbstractTask(ABC, Context):
|
||||
class AbstractTask():
|
||||
RESULT_SECTION = "section"
|
||||
"""
|
||||
Абстрактный базовый класс для всех хэндлеров.
|
||||
"""
|
||||
def __init__(self, config: Dict[str, Any]) -> None:
|
||||
Context.__init__(self, {})
|
||||
def __init__(self, config: Dict[str, Any]={}) -> None:
|
||||
self.context = Context()
|
||||
self.config = config
|
||||
|
||||
@abstractmethod
|
||||
@@ -0,0 +1,21 @@
|
||||
from mail_order_bot.email_processor.handlers.abstract_task import AbstractTask
|
||||
from mail_order_bot.email_client.utils import EmailUtils
|
||||
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class AttachmentHandler(AbstractTask):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def do(self) -> None:
|
||||
email = self.context.data["email"]
|
||||
attachments = EmailUtils.extract_attachments(email)
|
||||
self.context.data["attachments"] = attachments
|
||||
logger.debug(f"AttachmentHandler отработал, извлек вложений: {len(attachments)} ")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
from mail_order_bot.email_processor.handlers.abstract_task import AbstractTask
|
||||
from mail_order_bot.email_client.utils import EmailUtils
|
||||
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class DeliveryPeriodLocalStore(AbstractTask):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def do(self) -> None:
|
||||
attachments = self.context.data["attachments"]
|
||||
for attachment in attachments:
|
||||
order = attachment["order"]
|
||||
order.set_delivery_period(0)
|
||||
logger.info(f"Доставка только с локального склада, срок 1 день.")
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -3,50 +3,50 @@ import pandas as pd
|
||||
from typing import Dict, Any, Optional
|
||||
from decimal import Decimal
|
||||
from io import BytesIO
|
||||
#from mail_order_bot.email_handler.handlers.order_position import OrderPosition
|
||||
from mail_order_bot.email_handler.handlers.abstract_task import AbstractTask
|
||||
#from mail_order_bot.email_processor.handlers.order_position import OrderPosition
|
||||
from mail_order_bot.email_processor.handlers.abstract_task import AbstractTask
|
||||
|
||||
from ...order.auto_part_position import AutoPartPosition
|
||||
from ...order.auto_part_order import AutoPartOrder
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class BasicExcelParser(AbstractTask):
|
||||
RESULT_SECTION = "positions"
|
||||
"""
|
||||
Универсальный парсер, настраиваемый через конфигурацию.
|
||||
Подходит для большинства стандартных случаев.
|
||||
"""
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def do(self) -> None:
|
||||
|
||||
# todo сделать проверку на наличие файла и его тип
|
||||
file_bytes = BytesIO(self.context.get("attachment").content) # self.context.get("attachment") #
|
||||
try:
|
||||
df = self._make_dataframe(file_bytes)
|
||||
# Получаем маппинг колонок из конфигурации
|
||||
mapping = self.config['mapping']
|
||||
|
||||
# Парсим строки
|
||||
positions = []
|
||||
for idx, row in df.iterrows():
|
||||
try:
|
||||
attachments = self.context.data.get("attachments", [])
|
||||
for attachment in attachments:
|
||||
file_bytes = BytesIO(attachment['bytes']) # self.context.get("attachment") #
|
||||
try:
|
||||
df = self._make_dataframe(file_bytes)
|
||||
mapping = self.config['mapping']
|
||||
order = AutoPartOrder()
|
||||
|
||||
# Парсим строки
|
||||
positions = []
|
||||
for idx, row in df.iterrows():
|
||||
position = self._parse_row(row, mapping)
|
||||
if position:
|
||||
positions.append(position)
|
||||
self.order.add_position(position)
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка парсинга строки {idx}: {e}, {row}")
|
||||
continue
|
||||
order.add_position(position)
|
||||
|
||||
logger.info(f"Успешно обработано {len(positions)} позиций из {len(df)} строк")
|
||||
logger.info(f"Успешно обработано {len(order)} позиций из {len(df)} строк")
|
||||
|
||||
self.context[self.RESULT_SECTION] = positions
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка при обработке файла: {e}")
|
||||
else:
|
||||
attachment["order"] = order
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка при обработке файла: {e}")
|
||||
raise Exception from e
|
||||
|
||||
def _parse_row(self, row: pd.Series, mapping: Dict[str, str]) -> Optional[AutoPartPosition]:
|
||||
"""Парсит одну строку Excel в OrderPosition"""
|
||||
@@ -3,8 +3,8 @@ import pandas as pd
|
||||
from typing import Dict, Any, Optional
|
||||
from decimal import Decimal
|
||||
from io import BytesIO
|
||||
#from mail_order_bot.email_handler.handlers.order_position import OrderPosition
|
||||
from mail_order_bot.email_handler.handlers.abstract_task import AbstractTask
|
||||
#from mail_order_bot.email_processor.handlers.order_position import OrderPosition
|
||||
from mail_order_bot.email_processor.handlers.abstract_task import AbstractTask
|
||||
|
||||
from ...order.auto_part_position import AutoPartPosition
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import random
|
||||
import logging
|
||||
from mail_order_bot.email_processor.handlers.abstract_task import AbstractTask
|
||||
from mail_order_bot.email_processor.order.auto_part_order import OrderStatus
|
||||
from mail_order_bot.email_processor.order.auto_part_position import AutoPartPosition, PositionStatus
|
||||
from decimal import Decimal
|
||||
import random
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class LocalStoreOrder(AbstractTask):
|
||||
"""Сейчас логика такая
|
||||
- ищем на складе наш сапплиер код, берем самую дешевую позицию и делаем заказ из нее
|
||||
|
||||
Другие чуть более дорогие не рассматриваем
|
||||
|
||||
"""
|
||||
# это код нашего склада
|
||||
|
||||
def do(self) -> None:
|
||||
attachments = self.context.data["attachments"]
|
||||
for attachment in attachments:
|
||||
order = attachment["order"]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import logging
|
||||
|
||||
from mail_order_bot.email_handler.handlers.abstract_task import AbstractTask
|
||||
from mail_order_bot.email_processor.handlers.abstract_task import AbstractTask
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
2
src/mail_order_bot/email_processor/order/__init__.py
Normal file
2
src/mail_order_bot/email_processor/order/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from .auto_part_order import AutoPartOrder, OrderStatus
|
||||
from .auto_part_position import AutoPartPosition, PositionStatus
|
||||
59
src/mail_order_bot/email_processor/order/auto_part_order.py
Normal file
59
src/mail_order_bot/email_processor/order/auto_part_order.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from typing import List, Optional
|
||||
from .auto_part_position import AutoPartPosition, PositionStatus
|
||||
|
||||
from enum import Enum
|
||||
|
||||
class OrderStatus(Enum):
|
||||
NEW = "new"
|
||||
IN_PROGRESS = "in progress"
|
||||
FAILED = "failed"
|
||||
COMPLETED = "completed"
|
||||
OPERATOR_REQUIRED = "operator required"
|
||||
INVALID = "invalid"
|
||||
|
||||
|
||||
class AutoPartOrder:
|
||||
def __init__(self):
|
||||
self.positions: List[AutoPartPosition] = []
|
||||
self.status = OrderStatus.NEW
|
||||
self.delivery_period = 0
|
||||
self.reason = ""
|
||||
self.errors = []
|
||||
|
||||
def add_position(self, position: AutoPartPosition) -> None:
|
||||
self.positions.append(position)
|
||||
|
||||
def find_positions(self, brand: Optional[str] = None, sku: Optional[str] = None) -> List[AutoPartPosition]:
|
||||
results = self.positions
|
||||
if brand is not None:
|
||||
results = [p for p in results if p.manufacturer == brand]
|
||||
if sku is not None:
|
||||
results = [p for p in results if p.sku == sku]
|
||||
return results
|
||||
|
||||
def set_delivery_period(self, delivery_period: int) -> None:
|
||||
self.delivery_period = delivery_period
|
||||
|
||||
def fill_from_local_supplier(self) -> None:
|
||||
for position in self.positions:
|
||||
errors = position.fill_from_stock()
|
||||
self.errors += errors
|
||||
|
||||
|
||||
|
||||
def check_order(self, config) -> None:
|
||||
""" Проверяет заказ на возможность исполнения"""
|
||||
# 1. Проверка общего количества отказов
|
||||
order_refusal_threshold = config.get("order_refusal_threshold", 1)
|
||||
refusal_positions_count = len([position for position in self.positions if str(position.status) in
|
||||
[PositionStatus.REFUSED, PositionStatus.STOCK_FAILED]])
|
||||
|
||||
order_refusal_rate = refusal_positions_count / len(self.positions)
|
||||
if order_refusal_rate > order_refusal_threshold:
|
||||
self.errors.append(f"Превышен порог отказов в заказе - {order_refusal_rate:.0%} "
|
||||
f"({refusal_positions_count} из {len(self.positions)})")
|
||||
self.status = OrderStatus.OPERATOR_REQUIRED
|
||||
|
||||
|
||||
def __len__(self):
|
||||
return len(self.positions)
|
||||
118
src/mail_order_bot/email_processor/order/auto_part_position.py
Normal file
118
src/mail_order_bot/email_processor/order/auto_part_position.py
Normal file
@@ -0,0 +1,118 @@
|
||||
from typing import List, Optional
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Dict, Any
|
||||
from decimal import Decimal
|
||||
from enum import Enum
|
||||
|
||||
class PositionStatus(Enum):
|
||||
NEW = "new" # Новая позиция
|
||||
STOCK_RECIEVED = "stock_received" # Получен остаток
|
||||
STOCK_FAILED = "stock_failed" # Остаток не получен
|
||||
NO_AVAILABLE_STOCK = "no_available_stock" #Нет доступных складов
|
||||
READY = "ready"
|
||||
READY_PARTIAL = "ready_partial"
|
||||
ORDERED = "ordered" # Заказано
|
||||
REFUSED = "refused" # Отказано
|
||||
|
||||
|
||||
@dataclass
|
||||
class AutoPartPosition:
|
||||
DISTRIBUTOR_ID = "1577730"
|
||||
"""
|
||||
Унифицированная модель позиции для заказа.
|
||||
Все контрагенты приводятся к этой структуре.
|
||||
"""
|
||||
sku: str # Артикул товара
|
||||
manufacturer: str # Производитель
|
||||
|
||||
requested_price: Decimal # Цена за единицу
|
||||
requested_quantity: int # Количество
|
||||
|
||||
total: Decimal = 0 # Общая сумма
|
||||
name: str = "" # Наименование
|
||||
|
||||
order_quantity: int = 0 # Количество для заказа
|
||||
order_price: Decimal = Decimal('0.0') # Цена в заказе
|
||||
order_item: Dict[str, Any] = field(default_factory=dict)
|
||||
|
||||
stock: List[Dict[str, Any]] = None
|
||||
additional_attrs: Dict[str, Any] = field(default_factory=dict)
|
||||
|
||||
status: PositionStatus = PositionStatus.NEW
|
||||
desc: str = ""
|
||||
|
||||
def __post_init__(self):
|
||||
"""Валидация после инициализации"""
|
||||
if self.requested_quantity < 0:
|
||||
raise ValueError(f"Количество не может быть отрицательным: {self.requested_quantity}")
|
||||
if self.requested_price < 0:
|
||||
raise ValueError(f"Цена не может быть отрицательной: {self.requested_price}")
|
||||
|
||||
def update_stock(self, stock: Dict[str, Any], delivery_period: int = 0) -> None:
|
||||
if stock["success"]:
|
||||
available_distributors = stock["data"]
|
||||
|
||||
# Для доставки только с локального склада сперва убираем все остальные склады
|
||||
if delivery_period == 0:
|
||||
available_distributors = self._filter_only_local_storage(available_distributors)
|
||||
|
||||
#Отбираем склады по сроку доставки
|
||||
available_distributors = self._filter_proper_delivery_time(available_distributors, delivery_period)
|
||||
|
||||
# Убираем дорогие склады с ценой выше запрошенной
|
||||
available_distributors = self._filter_proper_price(available_distributors)
|
||||
|
||||
# Убираем отрицательные остатки
|
||||
available_distributors = self._filter_proper_availability(available_distributors)
|
||||
|
||||
# Сортируем по цене
|
||||
available_distributors.sort(key=lambda item: Decimal(item["price"]), reverse=False)
|
||||
|
||||
self.stock = available_distributors
|
||||
if len (self.stock):
|
||||
self.status = PositionStatus.STOCK_RECIEVED
|
||||
else:
|
||||
self.status = PositionStatus.NO_AVAILABLE_STOCK
|
||||
else:
|
||||
self.status = PositionStatus.STOCK_FAILED
|
||||
|
||||
|
||||
def fill_from_stock(self):
|
||||
if self.status == PositionStatus.STOCK_RECIEVED:
|
||||
|
||||
for distributor in self.stock:
|
||||
distributor["profit"] = int(distributor["availability"]) * self.requested_price - int(distributor["availability"]) * Decimal(distributor["price"])
|
||||
|
||||
self.stock.sort(key=lambda item: item["profit"], reverse=True)
|
||||
|
||||
self.order_quantity = self.stock[0]["availability"]
|
||||
self.order_price = self.requested_price
|
||||
self.order_item = self.stock[0]
|
||||
|
||||
self.status = PositionStatus.READY
|
||||
|
||||
|
||||
def _filter_only_local_storage(self, distributors):
|
||||
return [item for item in distributors if str(item["distributorId"]) == self.DISTRIBUTOR_ID]
|
||||
|
||||
def _filter_proper_delivery_time(self, distributors, delivery_period):
|
||||
return [item for item in distributors if item["deliveryPeriod"] <= delivery_period]
|
||||
|
||||
def _filter_proper_price(self, distributors):
|
||||
return [item for item in distributors if Decimal(item["price"]) <= self.requested_price]
|
||||
|
||||
def _filter_proper_availability(self, distributors):
|
||||
return [item for item in distributors if Decimal(item["availability"]) > 0]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
77
src/mail_order_bot/email_processor/processor.py
Normal file
77
src/mail_order_bot/email_processor/processor.py
Normal file
@@ -0,0 +1,77 @@
|
||||
import os
|
||||
import yaml
|
||||
import logging
|
||||
from typing import Dict, Any
|
||||
from pathlib import Path
|
||||
import threading
|
||||
from mail_order_bot.context import Context
|
||||
from mail_order_bot.email_client.utils import EmailUtils
|
||||
from enum import Enum
|
||||
|
||||
from mail_order_bot.email_processor.handlers import *
|
||||
|
||||
from mail_order_bot.email_processor.handlers import AttachmentHandler
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class RequestStatus(Enum):
|
||||
NEW = "new"
|
||||
IN_PROGRESS = "in progress"
|
||||
FAILED = "failed"
|
||||
EXECUTED = "executed"
|
||||
OPERATOR_REQUIRED = "operator required"
|
||||
INVALID = "invalid"
|
||||
|
||||
|
||||
class EmailProcessor:
|
||||
def __init__(self, configs_path: str):
|
||||
super().__init__()
|
||||
self.context = Context()
|
||||
self.configs_path = configs_path
|
||||
self.status = RequestStatus.NEW
|
||||
|
||||
def process_email(self, email):
|
||||
# Очистить контекст
|
||||
self.context.clear()
|
||||
|
||||
# Сохранить письмо в контекст
|
||||
self.context.data["email"] = email
|
||||
|
||||
# Определить клиента
|
||||
email_body = EmailUtils.extract_body(email)
|
||||
email_from = EmailUtils.extract_first_sender(email_body)
|
||||
client = EmailUtils.extract_domain(email_from)
|
||||
|
||||
try:
|
||||
# Определить конфиг для пайплайна
|
||||
config = self._load_config(client)
|
||||
self.context.data["config"] = config
|
||||
|
||||
# Обработка вложений
|
||||
attachments_handler_task = AttachmentHandler()
|
||||
attachments_handler_task.do()
|
||||
|
||||
# Запустить обработку пайплайна
|
||||
for stage in config["pipeline"]:
|
||||
handler_name = stage["handler"]
|
||||
logger.info(f"Processing handler: {handler_name}")
|
||||
task = globals()[handler_name](stage.get("config", None))
|
||||
task.do()
|
||||
|
||||
|
||||
except FileNotFoundError:
|
||||
logger.error(f"Конфиг для клиента {client} не найден")
|
||||
|
||||
for attachment in self.context.data["attachments"]:
|
||||
print(attachment["order"].__dict__)
|
||||
#except Exception as e:
|
||||
# logger.error(f"Произошла другая ошибка: {e}")
|
||||
|
||||
|
||||
def _load_config(self, client) -> Dict[str, Any]:
|
||||
"""Загружает конфигурацию из YAML или JSON"""
|
||||
|
||||
path = os.path.join(self.configs_path, client + '.yml')
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
return yaml.safe_load(f)
|
||||
@@ -1,4 +1,4 @@
|
||||
|
||||
import threading
|
||||
from config_manager import ConfigManager
|
||||
from dotenv import load_dotenv
|
||||
import asyncio
|
||||
@@ -7,39 +7,51 @@ import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from email_client import EmailClient
|
||||
from excel_proceccor import ExcelProcessor
|
||||
from email_processor import EmailProcessor
|
||||
|
||||
from mail_order_bot.context import Context
|
||||
|
||||
|
||||
logger = logging.getLogger()
|
||||
|
||||
|
||||
|
||||
class MailOrderBot(ConfigManager):
|
||||
def __init__(self, *agrs, **kwargs):
|
||||
super().__init__(*agrs, **kwargs)
|
||||
|
||||
# Объявить почтового клиента
|
||||
self.email_client = EmailClient(
|
||||
imap_host=os.getenv('IMAP_HOST'),
|
||||
smtp_host=os.getenv('SMTP_HOST'),
|
||||
email=os.getenv('EMAIL_USER'),
|
||||
password=os.getenv('EMAIL_PASSWORD'),
|
||||
imap_port=os.getenv('IMAP_PORT'),
|
||||
smtp_port=os.getenv('SMTP_PORT')
|
||||
imap_port=int(os.getenv('IMAP_PORT', default="993")),
|
||||
smtp_port=int(os.getenv('SMTP_PORT', default="587")),
|
||||
)
|
||||
|
||||
# Сохранить почтовый клиент в контекст
|
||||
self.context = Context()
|
||||
self.context.email_client = self.email_client
|
||||
|
||||
# Обработчик писем
|
||||
self.email_processor = EmailProcessor("./configs")
|
||||
logger.warning("MailOrderBot инициализирован")
|
||||
|
||||
|
||||
def execute(self):
|
||||
logger.debug(f"Check emails for new orders")
|
||||
emails = self.email_client.get_emails(folder="spareparts", only_unseen=True, mark_as_read=True)
|
||||
# Получить список айдишников письма
|
||||
unread_email_ids = self.email_client.get_emails_id(folder="spareparts")
|
||||
|
||||
logger.info(f"Новых писем - {len(unread_email_ids)}")
|
||||
|
||||
|
||||
for email in emails:
|
||||
logger.info(email.subj)
|
||||
logger.info(email.from_addr)
|
||||
logger.info(email.dt)
|
||||
logger.info(email.body)
|
||||
logger.info(email.first_sender)
|
||||
logger.info('--------------------------------')
|
||||
logger.critical("mail checked")
|
||||
# Обработать каждое письмо по идентификатору
|
||||
for email_id in unread_email_ids:
|
||||
logger.debug(f"==================================================")
|
||||
logger.debug(f"Обработка письма с идентификатором {email_id}")
|
||||
# Получить письмо по идентификатору и запустить его обработку
|
||||
email = self.email_client.get_email(email_id)
|
||||
self.email_processor.process_email(email)
|
||||
pass
|
||||
|
||||
|
||||
logger = logging.getLogger()
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from mail_order_bot.abcp_api.abcp_provider import AbcpProvider
|
||||
from mail_order_bot.email_handler.order.auto_part_order import AutoPartOrder
|
||||
from mail_order_bot.email_handler.order.auto_part_position import AutoPartPosition
|
||||
from mail_order_bot.email_processor.order.auto_part_order import AutoPartOrder
|
||||
from mail_order_bot.email_processor.order.auto_part_position import AutoPartPosition
|
||||
if __name__ == "__main__":
|
||||
print(__name__)# подгружаем переменные окружения
|
||||
load_dotenv()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import os
|
||||
import chardet # pip install chardet
|
||||
import traceback
|
||||
from mail_order_bot.email_handler import EmailProcessor
|
||||
from mail_order_bot.email_processor import EmailProcessor
|
||||
import datetime
|
||||
# установим рабочую директорию
|
||||
import os
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import os
|
||||
import chardet # pip install chardet
|
||||
import traceback
|
||||
from mail_order_bot.excel_processor import ExcelProcessor
|
||||
from mail_order_bot.excel_parcer import ExcelProcessor
|
||||
|
||||
# установим рабочую директорию
|
||||
import os
|
||||
|
||||
Reference in New Issue
Block a user