Created
January 29, 2024 22:30
-
-
Save tors42/4e5b4daffd6c4a3ebf3f549269eff108 to your computer and use it in GitHub Desktop.
jshell script to backup studies into a single file
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.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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Runs with
jshell
from Java 17 or newer (https://jdk.java.net/21/)Example without logging in, (if wanting to backup only public studies),
Example with logging in (if wanting to backup also private studies),
Now the Web Browser opens a Lichess web page, asking to authorize the script to read studies.
If clicking Authorize,