90 lines
2.7 KiB
Bash
Executable File
90 lines
2.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
||
set -euo pipefail
|
||
|
||
BASE="/home/openclaw/.openclaw/workspace/skills/paradiz/references"
|
||
STATE_DIR="/home/openclaw/.openclaw/workspace/.openclaw"
|
||
STATE_FILE="$STATE_DIR/paradiz-prices-sync.state"
|
||
OUT_FILE="$BASE/prices.csv"
|
||
mkdir -p "$STATE_DIR"
|
||
|
||
python3 - <<'PY'
|
||
import csv
|
||
import glob
|
||
import hashlib
|
||
import os
|
||
from pathlib import Path
|
||
|
||
base = Path('/home/openclaw/.openclaw/workspace/skills/paradiz/references')
|
||
state_file = Path('/home/openclaw/.openclaw/workspace/.openclaw/paradiz-prices-sync.state')
|
||
out_file = base / 'prices.csv'
|
||
|
||
candidates = sorted(glob.glob(str(base / 'prices_human_*.csv')), key=lambda p: os.path.getmtime(p), reverse=True)
|
||
if not candidates:
|
||
raise SystemExit(0)
|
||
|
||
src = Path(candidates[0]) # берём самый свежий human-файл
|
||
raw = src.read_bytes()
|
||
sig = hashlib.sha256(raw).hexdigest()
|
||
if state_file.exists() and state_file.read_text(encoding='utf-8').strip() == sig:
|
||
raise SystemExit(0)
|
||
|
||
rows = None
|
||
for enc in ('utf-8-sig', 'cp1251', 'utf-8'):
|
||
try:
|
||
txt = raw.decode(enc)
|
||
reader = csv.DictReader(txt.splitlines(), delimiter=';')
|
||
rows = list(reader)
|
||
if reader.fieldnames:
|
||
break
|
||
except Exception:
|
||
continue
|
||
|
||
if rows is None:
|
||
raise SystemExit(0)
|
||
|
||
# русские -> канонические поля prices.csv
|
||
mapping = {
|
||
'Тип номера': 'room',
|
||
'Период с': 'date_from',
|
||
'Период по': 'date_to',
|
||
'Гостей от': 'guests_min',
|
||
'Гостей до': 'guests_max',
|
||
'Питание': 'meal',
|
||
'Цена за ночь': 'price_per_night',
|
||
'Валюта': 'currency',
|
||
'Примечание': 'notes',
|
||
}
|
||
|
||
fields = ['date_from','date_to','guests_min','guests_max','room','meal','price_per_night','currency','notes']
|
||
|
||
normalized = []
|
||
for r in rows:
|
||
if not r:
|
||
continue
|
||
item = {k: '' for k in fields}
|
||
for ru, canon in mapping.items():
|
||
item[canon] = (r.get(ru, '') or '').strip()
|
||
|
||
# cleanup
|
||
if item['currency'].upper() == 'RUB':
|
||
item['currency'] = '₽'
|
||
if item['price_per_night'].endswith(' руб.'):
|
||
item['price_per_night'] = item['price_per_night'].replace(' руб.', '').strip()
|
||
|
||
# пропускаем пустые строки
|
||
if not item['room'] or not item['date_from'] or not item['date_to'] or not item['price_per_night']:
|
||
continue
|
||
normalized.append(item)
|
||
|
||
if not normalized:
|
||
raise SystemExit(0)
|
||
|
||
with out_file.open('w', encoding='utf-8', newline='') as f:
|
||
w = csv.DictWriter(f, fieldnames=fields)
|
||
w.writeheader()
|
||
w.writerows(normalized)
|
||
|
||
state_file.write_text(sig, encoding='utf-8')
|
||
print(f'synced from {src.name}: {len(normalized)} rows')
|
||
PY
|