Skip to content

Commit d0d00e2

Browse files
authored
Merge pull request #68 from olivierphi/better-lichess-import
[database] Slight improvement of the Lichess puzzles import
2 parents 87c3f80 + 76d3bcf commit d0d00e2

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/apps/daily_challenge/management/commands/dailychallenge_create_from_lichess_puzzles_csv.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22
import re
33
from pathlib import Path
44

5+
import chess
56
from django.core.management import BaseCommand
67
from django.db.models.functions import Substr
78

89
from apps.daily_challenge.models import DailyChallenge
910

11+
THEMES_TO_IGNORE = {
12+
"oneMove",
13+
"mateIn1",
14+
"mateIn2",
15+
}
16+
1017

1118
class Command(BaseCommand):
1219
def add_arguments(self, parser):
@@ -18,20 +25,20 @@ def add_arguments(self, parser):
1825
parser.add_argument(
1926
"--batch-size",
2027
type=int,
21-
default=1000,
28+
default=10, # low batch size to avoid OOM on low-end Fly.io plan
2229
help="DailyChallenges creation batch size.",
2330
)
2431
parser.add_argument(
2532
"--min-popularity",
2633
type=int,
27-
default=85,
34+
default=90,
2835
help="Only consider puzzles that have at least that Popularity.",
2936
)
3037
parser.add_argument(
3138
"--rating-range",
3239
type=rating_range,
3340
dest="rating_min_max",
34-
default=(800, 1300),
41+
default=(900, 1100),
3542
help="Only consider puzzles that have a Rating in this 'min-max' range.",
3643
)
3744
parser.add_argument(
@@ -63,6 +70,10 @@ def handle(
6370
with csv_file_path.open(newline="") as csv_file:
6471
reader = csv.DictReader(csv_file)
6572
for row in reader:
73+
themes: set[str] = set(row.get("Themes", "").split())
74+
if themes & THEMES_TO_IGNORE:
75+
self.stdout.write("Skipping too short puzzle")
76+
continue
6677
if (popularity := int(row["Popularity"])) < min_popularity:
6778
self.stdout.write(f"Skipping puzzle with Popularity {popularity}")
6879
continue
@@ -80,6 +91,10 @@ def handle(
8091
f"Creating DailyChallenge for puzzle '{puzzle_id}' with Popularity {popularity}, rating {rating}, FEN '{fen}'"
8192
)
8293

94+
if " b " in fen: # quick and dirty way to detect if black is to move
95+
self.stdout.write("Mirroring puzzle with black to move")
96+
fen = chess.Board(fen).mirror().fen()
97+
8398
current_batch.append(
8499
DailyChallenge(
85100
source=f"lichess-{puzzle_id}",

0 commit comments

Comments
 (0)