Compare commits
9 Commits
5b40b4b76c
...
ha-addon
| Author | SHA1 | Date | |
|---|---|---|---|
| 1df295b2ed | |||
| 0c9055122b | |||
| ccba75097f | |||
| 0e1d13dc6e | |||
| 0419a6b374 | |||
| 6110c12624 | |||
| af7d3b92a3 | |||
| 3005992ec7 | |||
| 8818b4efc2 |
23
Dockerfile
23
Dockerfile
@@ -1,17 +1,28 @@
|
||||
FROM python:3.9-slim-bookworm
|
||||
# FROM python:3.9-slim-bookworm
|
||||
ARG BUILD_FROM
|
||||
FROM $BUILD_FROM
|
||||
|
||||
# Install cron
|
||||
RUN apt-get update && apt-get -y install cron
|
||||
# RUN apt-get update && apt-get -y install cron
|
||||
# Install python3 and cron (dcron)
|
||||
RUN apk add --no-cache \
|
||||
python3 \
|
||||
py3-pip \
|
||||
dcron
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
RUN python3 -m venv /app/venv
|
||||
ENV PATH="/app/venv/bin:$PATH"
|
||||
RUN source /app/venv/bin/activate
|
||||
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
COPY . .
|
||||
|
||||
# Make entrypoint script executable
|
||||
RUN chmod +x /app/entrypoint.sh
|
||||
# Upewnij się, że skrypt entrypoint.sh jest wykonywalny
|
||||
RUN chmod a+x /app/entrypoint.sh
|
||||
|
||||
# Set the entrypoint
|
||||
ENTRYPOINT ["/app/entrypoint.sh"]
|
||||
# Uruchom skrypt entrypoint, który zajmie się resztą
|
||||
CMD ["/app/entrypoint.sh"]
|
||||
|
||||
16
config.yaml
16
config.yaml
@@ -1,5 +1,5 @@
|
||||
name: GoogleSheet Bot
|
||||
version: "1.0.0"
|
||||
version: "1.0.8"
|
||||
slug: googlesheet_bot
|
||||
description: Bot do automatyzacji zadań w Arkuszach Google.
|
||||
arch:
|
||||
@@ -11,5 +11,15 @@ arch:
|
||||
init: false
|
||||
startup: application
|
||||
boot: auto
|
||||
options: {}
|
||||
schema: []
|
||||
options:
|
||||
credentials_json: ""
|
||||
run_hours: "9,13,16"
|
||||
debug: false
|
||||
schema:
|
||||
credentials_json: str
|
||||
run_hours: str
|
||||
debug: bool
|
||||
# environment:
|
||||
# CONFIG_CREDENTIALS_JSON: "{options.credentials_json}"
|
||||
# CONFIG_RUN_HOURS: "{options.run_hours}"
|
||||
# CONFIG_DEBUG: "{options.debug}"
|
||||
|
||||
3
crontab
3
crontab
@@ -1 +1,2 @@
|
||||
0 9,13,16 * * * cd /app && /usr/local/bin/python main.py
|
||||
0 9,13,16 * * * cd /app && /usr/local/bin/python main.py >> /proc/1/fd/1 2>/proc/1/fd/2
|
||||
* * * * * date >> /proc/1/fd/1 2>/proc/1/fd/2
|
||||
|
||||
@@ -1,11 +1,62 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/with-contenv bashio
|
||||
set -e
|
||||
|
||||
# Load the cron job
|
||||
crontab /app/crontab
|
||||
|
||||
# Create the log file and set permissions
|
||||
touch /var/log/cron.log
|
||||
chmod 0666 /var/log/cron.log
|
||||
CONFIG_PATH=/data/options.json
|
||||
|
||||
# Start cron in the foreground
|
||||
cron -f
|
||||
RUN_HOURS=$(bashio::config 'run_hours')
|
||||
DEBUG_MODE=$(bashio::config 'debug')
|
||||
CREDENTIALS_JSON=$(bashio::config 'credentials_json')
|
||||
|
||||
echo "---------------------------------"
|
||||
echo "Strefa czasowa kontenera:"
|
||||
echo "Date: $(date)"
|
||||
echo "DEBUG: RUN_HOURS='${RUN_HOURS}'"
|
||||
echo "DEBUG: DEBUG_MODE='${DEBUG_MODE}'"
|
||||
echo "---------------------------------"
|
||||
|
||||
|
||||
# Odczytaj opcję 'credentials_json' z konfiguracji dodatku i utwórz plik
|
||||
# Home Assistant udostępnia opcje jako zmienne środowiskowe z prefiksem CONFIG_
|
||||
if [ -n "${CREDENTIALS_JSON}" ]; then
|
||||
echo "${CREDENTIALS_JSON}" > /app/credentials.json
|
||||
echo "✅ Plik credentials.json został utworzony."
|
||||
else
|
||||
echo "⚠️ Brak danych w CREDENTIALS_JSON – plik credentials.json nie został utworzony!"
|
||||
fi
|
||||
|
||||
|
||||
# Odczytaj i oczyść godziny uruchomienia z konfiguracji
|
||||
RUN_HOURS_CLEAN=$(echo "${RUN_HOURS}" | tr -d '"' | tr -d "'[:space:]")
|
||||
|
||||
if [ -z "$RUN_HOURS_CLEAN" ]; then
|
||||
echo "⚠️ Brak konfiguracji godzin! Używam wartości domyślnej: 9,13,16"
|
||||
RUN_HOURS_CLEAN="9,13,16"
|
||||
fi
|
||||
|
||||
echo "Tworzę zadania crona dla godzin: ${RUN_HOURS_CLEAN}"
|
||||
|
||||
# Tworzymy tymczasowy plik z zadaniami crona
|
||||
CRONFILE=$(mktemp)
|
||||
|
||||
# Główne zadanie aplikacji
|
||||
echo "0 ${RUN_HOURS_CLEAN} * * * cd /app && /app/venv/bin/python main.py >> /proc/1/fd/1 2>/proc/1/fd/2" >> "$CRONFILE"
|
||||
|
||||
# Zadanie heartbeat tylko jeśli DEBUG=true
|
||||
if [ "${DEBUG_MODE:-false}" = "true" ]; then
|
||||
echo "* * * * * date >> /proc/1/fd/1 2>/proc/1/fd/2" >> "$CRONFILE"
|
||||
echo "✅ Dodano heartbeat do crona (DEBUG=true)"
|
||||
else
|
||||
echo "ℹ️ DEBUG jest wyłączony lub nieustawiony — heartbeat pominięty"
|
||||
fi
|
||||
|
||||
# Wczytanie zadań do crontaba
|
||||
crontab "$CRONFILE"
|
||||
rm "$CRONFILE"
|
||||
|
||||
echo "✅ Zadania crona zostały załadowane."
|
||||
|
||||
# Uruchom usługę cron na pierwszym planie, aby kontener się nie zamknął
|
||||
echo "🚀 Uruchamiam usługę cron..."
|
||||
# cron -f
|
||||
exec crond -f -l 2
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import logging
|
||||
import time
|
||||
import gspread
|
||||
from google.oauth2.service_account import Credentials
|
||||
from gspread.exceptions import APIError, WorksheetNotFound
|
||||
|
||||
SCOPES = [
|
||||
"https://www.googleapis.com/auth/spreadsheets",
|
||||
@@ -10,32 +12,69 @@ SCOPES = [
|
||||
class GSheetAPI:
|
||||
def __init__(self, credentials_file="credentials.json"):
|
||||
"""Inicjalizuje klienta API przy tworzeniu obiektu."""
|
||||
creds = Credentials.from_service_account_file(credentials_file, scopes=SCOPES)
|
||||
self.client = gspread.authorize(creds)
|
||||
def connect():
|
||||
creds = Credentials.from_service_account_file(credentials_file, scopes=SCOPES)
|
||||
return gspread.authorize(creds)
|
||||
|
||||
self.client = self._execute_with_retry(connect)
|
||||
logging.info("✅ Połączono z Google Sheets API.")
|
||||
|
||||
def _execute_with_retry(self, func, *args, **kwargs):
|
||||
"""Wykonuje funkcję z logiką ponawiania dla błędu API 503."""
|
||||
retries = 5
|
||||
delay = 60
|
||||
last_exception = None
|
||||
for i in range(retries):
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
except APIError as e:
|
||||
last_exception = e
|
||||
# Sprawdzamy, czy błąd ma status code 503 (Service Unavailable)
|
||||
if hasattr(e, 'response') and e.response.status_code == 503:
|
||||
logging.warning(
|
||||
f"Błąd API 503: Usługa niedostępna. Ponawiam za {delay}s... (Próba {i + 1}/{retries})"
|
||||
)
|
||||
time.sleep(delay)
|
||||
else:
|
||||
# Inne błędy API rzucamy od razu dalej
|
||||
raise
|
||||
except Exception as e:
|
||||
# Przechwytujemy inne potencjalne błędy sieciowe i rzucamy je dalej
|
||||
logging.error(f"Wystąpił nieoczekiwany błąd podczas komunikacji z API: {e}")
|
||||
raise
|
||||
|
||||
logging.error(f"Operacja '{func.__name__}' nie powiodła się po {retries} próbach.")
|
||||
if last_exception:
|
||||
raise last_exception
|
||||
# Fallback, gdyby pętla się zakończyła bez wyjątku
|
||||
raise Exception(f"Operacja '{func.__name__}' nie powiodła się po {retries} próbach.")
|
||||
|
||||
|
||||
def list_sheets(self, doc_name):
|
||||
"""Zwraca listę arkuszy w danym dokumencie."""
|
||||
spreadsheet = self.client.open(doc_name)
|
||||
return [ws.title for ws in spreadsheet.worksheets()]
|
||||
spreadsheet = self._execute_with_retry(self.client.open, doc_name)
|
||||
worksheets = self._execute_with_retry(spreadsheet.worksheets)
|
||||
return [ws.title for ws in worksheets]
|
||||
|
||||
def get_sheet_data(self, doc_name, sheet_name):
|
||||
"""Pobiera wszystkie dane z danego arkusza."""
|
||||
sheet = self.client.open(doc_name).worksheet(sheet_name)
|
||||
return sheet.get_all_values()
|
||||
spreadsheet = self._execute_with_retry(self.client.open, doc_name)
|
||||
sheet = self._execute_with_retry(spreadsheet.worksheet, sheet_name)
|
||||
return self._execute_with_retry(sheet.get_all_values)
|
||||
|
||||
def ensure_worksheet(self, doc_name, sheet_name):
|
||||
"""
|
||||
Zwraca worksheet o danej nazwie.
|
||||
Tworzy nowy, jeśli nie istnieje.
|
||||
"""
|
||||
spreadsheet = self.client.open(doc_name)
|
||||
spreadsheet = self._execute_with_retry(self.client.open, doc_name)
|
||||
try:
|
||||
ws = spreadsheet.worksheet(sheet_name)
|
||||
except gspread.exceptions.WorksheetNotFound:
|
||||
ws = self._execute_with_retry(spreadsheet.worksheet, sheet_name)
|
||||
except WorksheetNotFound:
|
||||
logging.info(f"➕ Tworzę nowy arkusz: {sheet_name}")
|
||||
ws = spreadsheet.add_worksheet(title=sheet_name, rows=100, cols=10)
|
||||
ws.append_row(["#", "Link", "Nr zamówienia", "Model", "Wykończenie", "Kolor Top", "Kolor Body", "Kolor Neck", "Kolor Head", "Finish K/C", "Finish S"])
|
||||
ws = self._execute_with_retry(spreadsheet.add_worksheet, title=sheet_name, rows=100, cols=10)
|
||||
header = ["#", "Link", "Nr zamówienia", "Model", "Wykończenie", "Kolor Top", "Kolor Body", "Kolor Neck", "Kolor Head", "Finish K/C", "Finish S"]
|
||||
self._execute_with_retry(ws.append_row, header, value_input_option="USER_ENTERED")
|
||||
return ws
|
||||
|
||||
def batch_append_unique_rows(self, doc_name, sheet_name, rows_data):
|
||||
@@ -49,30 +88,26 @@ class GSheetAPI:
|
||||
|
||||
ws = self.ensure_worksheet(doc_name, sheet_name)
|
||||
|
||||
# 1. Pobierz wszystkie istniejące numery zamówień w JEDNYM zapytaniu
|
||||
logging.info("🔍 Sprawdzam istniejące numery zamówień w arkuszu docelowym...")
|
||||
# existing_orders = set(ws.col_values(3))
|
||||
existing_orders = {str(x).strip() for x in ws.col_values(3)}
|
||||
existing_orders_values = self._execute_with_retry(ws.col_values, 3)
|
||||
existing_orders = {str(x).strip() for x in existing_orders_values}
|
||||
|
||||
logging.debug(f"Znaleziono {len(existing_orders)} istniejących numerów.\n existing_orders: {existing_orders}")
|
||||
|
||||
# 2. Filtruj nowe wiersze, aby znaleźć tylko te unikalne
|
||||
unique_rows_to_add = []
|
||||
for row in rows_data:
|
||||
order_number = str(row[2]).strip()
|
||||
if order_number not in existing_orders:
|
||||
unique_rows_to_add.append(row)
|
||||
# Dodaj nowo dodany numer do seta, aby uniknąć duplikatów w ramach jednej paczki
|
||||
existing_orders.add(order_number)
|
||||
|
||||
# 3. Dodaj wszystkie unikalne wiersze w JEDNYM zapytaniu
|
||||
if unique_rows_to_add:
|
||||
logging.info(f"📝 Dodaję {len(unique_rows_to_add)} nowych unikalnych wierszy do arkusza {sheet_name}...")
|
||||
ws.append_rows(unique_rows_to_add, value_input_option="USER_ENTERED") # type: ignore
|
||||
self._execute_with_retry(ws.append_rows, unique_rows_to_add, value_input_option="USER_ENTERED")
|
||||
logging.info("✅ Zakończono dodawanie.")
|
||||
else:
|
||||
logging.info("ℹ️ Nie znaleziono żadnych nowych wierszy do dodania.")
|
||||
|
||||
skipped_count = len(rows_data) - len(unique_rows_to_add)
|
||||
if skipped_count > 0:
|
||||
logging.info(f"⏭️ Pominięto {skipped_count} wierszy, które już istniały w arkuszu.")
|
||||
logging.info(f"⏭️ Pominięto {skipped_count} wierszy, które już istniały w arkuszu.")
|
||||
|
||||
@@ -16,10 +16,13 @@ def setup_logging():
|
||||
if not os.path.exists(LOGS_DIR):
|
||||
os.makedirs(LOGS_DIR)
|
||||
|
||||
# 3. Stwórz i skonfiguruj handler dla konsoli (poziom INFO)
|
||||
# 3. Stwórz i skonfiguruj handler dla konsoli (poziom INFO lub DEBUG)
|
||||
debug_mode = os.getenv('CONFIG_DEBUG', 'false').lower() == 'true'
|
||||
console_level = logging.DEBUG if debug_mode else logging.INFO
|
||||
|
||||
console_handler = logging.StreamHandler(sys.stdout)
|
||||
console_handler.setLevel(logging.INFO)
|
||||
console_formatter = logging.Formatter('%(levelname)s - %(message)s')
|
||||
console_handler.setLevel(console_level)
|
||||
console_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
||||
console_handler.setFormatter(console_formatter)
|
||||
logger.addHandler(console_handler)
|
||||
|
||||
|
||||
@@ -1,6 +1,16 @@
|
||||
import re
|
||||
import logging
|
||||
|
||||
SUFFIX_TO_CATEGORY = {
|
||||
'G': 'gloss',
|
||||
'S': 'satin',
|
||||
'M': 'mat',
|
||||
'R': 'mat',
|
||||
'MAT': 'mat',
|
||||
'RAW': 'mat',
|
||||
'AG': 'aged',
|
||||
}
|
||||
|
||||
def normalize(text):
|
||||
if not text:
|
||||
return ""
|
||||
@@ -11,7 +21,13 @@ def get_finish_suffix(color):
|
||||
"""Extracts the finish suffix (e.g., 'G', 'S', 'M') from a color string."""
|
||||
if not color:
|
||||
return None
|
||||
return color.strip().split('-')[-1].upper()
|
||||
known_suffixes = list(SUFFIX_TO_CATEGORY.keys())
|
||||
pattern = r'\b(' + '|'.join(known_suffixes) + r')\b'
|
||||
matches = re.findall(pattern, color.upper())
|
||||
if matches:
|
||||
return matches[-1]
|
||||
return None
|
||||
|
||||
|
||||
def get_finish_type(row_data):
|
||||
"""Determines the finish type (GLOSS, SATIN, MAT, MIX) based on color suffixes."""
|
||||
@@ -26,31 +42,25 @@ def get_finish_type(row_data):
|
||||
top_suffix = top_suffix or body_suffix
|
||||
body_suffix = body_suffix or top_suffix
|
||||
|
||||
suffix_to_category = {
|
||||
'G': 'gloss',
|
||||
'S': 'satin',
|
||||
'M': 'mat',
|
||||
'R': 'mat',
|
||||
'MAT': 'mat',
|
||||
'RAW': 'mat',
|
||||
}
|
||||
|
||||
top_category = suffix_to_category.get(top_suffix)
|
||||
body_category = suffix_to_category.get(body_suffix)
|
||||
top_category = SUFFIX_TO_CATEGORY.get(top_suffix)
|
||||
body_category = SUFFIX_TO_CATEGORY.get(body_suffix)
|
||||
|
||||
if not top_category or not body_category:
|
||||
return None # Suffix not in our map
|
||||
return None # Suffix not in our map
|
||||
|
||||
if top_category == body_category:
|
||||
return top_category.upper()
|
||||
|
||||
if 'mat' in {top_category, body_category}:
|
||||
# If categories are different, and one of them is 'mat' or 'aged', it's a MIX.
|
||||
mix_triggers = {'mat', 'aged'}
|
||||
categories = {top_category, body_category}
|
||||
if categories.intersection(mix_triggers):
|
||||
return 'MIX'
|
||||
|
||||
except (KeyError, AttributeError):
|
||||
# This will catch if row_data is not a dict or keys are missing
|
||||
return None
|
||||
|
||||
|
||||
return None
|
||||
|
||||
def process_row(row, mayo, counter):
|
||||
@@ -64,20 +74,23 @@ def process_row(row, mayo, counter):
|
||||
if not link:
|
||||
return None
|
||||
|
||||
logging.info(f"\n🔗 Sprawdzam: {link}")
|
||||
logging.debug(f"\n🔗 Sprawdzam: {link}")
|
||||
try:
|
||||
info = mayo.get_order_info(link)
|
||||
order_number = info["order_number"]
|
||||
model = info["model"]
|
||||
|
||||
logging.info(f"Nr z arkusza: {nr_zam}")
|
||||
logging.info(f"Nr ze strony: {order_number}")
|
||||
logging.info(f"Model: {model}")
|
||||
|
||||
if normalize(order_number) == normalize(nr_zam):
|
||||
logging.info("✅ Numer się zgadza")
|
||||
logging.debug("✅ Numer się zgadza")
|
||||
logging.debug(f"Nr z arkusza: {nr_zam}")
|
||||
logging.debug(f"Nr ze strony: {order_number}")
|
||||
logging.debug(f"Model: {model}")
|
||||
else:
|
||||
logging.warning("⚠️ Numer NIE pasuje!")
|
||||
logging.warning(f"Nr z arkusza: {nr_zam}")
|
||||
logging.warning(f"Nr ze strony: {order_number}")
|
||||
logging.warning(f"Model: {model}")
|
||||
|
||||
row_data = [
|
||||
counter,
|
||||
|
||||
Reference in New Issue
Block a user