update
This commit is contained in:
@@ -123,6 +123,35 @@ def parse_history_month(value):
|
||||
return text
|
||||
|
||||
|
||||
def month_locative(month):
|
||||
names = {
|
||||
"01": "styczniu",
|
||||
"02": "lutym",
|
||||
"03": "marcu",
|
||||
"04": "kwietniu",
|
||||
"05": "maju",
|
||||
"06": "czerwcu",
|
||||
"07": "lipcu",
|
||||
"08": "sierpniu",
|
||||
"09": "wrześniu",
|
||||
"10": "październiku",
|
||||
"11": "listopadzie",
|
||||
"12": "grudniu",
|
||||
}
|
||||
return names.get(str(month)[5:7], str(month))
|
||||
|
||||
|
||||
def normalize_month_reference(text, month):
|
||||
"""Keep SEO worksheet text aligned with the report month when row date matches."""
|
||||
target = month_locative(month)
|
||||
return re.sub(
|
||||
r"^W miesiącu [A-Za-ząćęłńóśźżĄĆĘŁŃÓŚŹŻ]+",
|
||||
f"W {target}",
|
||||
text,
|
||||
count=1,
|
||||
)
|
||||
|
||||
|
||||
def parse_sheet_config(sheet_config):
|
||||
value = str(sheet_config or "").strip()
|
||||
if not value:
|
||||
@@ -192,6 +221,43 @@ def fetch_sales_history_from_sheet(domain, sheet_config):
|
||||
return sorted(history, key=lambda item: item["month"])
|
||||
|
||||
|
||||
def fetch_seo_text_rows_from_sheet(sheet_config, month):
|
||||
"""Fetch monthly SEO text rows from a public Google Sheet CSV export."""
|
||||
spreadsheet_id, gid = parse_sheet_config(sheet_config)
|
||||
export_url = f"https://docs.google.com/spreadsheets/d/{spreadsheet_id}/gviz/tq?tqx=out:csv"
|
||||
if gid:
|
||||
export_url += f"&gid={gid}"
|
||||
response = requests.get(export_url, timeout=30)
|
||||
response.raise_for_status()
|
||||
response.encoding = "utf-8"
|
||||
|
||||
reader = csv.DictReader(io.StringIO(response.text))
|
||||
rows = []
|
||||
for row in reader:
|
||||
normalized = {normalize_header(key): value for key, value in row.items()}
|
||||
row_month = parse_history_month(
|
||||
normalized.get("month")
|
||||
or normalized.get("miesiac")
|
||||
or normalized.get("data")
|
||||
or normalized.get("date")
|
||||
)
|
||||
text = (
|
||||
normalized.get("url")
|
||||
or normalized.get("link")
|
||||
or normalized.get("tekst")
|
||||
or normalized.get("opis")
|
||||
or normalized.get("dzialanie")
|
||||
or normalized.get("dzialania")
|
||||
or ""
|
||||
).strip()
|
||||
if row_month == month and text:
|
||||
rows.append({
|
||||
"date": row.get("Data") or row.get("data") or row_month,
|
||||
"url": normalize_month_reference(text, month),
|
||||
})
|
||||
return rows
|
||||
|
||||
|
||||
def apply_sheet_ecommerce(report, sales_history, month, previous_month):
|
||||
"""Use Google Sheet sales data for e-commerce KPI cards."""
|
||||
by_month = {row["month"]: row for row in sales_history}
|
||||
@@ -785,6 +851,29 @@ def main():
|
||||
else:
|
||||
report["seo_links"] = []
|
||||
|
||||
# SEO works and links from config/clients.toml override the legacy .env-only fetch above.
|
||||
seo_works_sheet = client_report_config.get("seo_works_history_sheet")
|
||||
if seo_works_sheet:
|
||||
print(f"Pobieram dzialania SEO z config/clients.toml...")
|
||||
try:
|
||||
seo_work_rows = fetch_seo_text_rows_from_sheet(seo_works_sheet, args.month)
|
||||
report["seo_activities"] = "\n\n".join(row["url"] for row in seo_work_rows)
|
||||
print(f" Dzialania SEO: {len(seo_work_rows)} wpisow w {args.month}")
|
||||
except Exception as e:
|
||||
print(f" UWAGA: Blad dzialan SEO: {e}")
|
||||
report["seo_activities"] = ""
|
||||
|
||||
seo_links_sheet = client_report_config.get("seo_links_history_sheet")
|
||||
if seo_links_sheet:
|
||||
print(f"Pobieram linki SEO z config/clients.toml...")
|
||||
try:
|
||||
seo_links = fetch_seo_text_rows_from_sheet(seo_links_sheet, args.month)
|
||||
report["seo_links"] = seo_links or []
|
||||
print(f" Linki SEO: {len(report['seo_links'])} w {args.month}")
|
||||
except Exception as e:
|
||||
print(f" UWAGA: Blad SEO links: {e}")
|
||||
report["seo_links"] = []
|
||||
|
||||
# Output
|
||||
if args.output:
|
||||
output_path = Path(args.output)
|
||||
|
||||
Reference in New Issue
Block a user