notebookvb

This commit is contained in:
Vladimir Buzalka
2026-05-08 22:06:57 +02:00
parent c9903646f1
commit c4c0d1d435
14 changed files with 1666 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
# DailySuguru — technické poznámky
## Přehled skriptů
| Skript | Popis |
|--------|-------|
| `preskumaj_suguru.py` | Průzkumný — vytáhne `gameLevels` z JS kontextu stránky |
| `stahni_suguru.py` | Stáhne data z webu a uloží do MySQL (celý rok najednou) |
| `vykresli_suguru.py` | Generuje PDF z dat v MySQL (reportlab, vektorové) |
## Zdroj dat
Stránka: https://www.solitaire.org/daily-suguru/
Stejná architektura jako ostatní puzzle — `game.php` načte `gameLevels` s daty pro celý rok (366 dní × 3 velikosti). Klíče `"MM-DD"`, bez roku.
## Obtížnosti (velikosti mřížek)
| Klíč | Rozměr |
|------|--------|
| 6x6 | 6×6 |
| 8x8 | 8×8 |
| 10x10 | 10×10 |
## Datová struktura `gameLevels`
Každý záznam je objekt `{colors, clues, solution}`:
- `colors` = skupiny buněk (písmena AZ), flat string řádek po řádku
- `clues` = nápovědy, tečka = prázdná buňka
- `solution` = řešení, flat string číslic
Pravidla Suguru: každá skupina velikosti N obsahuje čísla 1N, žádné dvě sousední buňky (včetně diagonálně) nesmí mít stejné číslo.
## MySQL tabulka `puzzle.puzzles`
Sdílená tabulka s ostatními puzzle. Pro Suguru:
- `game_type` = `'suguru'`
- `difficulty` = `'6x6'` / `'8x8'` / `'10x10'`
- `puzzle` = `colors|clues` (dva stringy spojené `|`)
- `solution` = flat string řešení
- `extra` = `{"grid_size": 6}` / `8` / `10`
- `source` = `'solitaire.org'`
Stav: celý rok 2026 naplněn (1095 řádků = 365 dní × 3 velikosti).
@@ -0,0 +1,97 @@
"""
Stáhne daily Suguru puzzle data ze solitaire.org a uloží do MySQL.
"""
import asyncio
import json
import sys
sys.stdout.reconfigure(encoding="utf-8")
sys.stderr.reconfigure(encoding="utf-8")
from datetime import date, timedelta
from pathlib import Path
from playwright.async_api import async_playwright
sys.path.insert(0, str(Path(__file__).parent.parent.parent / "Knihovny"))
from mysql_db import connect_mysql
URL = "https://www.solitaire.org/daily-suguru/"
DIFFICULTIES = ["6x6", "8x8", "10x10"]
async def fetch_all_levels() -> dict:
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True)
context = await browser.new_context(viewport={"width": 1280, "height": 900})
page = await context.new_page()
print(f"Načítám {URL} ...")
await page.goto(URL, wait_until="networkidle", timeout=60_000)
game_url = None
for frame in page.frames:
if frame.url != page.url and frame.url.strip() not in ("", "about:blank"):
game_url = frame.url
break
if not game_url:
iframe_src = await page.get_attribute("iframe", "src")
if iframe_src:
game_url = iframe_src if iframe_src.startswith("http") else f"https://www.solitaire.org{iframe_src}"
await page.close()
game_page = await context.new_page()
target_url = game_url if game_url else URL
print(f"Načítám hru: {target_url} ...")
await game_page.goto(target_url, wait_until="networkidle", timeout=60_000)
raw = await game_page.evaluate("() => JSON.stringify(gameLevels)")
await browser.close()
return json.loads(raw)
def save_to_mysql(game_levels: dict, start_date: date, end_date: date):
conn = connect_mysql(database="puzzle")
cur = conn.cursor()
inserted = 0
d = start_date
while d <= end_date:
mmdd = d.strftime("%m-%d")
date_str = d.strftime("%Y-%m-%d")
for diff in DIFFICULTIES:
if diff not in game_levels or mmdd not in game_levels[diff]:
continue
entry = game_levels[diff][mmdd]
grid_size = int(diff.split("x")[0])
puzzle_str = entry["colors"] + "|" + entry["clues"]
cur.execute(
"INSERT IGNORE INTO puzzles (game_type, difficulty, puzzle_date, puzzle, solution, extra, source) "
"VALUES (%s, %s, %s, %s, %s, %s, %s)",
("suguru", diff, date_str, puzzle_str, entry["solution"],
json.dumps({"grid_size": grid_size}), "solitaire.org"),
)
if cur.rowcount > 0:
inserted += 1
d += timedelta(days=1)
cur.close()
conn.close()
return inserted
async def main():
game_levels = await fetch_all_levels()
total = sum(len(game_levels.get(d, {})) for d in DIFFICULTIES)
print(f"gameLevels: {total} záznamů")
inserted = save_to_mysql(game_levels, date(2026, 1, 1), date(2026, 12, 31))
print(f"MySQL: vloženo {inserted} nových řádků (celý rok 2026)")
if __name__ == "__main__":
asyncio.run(main())
@@ -0,0 +1,177 @@
"""
Vykreslí Suguru puzzle do PDF z dat v MySQL tabulce puzzles.
"""
import json
import os
import sys
from pathlib import Path
sys.stdout.reconfigure(encoding="utf-8")
sys.path.insert(0, str(Path(__file__).parent.parent.parent / "Knihovny"))
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import cm
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen.canvas import Canvas
from mysql_db import connect_mysql
_fonts_dir = os.path.join(os.environ.get("WINDIR", r"C:\Windows"), "Fonts")
pdfmetrics.registerFont(TTFont("Arial", os.path.join(_fonts_dir, "arial.ttf")))
pdfmetrics.registerFont(TTFont("ArialBold", os.path.join(_fonts_dir, "arialbd.ttf")))
OUTPUT = Path(__file__).parent / "test_suguru.pdf"
def parse_puzzle(puzzle_str: str, grid_size: int):
colors_str, clues_str = puzzle_str.split("|")
rows = grid_size
cols = len(colors_str) // rows
color_map = []
clues = []
for r in range(rows):
color_row = []
clue_row = []
for c_ in range(cols):
idx = r * cols + c_
color_row.append(colors_str[idx])
ch = clues_str[idx]
clue_row.append(int(ch) if ch != "." else 0)
color_map.append(color_row)
clues.append(clue_row)
return color_map, clues, rows, cols
def parse_solution(solution_str: str, rows: int, cols: int) -> list[list[int]]:
return [[int(solution_str[r * cols + c]) for c in range(cols)]
for r in range(rows)]
def draw_suguru(c: Canvas, x0: float, y0: float, cell: float,
color_map: list, clues: list, rows: int, cols: int,
title: str = "", solution: list | None = None):
num_font = max(cell * 0.5, 7)
thin = 0.4
thick = 2.2
if title:
c.setFont("ArialBold", 12)
c.drawString(x0, y0 + 5, title)
# Bílé pozadí
c.setFillColor(colors.white)
c.rect(x0, y0 - rows * cell, cols * cell, rows * cell, fill=1, stroke=0)
# Čísla — nápovědy (zadání) nebo řešení
for r in range(rows):
for co in range(cols):
cx = x0 + co * cell + cell / 2
cy = y0 - (r + 1) * cell + cell * 0.3
if clues[r][co] > 0:
c.setFillColor(colors.black)
c.setFont("ArialBold", num_font)
c.drawCentredString(cx, cy, str(clues[r][co]))
elif solution:
c.setFillColor(colors.Color(0.4, 0.4, 0.4))
c.setFont("Arial", num_font)
c.drawCentredString(cx, cy, str(solution[r][co]))
# Tenké vnitřní čáry
c.setStrokeColor(colors.Color(0.75, 0.75, 0.75))
c.setLineWidth(thin)
for i in range(1, rows):
c.line(x0, y0 - i * cell, x0 + cols * cell, y0 - i * cell)
for i in range(1, cols):
c.line(x0 + i * cell, y0, x0 + i * cell, y0 - rows * cell)
# Tlusté hrany mezi různými skupinami
c.setStrokeColor(colors.black)
c.setLineWidth(thick)
# Vnější okraj
c.rect(x0, y0 - rows * cell, cols * cell, rows * cell, fill=0, stroke=1)
# Horizontální hrany
for r in range(rows - 1):
seg_start = None
for co in range(cols):
border = color_map[r][co] != color_map[r + 1][co]
if border and seg_start is None:
seg_start = co
elif not border and seg_start is not None:
c.line(x0 + seg_start * cell, y0 - (r + 1) * cell,
x0 + co * cell, y0 - (r + 1) * cell)
seg_start = None
if seg_start is not None:
c.line(x0 + seg_start * cell, y0 - (r + 1) * cell,
x0 + cols * cell, y0 - (r + 1) * cell)
# Vertikální hrany
for co in range(cols - 1):
seg_start = None
for r in range(rows):
border = color_map[r][co] != color_map[r][co + 1]
if border and seg_start is None:
seg_start = r
elif not border and seg_start is not None:
c.line(x0 + (co + 1) * cell, y0 - seg_start * cell,
x0 + (co + 1) * cell, y0 - r * cell)
seg_start = None
if seg_start is not None:
c.line(x0 + (co + 1) * cell, y0 - seg_start * cell,
x0 + (co + 1) * cell, y0 - rows * cell)
def main():
conn = connect_mysql(database="puzzle")
cur = conn.cursor()
cur.execute(
"SELECT difficulty, puzzle, solution, extra FROM puzzles "
"WHERE game_type='suguru' AND puzzle_date='2026-05-08' "
"ORDER BY FIELD(difficulty, '6x6', '8x8', '10x10') "
"LIMIT 1"
)
row = cur.fetchone()
cur.close()
conn.close()
if not row:
print("Žádná data.")
return
difficulty, puzzle_str, solution_str, extra_json = row
extra = json.loads(extra_json)
grid_size = extra["grid_size"]
color_map, clues, rows, cols = parse_puzzle(puzzle_str, grid_size)
solution = parse_solution(solution_str, rows, cols)
page_w, page_h = A4
board_cm = 11
cell = board_cm * cm / max(rows, cols)
board_w = cols * cell
board_h = rows * cell
c = Canvas(str(OUTPUT), pagesize=A4)
# Zadání
x0 = (page_w - board_w) / 2
y0 = page_h - 2 * cm
draw_suguru(c, x0, y0, cell, color_map, clues, rows, cols,
f"Suguru {difficulty} — 2026-05-08")
# Řešení
y0_sol = y0 - board_h - 3 * cm
draw_suguru(c, x0, y0_sol, cell, color_map, clues, rows, cols,
"Řešení", solution=solution)
c.save()
print(f"PDF uloženo: {OUTPUT}")
if __name__ == "__main__":
main()