Created
April 28, 2024 16:39
-
-
Save tors42/6fb3002c0a1bda6b04a157f09bf4d46e to your computer and use it in GitHub Desktop.
Download 9 months of solved puzzles into file puzzles.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run with at least Java 17 (https://jdk.java.net/22 is latest at time being)
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):
Opening the link and there clicking the Authorize button will make the script continue,
The links to the puzzles are now in a file named
puzzles.txt