Skip to content

Instantly share code, notes, and snippets.

@tors42
Created April 28, 2024 16:39
Show Gist options
  • Save tors42/6fb3002c0a1bda6b04a157f09bf4d46e to your computer and use it in GitHub Desktop.
Save tors42/6fb3002c0a1bda6b04a157f09bf4d46e to your computer and use it in GitHub Desktop.
Download 9 months of solved puzzles into file puzzles.txt
if (! Files.exists(Path.of("chariot.jar"))) {
Files.write(Path.of("chariot.jar"), URI.create(
"https://repo1.maven.org/maven2/io/github/tors42/chariot/0.0.87/chariot-0.0.87.jar"
).toURL().openStream().readAllBytes());
}
/env --class-path chariot.jar
var auth = chariot.Client.auth(
uri -> System.out.println("\nVisit the following URL to grant permission:\n\n" + uri),
pkce -> pkce.scope(chariot.Client.Scope.puzzle_read));
if (auth instanceof chariot.Client.AuthOk ok) {
var dateFormat = java.time.format.DateTimeFormatter.ISO_LOCAL_DATE;
var count = new java.util.concurrent.atomic.AtomicInteger();
var puzzles = ok.client().puzzles().activity(p -> p.since(now -> now.minusMonths(9))).stream().
peek(__ -> {if (count.incrementAndGet() % 10 == 0) System.out.print(
"\rDownloaded %d puzzles".formatted(count.get()));}).map(p ->
"https://lichess.org/training/%s - %s".formatted(
p.puzzle().id(), p.date().format(dateFormat))).toList();
Files.write(Path.of("puzzles.txt"), puzzles);
ok.client().revokeToken();
System.out.println("\nDone");
} else {
System.out.println("Failed to authenticate: " + auth);
}
/exit
@tors42
Copy link
Author

tors42 commented Apr 28, 2024

Run with at least Java 17 (https://jdk.java.net/22 is latest at time being)

$ jshell puzzles.jsh 

To read puzzle history, one needs to grant access to the puzzle:read scope,
which the script will ask for (link from this example is expired):

Visit the following URL to grant permission:

https://lichess.org/oauth?client_id=chariot&code_challenge_method=S256&response_type=code&code_challenge=PtKDhoZwfvF7veOVvPISMtfvaXwhu-yFF_2h3HE5Bfk&scope=puzzle%3Aread&state=Biw62UyE&redirect_uri=http%3A%2F%2F127.0.0.1%3A37433%2F

Opening the link and there clicking the Authorize button will make the script continue,

Downloaded 250 puzzles
Done

The links to the puzzles are now in a file named puzzles.txt

$ cat puzzles.txt
...
https://lichess.org/training/ncgjO - 2023-07-31
https://lichess.org/training/qh8Kr - 2023-07-30
https://lichess.org/training/EIOOK - 2023-07-29
https://lichess.org/training/gWbaK - 2023-07-29

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment