Skip to content

Instantly share code, notes, and snippets.

@tors42
Created January 29, 2024 22:30
Show Gist options
  • Save tors42/4e5b4daffd6c4a3ebf3f549269eff108 to your computer and use it in GitHub Desktop.
Save tors42/4e5b4daffd6c4a3ebf3f549269eff108 to your computer and use it in GitHub Desktop.
jshell script to backup studies into a single file
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.82/chariot-0.0.82.jar"
).toURL().openStream().readAllBytes());
}
/env --class-path chariot.jar
import chariot.*;
import chariot.Client.*;
import chariot.model.*;
void exportStudiesWithClientForUserId(Client client, String userId) throws Exception {
System.out.println("Downloading studies for " + userId);
var studies = client.studies().exportStudiesByUserId(userId, p -> p.source()).stream().map(Pgn::toString).toList();
if (studies.isEmpty()) {
System.out.println("No studies found by " + userId);
return;
}
var outputFile = Path.of(userId+".studies.pgn");
Files.write(outputFile, studies);
System.out.println("Wrote " + studies.size() + " studies to " + outputFile);
}
String userId = System.getProperty("user");
if (userId != null) {
exportStudiesWithClientForUserId(Client.basic(), userId);
} else {
var loginResult = Client.auth(uri -> {
if (java.awt.Desktop.isDesktopSupported()) {
var desktop = java.awt.Desktop.getDesktop();
if (desktop.isSupported(java.awt.Desktop.Action.BROWSE)) {
try {
System.out.println("Opening desktop browser");
desktop.browse(uri);
return;
} catch(Exception e) {
System.out.println("Failed to open browser");
e.printStackTrace();
}
}
}
System.out.println("Visit following Lichess URL to grant this script permission to export private studies:");
System.out.println(uri);
},
pkce -> pkce.scope(Scope.study_read));
if (loginResult instanceof AuthOk ok) {
System.out.println("Successfully logged in");
var profileResult = ok.client().account().profile();
if (profileResult instanceof Entry<UserAuth> profile) {
exportStudiesWithClientForUserId(ok.client(), profile.entry().id());
} else {
System.err.println("Failed to lookup profile " + profileResult);
}
System.out.println("Revoking token (Logging out)");
ok.client().revokeToken();
} else {
System.err.println("Failed to login " + loginResult);
}
}
/exit
@tors42
Copy link
Author

tors42 commented Jan 29, 2024

Runs with jshell from Java 17 or newer (https://jdk.java.net/21/)

Example without logging in, (if wanting to backup only public studies),

$ jshell -R-Duser=tors42 backup-studies.jsh
Downloading studies for tors42
Wrote 1 studies to tors42.studies.pgn

Example with logging in (if wanting to backup also private studies),

$ jshell backup-studies.jsh
Opening desktop browser

Now the Web Browser opens a Lichess web page, asking to authorize the script to read studies.
If clicking Authorize,

Successfully logged in
Downloading studies for tors42
Wrote 72 studies to tors42.studies.pgn
Revoking token (Logging out)

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