notebookvb

This commit is contained in:
Vladimir Buzalka
2026-05-08 13:30:47 +02:00
parent ee6fd79f5c
commit c9903646f1
20 changed files with 2024 additions and 56 deletions
+57
View File
@@ -0,0 +1,57 @@
# DailyCalcudoku — technické poznámky
## Přehled skriptů
| Skript | Popis |
|--------|-------|
| `preskumaj_calcudoku.py` | Průzkumný — vytáhne `gameLevels` z JS kontextu stránky |
| `stahni_calcudoku.py` | Stáhne data z webu a uloží do MySQL (celý rok najednou) |
| `vykresli_calcudoku.py` | Generuje PDF z dat v MySQL (reportlab, vektorové) |
## Zdroj dat
Stránka: https://www.solitaire.org/daily-calcudoku/
Stejná architektura jako Kakuro/Str8ts — `game.php` načte `gameLevels` s daty pro celý rok (366 dní × 4 velikosti). Klíče `"MM-DD"`, bez roku. Data se nemění přes rok — stačí jednorázové stažení v lednu.
## Obtížnosti (velikosti mřížek)
| Klíč | Rozměr | Číslice |
|------|--------|---------|
| 4x4 | 4×4 | 14 |
| 5x5 | 5×5 | 15 |
| 6x6 | 6×6 | 16 |
| 8x8 | 8×8 | 18 |
## Datová struktura `gameLevels`
Každý záznam je pole dvou stringů: `[cages, solution]`
### Cages (definice klecí)
Klece oddělené `|`, každá ve formátu `target,operator,cells`:
```
3,*,a1b1|8,+,a2b2a3|2,/,c4d4
```
- `target` = cílová hodnota
- `operator` = `+`, `-`, `*`, `/`
- `cells` = seznam buněk (sloupec=písmeno ah, řádek=číslo 18)
### Solution (řešení)
Flat string číslic, řádek po řádku:
```
1342213442133421 (4×4 = 16 znaků)
```
## MySQL tabulka `puzzle.puzzles`
Sdílená tabulka s ostatními puzzle. Pro Calcudoku:
- `game_type` = `'calcudoku'`
- `difficulty` = `'4x4'` / `'5x5'` / `'6x6'` / `'8x8'`
- `puzzle` = cage definice (cages string)
- `solution` = flat string řešení
- `extra` = `{"grid_size": 4}` / `5` / `6` / `8`
- `source` = `'solitaire.org'`
Stav: celý rok 2026 naplněn (1460 řádků = 365 dní × 4 velikosti).
@@ -0,0 +1,149 @@
"""
Průzkumný skript: připojí se na solitaire.org/daily-calcudoku/ a vytáhne
surová JS data o puzzle (gameLevels, Game objekt).
"""
import asyncio
import json
import sys
sys.stdout.reconfigure(encoding="utf-8")
from playwright.async_api import async_playwright
URL = "https://www.solitaire.org/daily-calcudoku/"
async def main():
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
print(f" Nalezen iframe: {game_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}"
print(f" Iframe src z DOM: {game_url}")
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)
# 1) gameLevels
print("\n=== gameLevels ===")
game_levels = await game_page.evaluate("""() => {
if (typeof gameLevels !== 'undefined') return JSON.stringify(gameLevels, null, 2);
return null;
}""")
if game_levels:
print(game_levels[:5000])
if len(game_levels) > 5000:
print(f"... (celkem {len(game_levels)} znaků)")
else:
print(" gameLevels není definováno")
# 2) Game objekt — klíče
print("\n=== Game objekt — klíče ===")
game_keys = await game_page.evaluate("""() => {
if (typeof Game !== 'undefined') return Object.keys(Game);
return null;
}""")
if game_keys:
print(json.dumps(game_keys, indent=2))
else:
print(" Game není definováno")
# 3) Game — datové vlastnosti
print("\n=== Game — datové vlastnosti ===")
game_data = await game_page.evaluate("""() => {
if (typeof Game === 'undefined') return null;
const result = {};
for (const key of Object.keys(Game)) {
const val = Game[key];
if (typeof val !== 'function') {
try { result[key] = JSON.parse(JSON.stringify(val)); }
catch(e) { result[key] = String(val); }
}
}
return result;
}""")
if game_data:
txt = json.dumps(game_data, indent=2, ensure_ascii=False)
print(txt[:3000])
else:
print(" žádná data")
# 4) Další globální proměnné
print("\n=== Další globální proměnné ===")
globals_check = await game_page.evaluate("""() => {
const names = ['puzzleData', 'dailyPuzzle', 'gameData', 'levels',
'puzzle', 'boardData', 'board', 'grid', 'cells',
'calcudokuData', 'calcudokuLevels', 'cages', 'groups'];
const found = {};
for (const name of names) {
if (typeof window[name] !== 'undefined') {
found[name] = typeof window[name];
}
}
return found;
}""")
print(json.dumps(globals_check, indent=2))
# 5) gameLevels — struktura klíčů a ukázka
print("\n=== gameLevels — struktura ===")
structure = await game_page.evaluate("""() => {
if (typeof gameLevels === 'undefined') return null;
const result = {};
for (const diff of Object.keys(gameLevels)) {
const keys = Object.keys(gameLevels[diff]);
result[diff] = {
count: keys.length,
first_keys: keys.slice(0, 3),
last_keys: keys.slice(-3),
sample_value: gameLevels[diff][keys[0]]
};
}
return result;
}""")
if structure:
print(json.dumps(structure, indent=2, ensure_ascii=False)[:5000])
else:
print(" žádná struktura")
# 6) Dnešní data
print("\n=== Dnešní data (05-08) ===")
today_data = await game_page.evaluate("""() => {
const key = '05-08';
const result = {};
if (typeof gameLevels === 'undefined') return null;
for (const diff of Object.keys(gameLevels)) {
if (gameLevels[diff] && gameLevels[diff][key]) {
result[diff] = gameLevels[diff][key];
}
}
return result;
}""")
if today_data:
print(json.dumps(today_data, indent=2, ensure_ascii=False)[:5000])
else:
print(" žádná data pro dnešek")
await browser.close()
if __name__ == "__main__":
asyncio.run(main())
@@ -0,0 +1,96 @@
"""
Stáhne daily Calcudoku 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-calcudoku/"
DIFFICULTIES = ["4x4", "5x5", "6x6", "8x8"]
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
cages, solution = game_levels[diff][mmdd]
grid_size = int(diff.split("x")[0])
cur.execute(
"INSERT IGNORE INTO puzzles (game_type, difficulty, puzzle_date, puzzle, solution, extra, source) "
"VALUES (%s, %s, %s, %s, %s, %s, %s)",
("calcudoku", diff, date_str, cages, 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,208 @@
"""
Vykreslí Calcudoku puzzle do PDF z dat v MySQL tabulce puzzles.
"""
import json
import os
import re
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_calcudoku.pdf"
OP_DISPLAY = {"+": "+", "-": "", "*": "×", "/": "÷"}
def parse_cages(cages_str: str) -> list[dict]:
cages = []
for part in cages_str.split("|"):
target, op, cells_str = part.split(",", 2)
cells = [(ord(m[0]) - ord("a"), int(m[1]) - 1)
for m in re.findall(r"([a-h])(\d)", cells_str)]
cages.append({"target": int(target), "op": op, "cells": cells})
return cages
def parse_solution(solution_str: str, grid_size: int) -> list[list[int]]:
return [[int(solution_str[r * grid_size + c])
for c in range(grid_size)]
for r in range(grid_size)]
def build_cage_map(cages: list[dict], grid_size: int) -> list[list[int]]:
cage_map = [[-1] * grid_size for _ in range(grid_size)]
for i, cage in enumerate(cages):
for col, row in cage["cells"]:
cage_map[row][col] = i
return cage_map
def cage_label(cage: dict) -> str:
if len(cage["cells"]) == 1:
return str(cage["target"])
return f"{cage['target']}{OP_DISPLAY.get(cage['op'], cage['op'])}"
def top_left_cell(cage: dict) -> tuple[int, int]:
return min(cage["cells"], key=lambda c: (c[1], c[0]))
def draw_calcudoku(c: Canvas, x0: float, y0: float, cell: float,
cages: list[dict], cage_map: list[list[int]],
grid_size: int, title: str = "",
solution: list[list[int]] | None = None):
label_font = max(cell * 0.28, 6)
num_font = max(cell * 0.45, 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 - grid_size * cell, grid_size * cell, grid_size * cell, fill=1, stroke=0)
# Řešení
if solution:
c.setFillColor(colors.black)
c.setFont("ArialBold", num_font)
for row in range(grid_size):
for col in range(grid_size):
cx = x0 + col * cell + cell / 2
cy = y0 - (row + 1) * cell + cell * 0.3
c.drawCentredString(cx, cy, str(solution[row][col]))
# Popisky klecí
c.setFillColor(colors.Color(0.15, 0.15, 0.15))
c.setFont("ArialBold", label_font)
for cage in cages:
col, row = top_left_cell(cage)
label = cage_label(cage)
lx = x0 + col * cell + cell * 0.06
ly = y0 - row * cell - label_font * 1.1
c.drawString(lx, ly, label)
# Tenké vnitřní čáry
c.setStrokeColor(colors.Color(0.75, 0.75, 0.75))
c.setLineWidth(thin)
for i in range(1, grid_size):
c.line(x0, y0 - i * cell, x0 + grid_size * cell, y0 - i * cell)
c.line(x0 + i * cell, y0, x0 + i * cell, y0 - grid_size * cell)
# Tlusté hrany mezi různými klecemi
c.setStrokeColor(colors.black)
c.setLineWidth(thick)
# Vnější okraj
bx = x0
by = y0 - grid_size * cell
bw = grid_size * cell
bh = grid_size * cell
c.rect(bx, by, bw, bh, fill=0, stroke=1)
# Horizontální hrany (mezi řádky row a row+1)
for row in range(grid_size - 1):
col_start = None
for col in range(grid_size):
border = cage_map[row][col] != cage_map[row + 1][col]
if border and col_start is None:
col_start = col
elif not border and col_start is not None:
lx1 = x0 + col_start * cell
lx2 = x0 + col * cell
ly = y0 - (row + 1) * cell
c.line(lx1, ly, lx2, ly)
col_start = None
if col_start is not None:
lx1 = x0 + col_start * cell
lx2 = x0 + grid_size * cell
ly = y0 - (row + 1) * cell
c.line(lx1, ly, lx2, ly)
# Vertikální hrany (mezi sloupci col a col+1)
for col in range(grid_size - 1):
row_start = None
for row in range(grid_size):
border = cage_map[row][col] != cage_map[row][col + 1]
if border and row_start is None:
row_start = row
elif not border and row_start is not None:
lx = x0 + (col + 1) * cell
ly1 = y0 - row_start * cell
ly2 = y0 - row * cell
c.line(lx, ly1, lx, ly2)
row_start = None
if row_start is not None:
lx = x0 + (col + 1) * cell
ly1 = y0 - row_start * cell
ly2 = y0 - grid_size * cell
c.line(lx, ly1, lx, ly2)
def main():
conn = connect_mysql(database="puzzle")
cur = conn.cursor()
cur.execute(
"SELECT difficulty, puzzle, solution, extra FROM puzzles "
"WHERE game_type='calcudoku' AND puzzle_date='2026-05-08' "
"ORDER BY FIELD(difficulty, '4x4', '5x5', '6x6', '8x8') "
"LIMIT 1"
)
row = cur.fetchone()
cur.close()
conn.close()
if not row:
print("Žádná data.")
return
difficulty, cages_str, solution_str, extra_json = row
extra = json.loads(extra_json)
grid_size = extra["grid_size"]
cages = parse_cages(cages_str)
cage_map = build_cage_map(cages, grid_size)
solution = parse_solution(solution_str, grid_size)
page_w, page_h = A4
board_cm = 11
cell = board_cm * cm / grid_size
board = grid_size * cell
c = Canvas(str(OUTPUT), pagesize=A4)
# Zadání
x0 = (page_w - board) / 2
y0 = page_h - 2 * cm
draw_calcudoku(c, x0, y0, cell, cages, cage_map, grid_size,
f"Calcudoku {difficulty} — 2026-05-08")
# Řešení
y0_sol = y0 - board - 3 * cm
draw_calcudoku(c, x0, y0_sol, cell, cages, cage_map, grid_size,
"Řešení", solution=solution)
c.save()
print(f"PDF uloženo: {OUTPUT}")
if __name__ == "__main__":
main()
+66
View File
@@ -0,0 +1,66 @@
# DailyKakuro — technické poznámky
## Přehled skriptů
| Skript | Popis |
|--------|-------|
| `preskumaj_kakuro.py` | Průzkumný — vytáhne `gameLevels` z JS kontextu stránky |
| `stahni_kakuro.py` | Stáhne data z webu a uloží do MySQL (celý rok najednou) |
| `vykresli_kakuro.py` | Generuje PDF z dat v MySQL (reportlab, vektorové) |
## Zdroj dat
Stránka: https://www.solitaire.org/daily-kakuro/
Stejná architektura jako Str8ts — `game.php` načte `gameLevels` s daty pro celý rok (366 dní × 4 obtížnosti). Klíče `"MM-DD"`, bez roku. Data se nemění přes rok — stačí jednorázové stažení v lednu.
## Obtížnosti a velikosti mřížek
| Obtížnost | Rozměr mřížky |
|-----------|---------------|
| easy | 10×10 (data 9×9 + prepend) |
| medium | 10×10 |
| hard | 12×12 (data 11×11 + prepend) |
| expert | 12×12 |
## Datová struktura `gameLevels`
Každý záznam je jeden string — řádky oddělené čárkou:
```
"098400970,986230849,850049095,613007810,..."
```
Při zpracování se:
1. Ke každému řádku přidá `0` na začátek
2. Přidá se nulový řádek na začátek (horní okraj)
Význam číslic:
- `0` = černá/nápovědní buňka
- `1``9` = číslo řešení
**Součty se neukládají** — vypočítají se z řešení: sečtou se čísla ve směru doprava/dolů až po další nulu.
Data obsahují rovnou **řešení**, ne zadání. Zadání = prázdné bílé buňky + součty v černých.
## MySQL tabulka `puzzle.puzzles`
Sdílená tabulka se Str8ts. Pro Kakuro:
- `game_type` = `'kakuro'`
- `puzzle` = surový string (řešení + struktura)
- `solution` = stejný string (data jsou řešení)
- `extra` = `{"grid_size": 10}` nebo `{"grid_size": 12}`
- `source` = `'solitaire.org'`
Stav: celý rok 2026 naplněn (1460 řádků = 365 dní × 4 obtížnosti).
## Generování PDF (`vykresli_kakuro.py`)
- Knihovna: **reportlab** — vektorová grafika
- Nápovědní buňky (černé): tmavé pozadí + diagonála + součty (bílý text)
- Součet doprava → horní pravý trojúhelník
- Součet dolů → dolní levý trojúhelník
- Font: ArialBold (podpora české diakritiky)
## Bonus: `Game.combos`
Na stránce je i tabulka `Game.combos` — možné kombinace číslic pro každý součet a počet buněk. Např. součet 17 ze 2 buněk = `"8+9"`. Dá se použít jako nápověda pro řešení.
@@ -0,0 +1,141 @@
"""
Průzkumný skript: připojí se na solitaire.org/daily-kakuro/ a vytáhne
surová JS data o puzzle (gameLevels, Game objekt).
"""
import asyncio
import json
import sys
sys.stdout.reconfigure(encoding="utf-8")
from playwright.async_api import async_playwright
URL = "https://www.solitaire.org/daily-kakuro/"
async def main():
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
print(f" Nalezen iframe: {game_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}"
print(f" Iframe src z DOM: {game_url}")
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)
# 1) gameLevels
print("\n=== gameLevels ===")
game_levels = await game_page.evaluate("""() => {
if (typeof gameLevels !== 'undefined') return JSON.stringify(gameLevels, null, 2);
return null;
}""")
if game_levels:
print(game_levels[:5000])
if len(game_levels) > 5000:
print(f"... (celkem {len(game_levels)} znaků)")
else:
print(" gameLevels není definováno")
# 2) Game objekt — klíče
print("\n=== Game objekt — klíče ===")
game_keys = await game_page.evaluate("""() => {
if (typeof Game !== 'undefined') return Object.keys(Game);
return null;
}""")
if game_keys:
print(json.dumps(game_keys, indent=2))
else:
print(" Game není definováno")
# 3) Game — datové vlastnosti
print("\n=== Game — datové vlastnosti ===")
game_data = await game_page.evaluate("""() => {
if (typeof Game === 'undefined') return null;
const result = {};
for (const key of Object.keys(Game)) {
const val = Game[key];
if (typeof val !== 'function') {
try { result[key] = JSON.parse(JSON.stringify(val)); }
catch(e) { result[key] = String(val); }
}
}
return result;
}""")
if game_data:
txt = json.dumps(game_data, indent=2, ensure_ascii=False)
print(txt[:3000])
else:
print(" žádná data")
# 4) Další globální proměnné
print("\n=== Další globální proměnné ===")
globals_check = await game_page.evaluate("""() => {
const names = ['puzzleData', 'dailyPuzzle', 'gameData', 'levels',
'puzzle', 'boardData', 'board', 'grid', 'cells',
'kakuroData', 'kakuroLevels'];
const found = {};
for (const name of names) {
if (typeof window[name] !== 'undefined') {
found[name] = typeof window[name];
}
}
return found;
}""")
print(json.dumps(globals_check, indent=2))
# 5) Game.printDaily zdrojový kód
print("\n=== Game.printDaily — zdrojový kód ===")
print_src = await game_page.evaluate("""() => {
if (typeof Game !== 'undefined' && typeof Game.printDaily === 'function') {
return Game.printDaily.toString();
}
return null;
}""")
if print_src:
print(print_src[:3000])
else:
print(" Game.printDaily neexistuje")
# 6) Dnešní data — zkus první záznam
print("\n=== Dnešní data (05-08) ===")
today_data = await game_page.evaluate("""() => {
const key = '05-08';
const result = {};
if (typeof gameLevels === 'undefined') return null;
for (const diff of Object.keys(gameLevels)) {
if (gameLevels[diff] && gameLevels[diff][key]) {
result[diff] = gameLevels[diff][key];
}
}
return result;
}""")
if today_data:
print(json.dumps(today_data, indent=2, ensure_ascii=False)[:5000])
else:
print(" žádná data pro dnešek")
await browser.close()
if __name__ == "__main__":
asyncio.run(main())
@@ -0,0 +1,103 @@
"""
Stáhne daily Kakuro puzzle data ze solitaire.org a uloží do MySQL.
PDF generování bude doplněno později.
"""
import asyncio
import json
import sys
sys.stdout.reconfigure(encoding="utf-8")
sys.stderr.reconfigure(encoding="utf-8")
from datetime import date
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-kakuro/"
DIFFICULTIES = ["easy", "medium", "hard", "expert"]
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 grid_size(data_str: str) -> int:
"""Vrátí rozměr mřížky z raw dat (počet sloupců prvního řádku + 1 pro prepended 0)."""
first_row = data_str.split(",")[0]
return len(first_row) + 1
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
data_str = game_levels[diff][mmdd]
size = grid_size(data_str)
cur.execute(
"INSERT IGNORE INTO puzzles (game_type, difficulty, puzzle_date, puzzle, solution, extra, source) "
"VALUES (%s, %s, %s, %s, %s, %s, %s)",
("kakuro", diff, date_str, data_str, data_str,
json.dumps({"grid_size": size}), "solitaire.org"),
)
if cur.rowcount > 0:
inserted += 1
d += __import__("datetime").timedelta(days=1)
cur.close()
conn.close()
return inserted
async def main():
today = date.today()
game_levels = await fetch_all_levels()
print(f"gameLevels: {sum(len(game_levels.get(d, {})) for d in DIFFICULTIES)} 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,161 @@
"""
Vykreslí Kakuro 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_kakuro.pdf"
def parse_grid(data_str: str) -> list[str]:
rows = ["0" + r for r in data_str.split(",")]
rows.insert(0, "0" * len(rows[0]))
return rows
def sum_hor(grid, x, y):
s = 0
w = len(grid[0])
for i in range(x + 1, w):
if grid[y][i] == "0":
break
s += int(grid[y][i])
return s if s > 0 else 0
def sum_vert(grid, x, y):
s = 0
h = len(grid)
for i in range(y + 1, h):
if grid[i][x] == "0":
break
s += int(grid[i][x])
return s if s > 0 else 0
def draw_kakuro(c: Canvas, x0: float, y0: float, cell: float,
data_str: str, title: str = "", show_solution: bool = False):
grid = parse_grid(data_str)
h = len(grid)
w = len(grid[0])
clue_font = max(cell * 0.3, 5)
num_font = max(cell * 0.5, 6)
if title:
c.setFont("ArialBold", 12)
c.drawString(x0, y0 + 5, title)
for gy in range(h):
for gx in range(w):
cx = x0 + gx * cell
cy = y0 - (gy + 1) * cell
ch = grid[gy][gx]
if ch == "0":
# Černá buňka
c.setFillColor(colors.Color(0.15, 0.15, 0.15))
c.rect(cx, cy, cell, cell, fill=1, stroke=0)
sv = sum_vert(grid, gx, gy)
sh = sum_hor(grid, gx, gy)
if sv or sh:
# Diagonála
c.setStrokeColor(colors.white)
c.setLineWidth(0.5)
c.line(cx, cy + cell, cx + cell, cy)
c.setStrokeColor(colors.black)
c.setFillColor(colors.white)
c.setFont("ArialBold", clue_font)
if sh:
# Součet doprava — horní pravý trojúhelník
c.drawString(cx + cell * 0.52, cy + cell * 0.55, str(sh))
if sv:
# Součet dolů — dolní levý trojúhelník
c.drawString(cx + cell * 0.08, cy + cell * 0.08, str(sv))
else:
# Bílá buňka
if show_solution:
c.setFillColor(colors.black)
c.setFont("ArialBold", num_font)
c.drawCentredString(cx + cell / 2, cy + cell * 0.3, ch)
# Mřížka
c.setStrokeColor(colors.black)
for i in range(h + 1):
c.setLineWidth(0.8)
c.line(x0, y0 - i * cell, x0 + w * cell, y0 - i * cell)
for i in range(w + 1):
c.setLineWidth(0.8)
c.line(x0 + i * cell, y0, x0 + i * cell, y0 - h * cell)
def main():
conn = connect_mysql(database="puzzle")
cur = conn.cursor()
cur.execute(
"SELECT difficulty, puzzle, extra FROM puzzles "
"WHERE game_type='kakuro' AND puzzle_date='2026-05-08' "
"ORDER BY FIELD(difficulty, 'easy', 'medium', 'hard', 'expert') "
"LIMIT 1"
)
row = cur.fetchone()
cur.close()
conn.close()
if not row:
print("Žádná data.")
return
difficulty, data_str, extra = row
grid = parse_grid(data_str)
grid_size = len(grid[0])
page_w, page_h = A4
board_cm = 11
cell = board_cm * cm / grid_size
board = grid_size * cell
c = Canvas(str(OUTPUT), pagesize=A4)
# Zadání (bez čísel)
x0 = (page_w - board) / 2
y0 = page_h - 2 * cm
draw_kakuro(c, x0, y0, cell, data_str,
f"Kakuro {difficulty.capitalize()} — 2026-05-08", show_solution=False)
# Řešení (s čísly)
y0_sol = y0 - board - 3 * cm
draw_kakuro(c, x0, y0_sol, cell, data_str,
f"Řešení", show_solution=True)
c.save()
print(f"PDF uloženo: {OUTPUT}")
if __name__ == "__main__":
main()
+57 -3
View File
@@ -1,13 +1,21 @@
# DailyStr8ts — technické poznámky
Skript `stahni_str8ts.py` stahuje denní Str8ts puzzle jako PDF ze https://www.solitaire.org/daily-str8ts/ a odesílá emailem.
## Přehled skriptů
| Skript | Popis |
|--------|-------|
| `stahni_str8ts.py` | Stahuje denní puzzle jako PDF z webu, odesílá emailem |
| `preskumaj_str8ts.py` | Průzkumný — vytáhne `gameLevels` z JS kontextu stránky |
| `vykresli_puzzle.py` | Generuje vlastní PDF z dat v MySQL (reportlab, vektorové) |
| `vykresli_velikosti.py` | Testovací — jedno puzzle v 17 velikostech (182 cm) |
| `create_puzzles_table.sql` | DDL pro tabulku `puzzle.puzzles` |
## Stahování z webu (`stahni_str8ts.py`)
**Výstup:** `U:\Dropbox\!!!Days\Downloads Z230\yyyy-mm-dd Daily Str8ts puzzle.pdf`
**Příjemci:** vladimir.buzalka@buzalka.cz, alica.buzalkova@buzalka.cz
## Jak to funguje (klíčové)
Stránka `game.php` má funkci `Game.printDaily()` která:
1. Vytvoří skrytý `<iframe id="printFrame">` v DOM
2. Zapíše do něj puzzle HTML přes `contentWindow.document.write()` — všechny 3 obtížnosti (Easy, Medium, Hard)
@@ -21,8 +29,54 @@ Stránka `game.php` má funkci `Game.printDaily()` která:
- Přidáme `<base href="https://www.solitaire.org/daily-str8ts/">` pro obrázky
- Načteme HTML do nové stránky a uložíme jako `page.pdf()`
## Datová struktura `gameLevels`
JS objekt `gameLevels` obsahuje puzzle pro všechny dny roku, klíčované jako `gameLevels[difficulty]["MM-DD"]`.
Každý záznam má 3 stringy po 81 znacích (9×9 mřížka, po řádcích):
| Pole | Význam |
|------|--------|
| `puzzle` | Zadání: `1``9` = předvyplněné číslo, `.` = prázdná buňka |
| `bw` | Mapa barev: `0` = bílá buňka, `1` = černá buňka |
| `solution` | Řešení: `1``9` = číslo, `.` = černá bez čísla |
## MySQL tabulka `puzzle.puzzles`
Univerzální tabulka pro Str8ts, Sudoku i další puzzle typy.
```sql
CREATE TABLE puzzles (
id INT AUTO_INCREMENT PRIMARY KEY,
game_type VARCHAR(30) NOT NULL, -- 'str8ts', 'sudoku', 'killer_sudoku'
difficulty VARCHAR(20) NOT NULL, -- 'easy', 'medium', 'hard'
puzzle_date DATE NOT NULL,
puzzle VARCHAR(200) NOT NULL, -- zadání (81 znaků pro 9×9)
solution VARCHAR(200) DEFAULT NULL,
extra JSON DEFAULT NULL, -- specifika typu (bw mapa, cage definice...)
source VARCHAR(100) DEFAULT NULL,
fetched_at DATETIME DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY uq_puzzle (game_type, difficulty, puzzle_date)
);
```
Pro Str8ts se do `extra` ukládá `{"bw": "001100011..."}`.
## Generování vlastního PDF (`vykresli_puzzle.py`)
- Knihovna: **reportlab** — vektorová grafika, plynule škálovatelná
- Zvolená velikost: **11×11 cm**, 2 puzzle nad sebou na A4
- Černé buňky: `c.rect(fill=1)`, žlutá čísla na černé, černá čísla na bílé
- Str8ts nemá 3×3 bloky → všechny čáry mřížky stejně silné
## Co nefunguje (neopakovat)
- `page.pdf()` na hlavní stránce → uvítací obrazovka, ne puzzle
- `context.expect_page()` → žádný popup se neotevírá
- Override `window.print` → Game volá `contentWindow.print()`, ne `window.print()`
## Co zbývá
- Zabudovat extrakci + MySQL insert do `stahni_str8ts.py`
- Nahradit stahované PDF z webu vlastním generovaným
- Rozšířit na další typy puzzle (Sudoku, Killer Sudoku)
@@ -0,0 +1,13 @@
CREATE TABLE IF NOT EXISTS puzzles (
id INT AUTO_INCREMENT PRIMARY KEY,
game_type VARCHAR(30) NOT NULL,
difficulty VARCHAR(20) NOT NULL,
puzzle_date DATE NOT NULL,
puzzle VARCHAR(200) NOT NULL,
solution VARCHAR(200) DEFAULT NULL,
extra JSON DEFAULT NULL,
source VARCHAR(100) DEFAULT NULL,
fetched_at DATETIME DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY uq_puzzle (game_type, difficulty, puzzle_date)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
@@ -0,0 +1,137 @@
"""
Průzkumný skript: připojí se na solitaire.org/daily-str8ts/ a vytáhne
surová JS data o puzzle (gameLevels, Game objekt) PŘED generováním PDF.
"""
import asyncio
import json
import sys
sys.stdout.reconfigure(encoding="utf-8")
from playwright.async_api import async_playwright
URL = "https://www.solitaire.org/daily-str8ts/"
async def main():
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True)
context = await browser.new_context(viewport={"width": 1280, "height": 900})
# Krok 1: načti hlavní stránku a najdi iframe s hrou
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
print(f" Nalezen iframe: {game_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}"
)
print(f" Iframe src z DOM: {game_url}")
await page.close()
# Krok 2: otevři URL hry přímo
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)
# --- Průzkum: co je dostupné v JS kontextu ---
# 1) gameLevels
print("\n=== gameLevels ===")
game_levels = await game_page.evaluate("""() => {
if (typeof gameLevels !== 'undefined') return JSON.stringify(gameLevels, null, 2);
return null;
}""")
if game_levels:
print(game_levels[:3000])
if len(game_levels) > 3000:
print(f"... (celkem {len(game_levels)} znaků)")
else:
print(" gameLevels není definováno")
# 2) Game objekt — klíče
print("\n=== Game objekt — klíče ===")
game_keys = await game_page.evaluate("""() => {
if (typeof Game !== 'undefined') {
return Object.keys(Game);
}
return null;
}""")
if game_keys:
print(json.dumps(game_keys, indent=2))
else:
print(" Game není definováno")
# 3) Game objekt — vlastnosti které nejsou funkce
print("\n=== Game — datové vlastnosti ===")
game_data = await game_page.evaluate("""() => {
if (typeof Game === 'undefined') return null;
const result = {};
for (const key of Object.keys(Game)) {
const val = Game[key];
if (typeof val !== 'function') {
try {
result[key] = JSON.parse(JSON.stringify(val));
} catch(e) {
result[key] = String(val);
}
}
}
return result;
}""")
if game_data:
txt = json.dumps(game_data, indent=2, ensure_ascii=False)
print(txt[:5000])
if len(txt) > 5000:
print(f"... (celkem {len(txt)} znaků)")
else:
print(" žádná data")
# 4) Zkusit další globální proměnné
print("\n=== Další globální proměnné ===")
globals_check = await game_page.evaluate("""() => {
const names = ['puzzleData', 'dailyPuzzle', 'gameData', 'levels',
'puzzle', 'boardData', 'board', 'grid', 'cells'];
const found = {};
for (const name of names) {
if (typeof window[name] !== 'undefined') {
found[name] = typeof window[name];
}
}
return found;
}""")
print(json.dumps(globals_check, indent=2))
# 5) Zdrojový kód Game.printDaily
print("\n=== Game.printDaily — zdrojový kód ===")
print_src = await game_page.evaluate("""() => {
if (typeof Game !== 'undefined' && typeof Game.printDaily === 'function') {
return Game.printDaily.toString();
}
return null;
}""")
if print_src:
print(print_src[:3000])
else:
print(" Game.printDaily neexistuje")
await browser.close()
if __name__ == "__main__":
asyncio.run(main())
+191 -52
View File
@@ -1,24 +1,40 @@
"""
Stáhne daily Str8ts puzzle jako PDF ze solitaire.org a uloží do stejné složky.
Název souboru: yyyy-mm-dd Daily Str8ts puzzle.pdf
Stáhne daily Str8ts puzzle data ze solitaire.org, uloží do MySQL,
vygeneruje vlastní PDF (reportlab) a odešle emailem.
"""
import asyncio
import json
import sys
sys.stdout.reconfigure(encoding="utf-8")
sys.stderr.reconfigure(encoding="utf-8")
from datetime import date
from pathlib import Path
from playwright.async_api import async_playwright
import os
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
_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")))
sys.path.insert(0, str(Path(__file__).parent.parent.parent / "Knihovny"))
from EmailMessagingGraph import send_mail
from mysql_db import connect_mysql
from najdi_dropbox import get_dropbox_root
OUTPUT_DIR = Path(get_dropbox_root()) / "!!!Days" / "Downloads Z230"
URL = "https://www.solitaire.org/daily-str8ts/"
RECIPIENT = ["vladimir.buzalka@buzalka.cz", "alica.buzalkova@buzalka.cz"]
RECIPIENT = ["vladimir.buzalka@buzalka.cz"] # TODO: vrátit alica.buzalkova@buzalka.cz
EMAIL_BODY = """Str8ts — pravidla
@@ -33,22 +49,138 @@ Hrací pole 9×9, každá buňka je buď bílá nebo černá.
Rozdíl od Sudoku: nemusíš vyplnit 19 do každé skupiny — jen zajistit, že čísla v každé skupině tvoří „straight" (jako v pokeru).
"""
GRID = 9
BOARD_CM = 11
BOARD = BOARD_CM * cm
CELL = BOARD / GRID
DIFFICULTIES = ["easy", "medium", "hard"]
async def main():
today = date.today().strftime("%Y-%m-%d")
output_path = OUTPUT_DIR / f"{today} Daily Str8ts puzzle.pdf"
if output_path.exists():
print(f"Soubor již existuje: {output_path}")
return
# --- PDF generování ---
def draw_str8ts(c: Canvas, x0: float, y0: float, puzzle: str, bw: str, title: str = ""):
font_size = CELL * 0.55
if title:
c.setFont("ArialBold", 12)
c.drawString(x0, y0 + 5, title)
for idx in range(81):
row, col = divmod(idx, 9)
cell_x = x0 + col * CELL
cell_y = y0 - (row + 1) * CELL
cx = cell_x + CELL / 2
cy = cell_y + CELL * 0.3
is_black = bw[idx] == "1"
ch = puzzle[idx]
if is_black:
c.setFillColor(colors.black)
c.rect(cell_x, cell_y, CELL, CELL, fill=1, stroke=0)
if ch in "123456789":
c.setFillColor(colors.yellow if is_black else colors.black)
c.setFont("ArialBold", font_size)
c.drawCentredString(cx, cy, ch)
c.setFillColor(colors.black)
for i in range(GRID + 1):
c.setLineWidth(0.8)
c.line(x0, y0 - i * CELL, x0 + BOARD, y0 - i * CELL)
c.line(x0 + i * CELL, y0, x0 + i * CELL, y0 - BOARD)
def draw_str8ts_small(c: Canvas, x0: float, y0: float, board_cm: float,
solution: str, bw: str, title: str = ""):
board = board_cm * cm
cell = board / GRID
font_size = cell * 0.55
if title:
c.setFont("ArialBold", 8)
c.drawString(x0, y0 + 4, title)
for idx in range(81):
row, col = divmod(idx, 9)
cell_x = x0 + col * cell
cell_y = y0 - (row + 1) * cell
cx = cell_x + cell / 2
cy = cell_y + cell * 0.3
is_black = bw[idx] == "1"
ch = solution[idx]
if is_black:
c.setFillColor(colors.black)
c.rect(cell_x, cell_y, cell, cell, fill=1, stroke=0)
if ch in "123456789":
c.setFillColor(colors.yellow if is_black else colors.black)
c.setFont("ArialBold", max(font_size, 4))
c.drawCentredString(cx, cy, ch)
c.setFillColor(colors.black)
for i in range(GRID + 1):
c.setLineWidth(0.5)
c.line(x0, y0 - i * cell, x0 + board, y0 - i * cell)
c.line(x0 + i * cell, y0, x0 + i * cell, y0 - board)
def generate_pdf(output_path: Path, puzzles: list[dict], title_date: str):
"""puzzles = [{"difficulty": ..., "puzzle": ..., "bw": ..., "solution": ...}, ...]"""
page_w, page_h = A4
x0 = (page_w - BOARD) / 2
c = Canvas(str(output_path), pagesize=A4)
# Stránky se zadáním — 2 puzzle nad sebou
for i in range(0, len(puzzles), 2):
page_puzzles = puzzles[i:i + 2]
for j, p in enumerate(page_puzzles):
y0 = page_h - 2 * cm - j * (BOARD + 3 * cm)
draw_str8ts(c, x0, y0, p["puzzle"], p["bw"], p["difficulty"].capitalize())
c.showPage()
# Poslední stránka — řešení 6×6 cm
sol_cm = 6
sol_board = sol_cm * cm
gap = 1.5 * cm
sol_x0 = (page_w - sol_board) / 2
c.setFont("ArialBold", 14)
c.drawCentredString(page_w / 2, page_h - 2 * cm, "Řešení")
y_cursor = page_h - 3.5 * cm
for p in puzzles:
draw_str8ts_small(c, sol_x0, y_cursor, sol_cm, p["solution"], p["bw"],
p["difficulty"].capitalize())
y_cursor -= sol_board + gap
c.showPage()
c.save()
# --- MySQL ---
def save_to_mysql(puzzle_date: str, puzzles: list[dict]):
conn = connect_mysql(database="puzzle")
cur = conn.cursor()
for p in puzzles:
cur.execute(
"INSERT IGNORE INTO puzzles (game_type, difficulty, puzzle_date, puzzle, solution, extra, source) "
"VALUES (%s, %s, %s, %s, %s, %s, %s)",
("str8ts", p["difficulty"], puzzle_date, p["puzzle"], p["solution"],
json.dumps({"bw": p["bw"]}), "solitaire.org"),
)
inserted = cur.rowcount
cur.close()
conn.close()
return inserted
# --- Extrakce z webu ---
async def fetch_puzzles(mmdd: str) -> list[dict]:
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True)
context = await browser.new_context(
viewport={"width": 1280, "height": 900},
)
context = await browser.new_context(viewport={"width": 1280, "height": 900})
# Krok 1: načti hlavní stránku a zjisti URL iframu s hrou
page = await context.new_page()
print(f"Načítám {URL} ...")
await page.goto(URL, wait_until="networkidle", timeout=60_000)
@@ -57,61 +189,68 @@ async def main():
for frame in page.frames:
if frame.url != page.url and frame.url.strip() not in ("", "about:blank"):
game_url = frame.url
print(f" Nalezen iframe: {game_url}")
break
if not game_url:
# Zkus najít iframe src přímo v DOM
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}"
print(f" Iframe src z DOM: {game_url}")
await page.close()
# Krok 2: otevři URL hry
game_page = await context.new_page()
target_url = game_url if game_url else URL
print(f"Načítám hru přímo: {target_url} ...")
print(f"Načítám hru: {target_url} ...")
await game_page.goto(target_url, wait_until="networkidle", timeout=60_000)
# Game.printDaily() vytvoří iframe #printFrame a zapíše do něj puzzle HTML
await game_page.evaluate("Game.printDaily()")
await game_page.wait_for_timeout(1_000)
data = await game_page.evaluate("""(key) => {
const result = {};
for (const diff of ['easy', 'medium', 'hard']) {
if (gameLevels[diff] && gameLevels[diff][key]) {
result[diff] = gameLevels[diff][key];
}
}
return result;
}""", mmdd)
# Přečti HTML přímo z iframu
iframe_html = await game_page.evaluate("""() => {
const iframe = document.getElementById('printFrame');
if (!iframe) return null;
try {
return iframe.contentDocument.documentElement.outerHTML;
} catch(e) { return null; }
}""")
if not iframe_html:
raise RuntimeError("Nepodařilo se získat HTML z #printFrame")
print(f" iframe HTML zachycen ({len(iframe_html)} znaků), ukládám PDF...")
# Přidej base URL aby se načetly obrázky (img/black.png, img/title.jpg)
base_url = "https://www.solitaire.org/daily-str8ts/"
iframe_html = iframe_html.replace("<head>", f'<head><base href="{base_url}">', 1)
# Načti HTML do nové stránky a ulož jako PDF
pdf_page = await context.new_page()
await pdf_page.set_content(iframe_html, wait_until="networkidle")
await pdf_page.wait_for_timeout(500)
await pdf_page.pdf(
path=str(output_path),
format="A4",
print_background=True,
margin={"top": "10mm", "bottom": "10mm", "left": "10mm", "right": "10mm"},
)
print(f"PDF uloženo: {output_path}")
await browser.close()
puzzles = []
for diff in DIFFICULTIES:
if diff in data:
puzzles.append({
"difficulty": diff,
"puzzle": data[diff]["puzzle"],
"bw": data[diff]["bw"],
"solution": data[diff]["solution"],
})
return puzzles
# --- Main ---
async def main():
today = date.today()
today_str = today.strftime("%Y-%m-%d")
mmdd = today.strftime("%m-%d")
output_path = OUTPUT_DIR / f"{today_str} Daily Str8ts puzzle.pdf"
if output_path.exists():
print(f"Soubor již existuje: {output_path}")
return
puzzles = await fetch_puzzles(mmdd)
if not puzzles:
raise RuntimeError(f"Žádná data pro {mmdd}")
print(f"Staženo {len(puzzles)} puzzle pro {today_str}")
inserted = save_to_mysql(today_str, puzzles)
print(f"MySQL: vloženo {inserted} nových řádků")
generate_pdf(output_path, puzzles, today_str)
print(f"PDF uloženo: {output_path}")
send_mail(
to=RECIPIENT,
subject="Posílám dnešní Str8ts puzzle v příloze",
@@ -0,0 +1,94 @@
"""
Vykreslí Str8ts puzzle do PDF z dat v MySQL tabulce puzzles.
Formát: 2 puzzle nad sebou na A4, velikost 11×11 cm.
"""
import json
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.pdfgen.canvas import Canvas
from mysql_db import connect_mysql
OUTPUT = Path(__file__).parent / "test_grid2.pdf"
GRID = 9
BOARD_CM = 11
BOARD = BOARD_CM * cm
CELL = BOARD / GRID
def draw_str8ts(c: Canvas, x0: float, y0: float, puzzle: str, bw: str, title: str = ""):
font_size = CELL * 0.55
if title:
c.setFont("Helvetica-Bold", 12)
c.drawString(x0, y0 + 5, title)
for idx in range(81):
row, col = divmod(idx, 9)
cell_x = x0 + col * CELL
cell_y = y0 - (row + 1) * CELL
cx = cell_x + CELL / 2
cy = cell_y + CELL * 0.3
is_black = bw[idx] == "1"
ch = puzzle[idx]
if is_black:
c.setFillColor(colors.black)
c.rect(cell_x, cell_y, CELL, CELL, fill=1, stroke=0)
if ch in "123456789":
c.setFillColor(colors.yellow if is_black else colors.black)
c.setFont("Helvetica-Bold", font_size)
c.drawCentredString(cx, cy, ch)
c.setFillColor(colors.black)
for i in range(GRID + 1):
c.setLineWidth(0.8)
c.line(x0, y0 - i * CELL, x0 + BOARD, y0 - i * CELL)
c.line(x0 + i * CELL, y0, x0 + i * CELL, y0 - BOARD)
def main():
conn = connect_mysql(database="puzzle")
cur = conn.cursor()
cur.execute(
"SELECT difficulty, puzzle, extra FROM puzzles "
"WHERE game_type='str8ts' AND puzzle_date='2026-05-08' "
"ORDER BY FIELD(difficulty, 'easy', 'medium', 'hard')"
)
rows = cur.fetchall()
cur.close()
conn.close()
if not rows:
print("Žádná data pro dnešek.")
return
page_w, page_h = A4
x0 = (page_w - BOARD) / 2
c = Canvas(str(OUTPUT), pagesize=A4)
# 2 puzzle na stránku
for i in range(0, len(rows), 2):
page_rows = rows[i:i + 2]
for j, (difficulty, puzzle, extra) in enumerate(page_rows):
bw = json.loads(extra)["bw"]
y0 = page_h - 2 * cm - j * (BOARD + 3 * cm)
draw_str8ts(c, x0, y0, puzzle, bw, difficulty.capitalize())
c.showPage()
c.save()
print(f"PDF uloženo: {OUTPUT}")
if __name__ == "__main__":
main()
@@ -0,0 +1,94 @@
"""
Vykreslí jedno Str8ts puzzle v různých velikostech (18×18 cm až 2×2 cm),
každou velikost na samostatnou stránku A4, vycentrované.
"""
import json
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.pdfgen.canvas import Canvas
from mysql_db import connect_mysql
OUTPUT = Path(__file__).parent / "str8ts_velikosti.pdf"
GRID = 9
def draw_str8ts(c: Canvas, x0: float, y0: float, cell: float,
puzzle: str, bw: str, title: str = ""):
board = GRID * cell
font_size = cell * 0.55
if title:
c.setFont("Helvetica-Bold", 12)
c.drawCentredString(x0 + board / 2, y0 + 5, title)
for idx in range(81):
row, col = divmod(idx, 9)
cell_x = x0 + col * cell
cell_y = y0 - (row + 1) * cell
cx = cell_x + cell / 2
cy = cell_y + cell * 0.3
is_black = bw[idx] == "1"
ch = puzzle[idx]
if is_black:
c.setFillColor(colors.black)
c.rect(cell_x, cell_y, cell, cell, fill=1, stroke=0)
if ch in "123456789":
c.setFillColor(colors.yellow if is_black else colors.black)
c.setFont("Helvetica-Bold", max(font_size, 4))
c.drawCentredString(cx, cy, ch)
c.setFillColor(colors.black)
for i in range(GRID + 1):
c.setLineWidth(0.8)
c.line(x0, y0 - i * cell, x0 + board, y0 - i * cell)
c.line(x0 + i * cell, y0, x0 + i * cell, y0 - board)
def main():
conn = connect_mysql(database="puzzle")
cur = conn.cursor()
cur.execute(
"SELECT puzzle, extra FROM puzzles "
"WHERE game_type='str8ts' AND difficulty='easy' AND puzzle_date='2026-05-08'"
)
row = cur.fetchone()
cur.close()
conn.close()
if not row:
print("Žádná data.")
return
puzzle = row[0]
bw = json.loads(row[1])["bw"]
page_w, page_h = A4
c = Canvas(str(OUTPUT), pagesize=A4)
for size_cm in range(18, 1, -1):
board = size_cm * cm
cell = board / GRID
x0 = (page_w - board) / 2
y0 = (page_h + board) / 2
title = f"Str8ts Easy — {size_cm}×{size_cm} cm"
draw_str8ts(c, x0, y0, cell, puzzle, bw, title)
c.showPage()
c.save()
print(f"PDF uloženo: {OUTPUT} ({18 - 2} stránek)")
if __name__ == "__main__":
main()
+49
View File
@@ -0,0 +1,49 @@
# DailySudoku — technické poznámky
## Přehled skriptů
| Skript | Popis |
|--------|-------|
| `preskumaj_sudoku.py` | Průzkumný — vytáhne `gameLevels` z JS kontextu stránky |
| `stahni_sudoku.py` | Stáhne data z webu a uloží do MySQL (celý rok najednou) |
| `vykresli_sudoku.py` | Generuje PDF z dat v MySQL (reportlab, vektorové) |
## Zdroj dat
Stránka: https://www.solitaire.org/daily-sudoku/
Stejná architektura jako ostatní puzzle — `game.php` načte `gameLevels` s daty pro celý rok (366 dní × 4 obtížnosti). Klíče `"MM-DD"`, bez roku.
## Obtížnosti
| Obtížnost | Mřížka |
|-----------|--------|
| easy | 9×9 |
| medium | 9×9 |
| hard | 9×9 |
| expert | 9×9 |
## Datová struktura `gameLevels`
Každý záznam je objekt `{board, solution}`:
```json
{
"board": "2.78..3.6..5......39.614.7...",
"solution": "247895316165723984398614275..."
}
```
- `board` = zadání, 81 znaků (9×9), tečka = prázdná buňka
- `solution` = řešení, 81 znaků číslic 19
## MySQL tabulka `puzzle.puzzles`
Sdílená tabulka s ostatními puzzle. Pro Sudoku:
- `game_type` = `'sudoku'`
- `difficulty` = `'easy'` / `'medium'` / `'hard'` / `'expert'`
- `puzzle` = board string (zadání s tečkami)
- `solution` = solution string (řešení)
- `extra` = `{"grid_size": 9}`
- `source` = `'solitaire.org'`
Stav: celý rok 2026 naplněn (1460 řádků = 365 dní × 4 obtížnosti).
@@ -0,0 +1,100 @@
"""
Průzkumný skript: připojí se na solitaire.org/daily-sudoku/ a vytáhne
surová JS data o puzzle (gameLevels, Game objekt).
"""
import asyncio
import json
import sys
sys.stdout.reconfigure(encoding="utf-8")
from playwright.async_api import async_playwright
URL = "https://www.solitaire.org/daily-sudoku/"
async def main():
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
print(f" Nalezen iframe: {game_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}"
print(f" Iframe src z DOM: {game_url}")
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)
# 1) gameLevels — struktura
print("\n=== gameLevels — struktura ===")
structure = await game_page.evaluate("""() => {
if (typeof gameLevels === 'undefined') return null;
const result = {};
for (const diff of Object.keys(gameLevels)) {
const keys = Object.keys(gameLevels[diff]);
result[diff] = {
count: keys.length,
first_keys: keys.slice(0, 3),
last_keys: keys.slice(-3),
sample_value: gameLevels[diff][keys[0]]
};
}
return result;
}""")
if structure:
print(json.dumps(structure, indent=2, ensure_ascii=False)[:5000])
else:
print(" gameLevels není definováno")
# 2) Dnešní data
print("\n=== Dnešní data (05-08) ===")
today_data = await game_page.evaluate("""() => {
const key = '05-08';
const result = {};
if (typeof gameLevels === 'undefined') return null;
for (const diff of Object.keys(gameLevels)) {
if (gameLevels[diff] && gameLevels[diff][key]) {
result[diff] = gameLevels[diff][key];
}
}
return result;
}""")
if today_data:
print(json.dumps(today_data, indent=2, ensure_ascii=False)[:5000])
else:
print(" žádná data pro dnešek")
# 3) Game objekt — klíče
print("\n=== Game objekt — klíče ===")
game_keys = await game_page.evaluate("""() => {
if (typeof Game !== 'undefined') return Object.keys(Game);
return null;
}""")
if game_keys:
print(json.dumps(game_keys, indent=2))
else:
print(" Game není definováno")
await browser.close()
if __name__ == "__main__":
asyncio.run(main())
@@ -0,0 +1,97 @@
"""
Stáhne daily Sudoku 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-sudoku/"
DIFFICULTIES = ["easy", "medium", "hard", "expert"]
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]
board = entry["board"]
solution = entry["solution"]
cur.execute(
"INSERT IGNORE INTO puzzles (game_type, difficulty, puzzle_date, puzzle, solution, extra, source) "
"VALUES (%s, %s, %s, %s, %s, %s, %s)",
("sudoku", diff, date_str, board, solution,
json.dumps({"grid_size": 9}), "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,121 @@
"""
Vykreslí Sudoku 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_sudoku.pdf"
def draw_sudoku(c: Canvas, x0: float, y0: float, cell: float,
board: str, title: str = "", show_solution: bool = False,
solution: str = ""):
num_font = max(cell * 0.5, 7)
given_font = max(cell * 0.5, 7)
thin = 0.5
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 - 9 * cell, 9 * cell, 9 * cell, fill=1, stroke=0)
# Číslice
for i in range(81):
row = i // 9
col = i % 9
cx = x0 + col * cell + cell / 2
cy = y0 - (row + 1) * cell + cell * 0.3
ch = board[i]
if ch != ".":
c.setFillColor(colors.black)
c.setFont("ArialBold", given_font)
c.drawCentredString(cx, cy, ch)
elif show_solution and solution:
c.setFillColor(colors.Color(0.4, 0.4, 0.4))
c.setFont("Arial", num_font)
c.drawCentredString(cx, cy, solution[i])
# Tenké čáry
c.setStrokeColor(colors.Color(0.6, 0.6, 0.6))
c.setLineWidth(thin)
for i in range(1, 9):
if i % 3 == 0:
continue
c.line(x0, y0 - i * cell, x0 + 9 * cell, y0 - i * cell)
c.line(x0 + i * cell, y0, x0 + i * cell, y0 - 9 * cell)
# Tlusté čáry (3×3 bloky + vnější okraj)
c.setStrokeColor(colors.black)
c.setLineWidth(thick)
for i in range(0, 10, 3):
c.line(x0, y0 - i * cell, x0 + 9 * cell, y0 - i * cell)
c.line(x0 + i * cell, y0, x0 + i * cell, y0 - 9 * cell)
def main():
conn = connect_mysql(database="puzzle")
cur = conn.cursor()
cur.execute(
"SELECT difficulty, puzzle, solution FROM puzzles "
"WHERE game_type='sudoku' AND puzzle_date='2026-05-08' "
"ORDER BY FIELD(difficulty, 'easy', 'medium', 'hard', 'expert') "
"LIMIT 1"
)
row = cur.fetchone()
cur.close()
conn.close()
if not row:
print("Žádná data.")
return
difficulty, board, solution = row
page_w, page_h = A4
board_cm = 11
cell = board_cm * cm / 9
board_px = 9 * cell
c = Canvas(str(OUTPUT), pagesize=A4)
# Zadání
x0 = (page_w - board_px) / 2
y0 = page_h - 2 * cm
draw_sudoku(c, x0, y0, cell, board,
f"Sudoku {difficulty.capitalize()} — 2026-05-08")
# Řešení
y0_sol = y0 - board_px - 3 * cm
draw_sudoku(c, x0, y0_sol, cell, board,
"Řešení", show_solution=True, solution=solution)
c.save()
print(f"PDF uloženo: {OUTPUT}")
if __name__ == "__main__":
main()
@@ -0,0 +1,89 @@
"""
Průzkumný skript: připojí se na solitaire.org/daily-suguru/ a vytáhne
surová JS data o puzzle (gameLevels, Game objekt).
"""
import asyncio
import json
import sys
sys.stdout.reconfigure(encoding="utf-8")
from playwright.async_api import async_playwright
URL = "https://www.solitaire.org/daily-suguru/"
async def main():
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
print(f" Nalezen iframe: {game_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}"
print(f" Iframe src z DOM: {game_url}")
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)
# 1) gameLevels — struktura
print("\n=== gameLevels — struktura ===")
structure = await game_page.evaluate("""() => {
if (typeof gameLevels === 'undefined') return null;
const result = {};
for (const diff of Object.keys(gameLevels)) {
const keys = Object.keys(gameLevels[diff]);
result[diff] = {
count: keys.length,
first_keys: keys.slice(0, 3),
last_keys: keys.slice(-3),
sample_value: gameLevels[diff][keys[0]]
};
}
return result;
}""")
if structure:
print(json.dumps(structure, indent=2, ensure_ascii=False)[:8000])
else:
print(" gameLevels není definováno")
# 2) Dnešní data
print("\n=== Dnešní data (05-08) ===")
today_data = await game_page.evaluate("""() => {
const key = '05-08';
const result = {};
if (typeof gameLevels === 'undefined') return null;
for (const diff of Object.keys(gameLevels)) {
if (gameLevels[diff] && gameLevels[diff][key]) {
result[diff] = gameLevels[diff][key];
}
}
return result;
}""")
if today_data:
print(json.dumps(today_data, indent=2, ensure_ascii=False)[:8000])
else:
print(" žádná data pro dnešek")
await browser.close()
if __name__ == "__main__":
asyncio.run(main())